xcal

今日をハイライト

さて、シェルスクリプト版のxcalの目玉機能、今日の日付をハイライトするを実装してみます。

#! /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")
}
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 {
                print("\(sequence[i * 7 + ((j + 3 * l) * 49 + k)]) ", terminator:"")
            }
            print(" ", terminator:"")
        }
        print("")
    }
}

3から5行目で今日の年月日を覚えておく変数を定義。63から65行目で実際に今日の年月日を代入しています。各月のデータ生成において29行目で今日の年月日と比較し、今日の日付だったときだけ30行目で背景色をグレーにします。

背景色の変更はエスケープシーケンスを使用しています。printコマンドで文字列を表示する際、エスケープシーケンスという特殊コードを埋め込むことで表示色をコントロールできます。

エスケープシーケンスの始まりは"\u{001B}"というユニコードで指定します。"[0;47m"は文字色が黒で背景色がグレー、"[0;30m"は文字色が黒で背景色が白になります。

calendar3実行結果