xcal

土日に色をつけてみる

今日をハイライトではエスケープシーケンスを使うことで背景色の変更ができました。今度は文字色を変えてみましょう。例えば休日である土日をマゼンタに。

calendar4実行結果

#! /usr/bin/swift
import Foundation
var curr_y: Int = 0
var curr_m: Int = 0
var curr_d: Int = 0
// Date型の引数を受け取り、DateComponents型を返す関数
func getCalComp(date: Date) -> DateComponents {
    let cal: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
    let components: DateComponents = cal.components([.year, .month, .day, .weekday], from:date)
    return components
}
// 一日始まりのDateComponents型の引数を受け取り、その月の配列を返す関数
func getCalendar(components: DateComponents) -> [String] {
    var cal_seq: [String] = ["日","月","火","水","木","金","土"]
    var endofmonth: [Int] = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if (components.year! % 4) == 0 && (components.year! % 100) != 0 || (components.year! % 400) == 0 {
        endofmonth[2]+=1
    }
    for _ in 0..<42 {
        cal_seq.append("  ")
    }
    var datestring: String
    for var i in 1..<endofmonth[components.month!]+1 {
        if i < 10 {
            datestring = " \(i)"
        } else {
            datestring = "\(i)"
        }
        if components.year! == curr_y && components.month! == curr_m && i == curr_d {
            datestring = "\u{001B}[0;47m\(datestring)\u{001B}[0;30m"
        }
        cal_seq[ (6 + i) + (components.weekday! - 1) ] = datestring
    }
    return cal_seq
}
// 実質プログラムの始まり
var flag_y: Int = 1
var flag_m: Int = 1
var spec_y: Int = 0
var spec_m: Int = 0
let arguments: [String] = CommandLine.arguments
if arguments.count != 1 {
    switch arguments[1] {
    case "-y":
        flag_y = 4
        flag_m = 3
        if arguments.count == 3 {
            spec_y = Int(arguments[2])!
        }
    case "-3":
        flag_m = 3
    case "-m":
        if arguments.count == 3 {
            spec_m = Int(arguments[2])!
        }
    default:
        break
    }
}
let je_flag = 1
let now: Date = Date()
var cal_comp: DateComponents = getCalComp(date:now)
curr_y = cal_comp.year!
curr_m = cal_comp.month!
curr_d = cal_comp.day!
if flag_y == 4 {
    cal_comp.month = 1
    if spec_y != 0 {
        cal_comp.year = spec_y
    }
} else if flag_y == 1 && flag_m == 3 {
    cal_comp.month!-=1
}
if spec_m != 0 {
    cal_comp.month = spec_m
}
cal_comp.day = 1
var sequence: [String] = []
var header: String = ""
var header_y: String = ""
var header_m: [String] = ["", "", "", "", "", "", "", "", "", "", "", ""]
for var i in 0..<12 {
    var calen: Date = Calendar.current.date(from:cal_comp)!
    cal_comp = getCalComp(date:calen)
    sequence += getCalendar(components:cal_comp)
    if i == 1 { header_y += "\(cal_comp.year!)" }
    header_m[i] = "\(cal_comp.month!)月"
    cal_comp.month!+=1
}
var length: Int
if flag_m == 3 {
    length = (65 - header_y.characters.count) / 2
    for var i in 0..<length+1 {
        header_y = " " + header_y
    }
    print("\(header_y)\n")
}
var hol: [Int] = [0, 6]
for var l in 0..<flag_y {
    if flag_m == 3 {
        for var i in 0..<flag_m {
            length = (21 - (header_m[i + 3 * l].characters.count + je_flag)) / 2
            for var j in 0..<length {
                header_m[i + 3 * l] = " " + header_m[i + 3 * l]
            }
            length = 22 - (header_m[i + 3 * l].characters.count + je_flag)
            for var j in 0..<length {
                header_m[i + 3 * l] = header_m[i + 3 * l] + " "
            }
            header += header_m[i + 3 * l]
        }
    } else {
        header = "\(header_m[0]) \(header_y)"
        length = (21 - (header.characters.count + je_flag)) / 2
        for var j in 0..<length {
            header = " " + header
        }
    }
    print(header)
    header = ""
    for var i in 0..<7 {
        for var j in 0..<flag_m {
            for var k in 0..<7 {
                for l in hol{
                    if l == k {
                        print("\u{001B}[0;35m", terminator:"")
                    }
                }
                print("\(sequence[i * 7 + ((j + 3 * l) * 49 + k)])\u{001B}[0;30m ", terminator:"")
            }
            print(" ", terminator:"")
        }
        print("")
    }
}

変更点は僅か数行です。

98行目に対象となる曜日を保管する配列holを定義し、日曜をあらわす”0″と土曜をあらわす”6″を代入しました。この情報をもとに124から128行目で対象の曜日だけマゼンタ色をあらわすエスケープシーケンス”[0;35m”を表示しています。

ただしこのままだと、その後の表示が全てマゼンタになってしまうので129行目を少し変更して、必ず各データの最後に”[0;30m”を表示して後に続く表示を黒色に戻しています。