xcal 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/xcal +6 -0
  2. data/lib/xcal.rb +136 -0
  3. metadata +63 -0
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'xcal'
5
+
6
+ main()
@@ -0,0 +1,136 @@
1
+ require 'date'
2
+ require 'open-uri'
3
+ require 'icalendar'
4
+
5
+ BLUE = "\e[34m"
6
+ RED = "\e[31m"
7
+ REVERSE = "\e[7m"
8
+ CLEAR = "\e[0m"
9
+
10
+ def red(str)
11
+ RED + str + CLEAR
12
+ end
13
+
14
+ def blue(str)
15
+ BLUE + str + CLEAR
16
+ end
17
+
18
+ def reverse(str)
19
+ REVERSE + str + CLEAR
20
+ end
21
+
22
+ URL = 'https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics'
23
+
24
+ MONTH_TEMPLATE = [
25
+ " January % ",
26
+ " February % ",
27
+ " March % ",
28
+ " April % ",
29
+ " May % ",
30
+ " June % ",
31
+ " July % ",
32
+ " August % ",
33
+ " September % ",
34
+ " October % ",
35
+ " November % ",
36
+ " December % "
37
+ ]
38
+
39
+ MONTH_DAY = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
40
+
41
+ ROWS = 6
42
+ WEEK = 7
43
+ MONTH = 3
44
+ SPACE = 2
45
+ HEADER = [red('Su'), 'Mo', 'Tu', 'We', 'Th', 'Fr', blue('Sa')]
46
+
47
+ def get_japan_holidays()
48
+ holidays = {}
49
+ ical = open(URL){|f| f.read}
50
+ cals = Icalendar.parse(ical)
51
+ cals.each{|cal|
52
+ cal.events.each{|event|
53
+ start = event.dtstart
54
+
55
+ y = start.year
56
+ m = start.month
57
+ d = start.day
58
+
59
+ holidays[y] = {} unless holidays[y]
60
+ holidays[y][m] = {} unless holidays[y][m]
61
+ holidays[y][m][d] = true
62
+ }
63
+ }
64
+
65
+ holidays
66
+ end
67
+
68
+ def fill_calendar(year, month, table, month_offset, holidays, today)
69
+ offset = Date.new(year, month, 1).wday - 1
70
+ month_day = get_month_day(year, month)
71
+ (1..month_day).each{|d|
72
+ date = Date.new(year, month, d)
73
+
74
+ day = sprintf('%2d', d)
75
+ if is_holiday?(year, month, d, holidays)
76
+ day = red(day)
77
+ elsif date.wday == 6
78
+ day = blue(day)
79
+ elsif date.wday == 0
80
+ day = red(day)
81
+ end
82
+
83
+ if today == date
84
+ day = reverse(day)
85
+ end
86
+
87
+ table[(offset + d) / 7][month_offset + date.wday] = day
88
+ }
89
+ end
90
+
91
+ def is_holiday?(y, m, d, holidays)
92
+ holidays[y] and holidays[y][m] and holidays[y][m][d]
93
+ end
94
+
95
+ def month_header(y, m)
96
+ MONTH_TEMPLATE[m - 1].sub('%', sprintf('%4d', y))
97
+ end
98
+
99
+ def get_month_day(y, m)
100
+ day = MONTH_DAY[m - 1]
101
+ if m == 2 and Date.leap?(y)
102
+ day += 1
103
+ end
104
+ day
105
+ end
106
+
107
+ def main()
108
+ japan_holidays = get_japan_holidays()
109
+
110
+ table = Array.new(ROWS){
111
+ Array.new(WEEK * MONTH + SPACE){|i|
112
+ (i + 1) % 8 == 0 ? '' : ' '
113
+ }
114
+ }
115
+
116
+ today = Date.today
117
+ last_month = today << 1
118
+ next_month = today >> 1
119
+
120
+ fill_calendar(last_month.year, last_month.month, table, 0, japan_holidays, today)
121
+ fill_calendar(today.year, today.month, table, 8, japan_holidays, today)
122
+ fill_calendar(next_month.year, next_month.month, table, 16, japan_holidays, today)
123
+
124
+ month_headers = [
125
+ month_header(last_month.year, last_month.month),
126
+ month_header(today.year, today.month),
127
+ month_header(next_month.year, next_month.month)
128
+ ]
129
+ puts month_headers.join(' ')
130
+
131
+ puts HEADER.join(' ') + ' ' + HEADER.join(' ') + ' ' + HEADER.join(' ')
132
+
133
+ table.each{|row|
134
+ puts row.join(' ')
135
+ }
136
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xcal
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - xmisao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: icalendar
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: xcal displays a japan calendar like cal, ncal
31
+ email: mail@xmisao.com
32
+ executables:
33
+ - xcal
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - bin/xcal
38
+ - lib/xcal.rb
39
+ homepage: https://github.com/xmisao/xcal
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.23
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: xcal displays a japan calendar like cal, ncal
63
+ test_files: []