Class: Cali
- Inherits:
-
Object
- Object
- Cali
- Defined in:
- lib/cali.rb
Constant Summary collapse
- KEYBINDINGS =
{ ['q'[0]]=> :quit, [12]=> :refresh, # C-l ['l'[0], Ncurses::KEY_RIGHT, 6]=> :tomorrow, # C-f ['h'[0], Ncurses::KEY_LEFT, 2]=> :yesterday, # C-b ['j'[0], Ncurses::KEY_DOWN, 14]=> :nextweek, # C-n ['k'[0], Ncurses::KEY_UP, 16]=> :prevweek, # C-p ['n'[0], Ncurses::KEY_NPAGE]=> :nextmonth, ['p'[0], Ncurses::KEY_PPAGE]=> :prevmonth, ['}'[0]]=> :nextyear, ['{'[0]]=> :prevyear, ['w'[0]]=> :nextevent, ['b'[0]]=> :prevevent }
Class Method Summary collapse
Instance Method Summary collapse
- #bounce(&block) ⇒ Object
- #displaycal ⇒ Object
- #displaydays ⇒ Object
- #displayevents ⇒ Object
- #first ⇒ Object
- #has_items?(date = @today) ⇒ Boolean
-
#initialize ⇒ Cali
constructor
A new instance of Cali.
- #last ⇒ Object
- #mainloop ⇒ Object
- #move(to) ⇒ Object
- #movecursor(old) ⇒ Object
- #run ⇒ Object
- #update(&block) ⇒ Object
- #weekdays ⇒ Object
Constructor Details
#initialize ⇒ Cali
Returns a new instance of Cali.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cali.rb', line 73 def initialize @today = Date.today @days = {} @dates = {} if Cali.config.dates open(File.(Cali.config.dates)){|f| until f.eof line = f.readline.strip d = line.match(/(\d{4})-(\d{2})-(\d{2})/) if d date = Date.new(*d[1..3].map{|m|m.to_i}) @dates[date] ||= [] @dates[date] << line end end } end @key = {} KEYBINDINGS.each {|k,v| k.each {|kk| @key[kk] = v } } end |
Class Method Details
.config ⇒ Object
68 69 70 71 |
# File 'lib/cali.rb', line 68 def Cali::config @@config ||= OpenStruct.new @@config end |
.run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cali.rb', line 52 def Cali::run dotcali = File. "~/.calirc" load dotcali if File.exist? dotcali dates = nil OptionParser.new { |opts| opts. = "Usage: cali [-d FILE]" opts.on("-d","--dates FILE","Use FILE for events") {|d| Cali.config.dates = d } }.parse! cali=Cali.new cali.run end |
Instance Method Details
#bounce(&block) ⇒ Object
175 176 177 178 179 |
# File 'lib/cali.rb', line 175 def bounce (&block) y,x = Ncurses.getcury(Ncurses.stdscr),Ncurses.getcurx(Ncurses.stdscr) yield Ncurses.move(y,x) end |
#displaycal ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/cali.rb', line 120 def displaycal Ncurses.clear Ncurses.move(0,0) Ncurses.printw(@today.strftime(" %B %Y\n")) Ncurses.printw(weekdays.join(" ")+"\n") displaydays Ncurses.refresh end |
#displaydays ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cali.rb', line 129 def displaydays counter = (first - first.wday) before_month = true after_month = false while not after_month if counter.month == @today.month and before_month before_month = false elsif counter.month != @today.month and not before_month after_month = true end if before_month or after_month Ncurses.printw(" ") Ncurses.printw("\n") if after_month else @days[counter.day] = [Ncurses.getcurx(Ncurses.stdscr), Ncurses.getcury(Ncurses.stdscr)] if counter == @today x,y = Ncurses.getcurx(Ncurses.stdscr),Ncurses.getcury(Ncurses.stdscr) end Ncurses.attron(Ncurses::A_UNDERLINE) if has_items?(counter) Ncurses.attron(Ncurses::A_REVERSE) if counter == @today Ncurses.printw("%2d" % counter.day) Ncurses.attroff(Ncurses::A_REVERSE) if counter == @today Ncurses.attroff(Ncurses::A_UNDERLINE) if has_items?(counter) Ncurses.printw(" ") end if counter.wday == 6 Ncurses.printw("\n") end counter += 1 end Ncurses.move(y,x+1) end |
#displayevents ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cali.rb', line 163 def displayevents displaycal bounce { y = @days[last.day][1] @dates[@today].each {|l| y += 2 Ncurses.move(y,0) Ncurses.printw("#{l}") } if @dates[@today] } end |
#first ⇒ Object
277 278 279 |
# File 'lib/cali.rb', line 277 def first Date.new(@today.year,@today.month,1) end |
#has_items?(date = @today) ⇒ Boolean
290 291 292 |
# File 'lib/cali.rb', line 290 def has_items?(date=@today) @dates.include?(date) end |
#last ⇒ Object
281 282 283 284 285 286 287 288 |
# File 'lib/cali.rb', line 281 def last newlast = @today.dup until newlast.month != @today.month last = newlast.dup newlast += 1 end last end |
#mainloop ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/cali.rb', line 110 def mainloop displaycal displayevents catch :quit do while true move @key[Ncurses.getch] end end end |
#move(to) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/cali.rb', line 208 def move(to) case to when :quit throw :quit when :refresh displaycal when :tomorrow update { @today += 1 } when :yesterday update { @today -= 1 } when :nextweek update { @today += 7 } when :prevweek update { @today -= 7 } when :nextmonth oldtoday = @today.dup @today += 4*7 if @today.month == oldtoday.month @today += 7 end displaycal when :prevmonth oldtoday = @today.dup @today -= 4*7 if @today.month == oldtoday.month @today -= 7 end displaycal when :nextyear oldtoday = @today.dup @today += 52*7 while @today.year == oldtoday.year or @today.month != oldtoday.month @today += 7 end displaycal when :prevyear oldtoday = @today.dup @today -= 52*7 while @today.year == oldtoday.year or @today.month != oldtoday.month @today -= 7 end displaycal when :nextevent update { newtoday = @dates.keys.select{|d| @today < d }.sort.first @today = newtoday if newtoday } when :prevevent update { newtoday = @dates.keys.select{|d| @today > d }.sort.last @today = newtoday if newtoday } end displayevents end |
#movecursor(old) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/cali.rb', line 181 def movecursor(old) if old != @today a = @days[old.day] Ncurses.attron(Ncurses::A_UNDERLINE) if has_items?(old) Ncurses.mvprintw(a[1],a[0],"%2d" % old.day) Ncurses.attroff(Ncurses::A_UNDERLINE) if has_items?(old) end Ncurses.attron(Ncurses::A_UNDERLINE) if has_items?(@today) Ncurses.attron(Ncurses::A_REVERSE) a = @days[@today.day] Ncurses.mvprintw(a[1],a[0],"%2d" % @today.day) Ncurses.attroff(Ncurses::A_REVERSE) Ncurses.attroff(Ncurses::A_UNDERLINE) if has_items?(@today) Ncurses.move(a[1],a[0]+1) end |
#run ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cali.rb', line 98 def run begin Ncurses.initscr Ncurses.cbreak Ncurses.noecho Ncurses.keypad(Ncurses.stdscr,true) mainloop ensure Ncurses.endwin end end |
#update(&block) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/cali.rb', line 198 def update(&block) oldtoday = @today.dup yield if oldtoday.month == @today.month and oldtoday.year == @today.year movecursor(oldtoday) else displaycal end end |
#weekdays ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/cali.rb', line 264 def weekdays wds = [] counter = (@today - @today.wday) n = 0 while n < 7 #TODO: handle other charsets correctly wds << counter.strftime('%a')[0..1] counter += 1 n += 1 end wds end |