xcal

英語対応カレンダー

環境変数を読んでみるで言語表示の対応状況がわかるようになったので、早速日英自動切り替え機能を実装します。

calendar5実行結果

少しごちゃごちゃしてきたので、定数と変数も少し整理しました。英語環境への切り替えのために、3,4行目に英語の月表示と曜日表示データを追加してあります。環境変数LANGの値に応じて、表示を切り替えるためです。

8から12行目の環境変数チェックの部分は前回コードの関数部分と同じです。実際の呼び出し部分は44行目で、通常0となっているd_flag.jeを日本語表示環境の時だけ1に書き換えます。

これにより22行目で月データの最初に曜日配列のデータを代入する際に、英語と日本語とを適切に選択できるようになります。配列自体は4行目のように配列を配列の値に取ることができます。

#! /usr/bin/swift
import Foundation
let month_odr: [String] = ["January", "Febrary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let week_odr: [[String]] = [["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], ["日","月","火","水","木","金","土"]]
let esc = (fg_bk:"\u{001B}[0;30m", fg_mg:"\u{001B}[0;35m", bg_wt:"\u{001B}[1;47m", bg_gy:"\u{001B}[0;47m")
var curr = (year:0, month:0, day:0)
var d_flag = (raws:1, cols:1, year:0, month:0, je:0, pos:0)
// シェル変数名を指定すると値を返す関数
func getEnvironmentVar(name: String) -> String? {
	guard let rawValue = getenv(name) else { return nil }
	return String(utf8String: rawValue)
}
// 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] = week_odr[d_flag.je]
    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.year && components.month! == curr.month && i == curr.day {
            datestring = "\(esc.bg_gy)\(datestring)\(esc.bg_wt)"
        }
        cal_seq[ (6 + i) + (components.weekday! - 1) ] = datestring
    }
    return cal_seq
}
// 実質プログラムの始まり
if getEnvironmentVar(name:"LANG") == "ja_JP.UTF-8" {
	d_flag.je = 1
}
let arguments: [String] = CommandLine.arguments
if arguments.count != 1 {
    switch arguments[1] {
    case "-y":
        d_flag.raws = 4
        d_flag.cols = 3
        if arguments.count == 3 {
            d_flag.year = Int(arguments[2])!
        }
    case "-3":
        d_flag.cols = 3
        d_flag.pos-=1
    case "+3":
        d_flag.cols = 3
    case "-m":
        if arguments.count == 3 {
            d_flag.month = Int(arguments[2])!
        }
    default:
        break
    }
}
let now: Date = Date()
var cal_comp: DateComponents = getCalComp(date:now)
curr = (cal_comp.year!, cal_comp.month!, cal_comp.day!)
if d_flag.raws == 4 {
    cal_comp.month = 1
    if d_flag.year != 0 {
        cal_comp.year! = d_flag.year
    }
} else if d_flag.raws == 1 && d_flag.cols == 3 {
    cal_comp.month = cal_comp.month! + d_flag.pos
}
if d_flag.month != 0 {
    cal_comp.month = d_flag.month
}
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!)" }
    if d_flag.je == 1 {
        header_m[i] = "\(cal_comp.month!)月"
    } else {
        header_m[i] = "\(month_odr[cal_comp.month! - 1])"
    }
    cal_comp.month!+=1
}
var length: Int
if d_flag.cols == 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..<d_flag.raws {
    if d_flag.cols == 3 {
        for var i in 0..<d_flag.cols {
            length = (21 - header_m[i + 3 * l].characters.count - d_flag.je) / 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 - d_flag.je
            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 - d_flag.je) / 2
        for var j in 0..<length {
            header = " " + header
        }
    }
    print(header)
    header = ""
    for var i in 0..<7 {
        for var j in 0..<d_flag.cols {
            for var k in 0..<7 {
                for l in hol{
                    if l == k {
                        print(esc.fg_mg, terminator:"")
                    }
                }
                print("\(sequence[i * 7 + ((j + 3 * l) * 49 + k)])\(esc.fg_bk) ", terminator:"")
            }
            print(" ", terminator:"")
        }
        print("\n", terminator:"")
    }
}

月表示で変更したのは93から97行目。環境変数の値に応じて処理を変えます。

さらに110から128行目で月表示のセンタリングの際、日本語表示は2バイト分の幅を取るので、日英判定結果を利用して日本語の時だけ文字列長さに+1して長さ調整をしています。