色付きのcal
30分プログラム、その254。色付きのcalコマンド。
calendarモジュールを使ってみたかったので。
これを使うとcalコマンドが簡単に作れる。
>>> import calendar >>> print calendar.month(2008,2) February 2008 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
で、単純にcalを作るだけだと簡単すぎるので、色を付けてみた。色の付けかたはid:mzp:20070512:color(色付きgrep)あたりを参考に。
使い方
$ python cal.py February 2008 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
ソースコード
#! /usr/bin/python # -*- mode:python; coding:utf-8 -*- # # cal.py - # # Copyright(C) 2008 by mzp # Author: MIZUNO Hiroki / mzpppp at gmail dot com # http://howdyworld.org # # Timestamp: 2008/02/25 11:09:04 # # This program is free software; you can redistribute it and/or # modify it under MIT Lincence. # import sys import calendar import time Normal=0 Black = 30 Red = 31 Green = 32 Brown = 33 Blue = 34 Purple = 35 Cyan = 36 LightGray = 37 def printc(msg,fg=Normal,bg=Normal): write(sys.stdout,str(msg)+"\n",fg,bg) def write(io,msg,fg=Normal,bg=Normal): io.write("\x1b[%sm\x1b[%sm%s" % (fg,bg+10,msg)) def format(n): return '% 2s ' % ('' if n == 0 else n) def cal(): t = time.localtime() cal = calendar.monthcalendar(t.tm_year,t.tm_mon) printc(calendar.month(t.tm_year,t.tm_mon).split("\n")[0],Green) for week in cal: wd = week[0:-2] sat = week[-2] sun = week[-1] for day in wd: write(sys.stdout,format(day)) write(sys.stdout,format(sat),Cyan) write(sys.stdout,format(sun),Red) print '' print '' if __name__ == '__main__': cal()
参考
- 過去の30分プログラム
- http://www.python.jp/doc/current/lib/module-calendar.html
- id:mzp:20070512:color - 色付きgrep