Class: WWID
- Inherits:
-
Object
- Object
- WWID
- Defined in:
- lib/doing/wwid.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#content ⇒ Object
Returns the value of attribute content.
-
#current_section ⇒ Object
Returns the value of attribute current_section.
-
#doing_file ⇒ Object
Returns the value of attribute doing_file.
-
#sections ⇒ Object
Returns the value of attribute sections.
Instance Method Summary collapse
- #add_item(title, section = nil, opt = {}) ⇒ Object
- #add_section(title) ⇒ Object
- #all(order = "") ⇒ Object
- #archive(section = nil, count = 10) ⇒ Object
- #choose_section ⇒ Object
- #colors ⇒ Object
- #create(filename = nil) ⇒ Object
- #get_view(title) ⇒ Object
-
#initialize(input = nil) ⇒ WWID
constructor
A new instance of WWID.
- #last ⇒ Object
- #list_section(opt = {}) ⇒ Object
- #read_config ⇒ Object
- #recent(count = 10, section = nil) ⇒ Object
- #today ⇒ Object
- #write(file = nil) ⇒ Object
Constructor Details
#initialize(input = nil) ⇒ WWID
Returns a new instance of WWID.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/doing/wwid.rb', line 19 def initialize(input=nil) @content = {} @config = read_config @config['doing_file'] ||= "~/what_was_i_doing.md" @config['current_section'] ||= 'Currently' @config['templates'] ||= {} @config['templates']['default'] ||= { 'date_format' => '%Y-%m-%d %H:%M', 'template' => '%date | %title%note', 'wrap_width' => 0 } @config['templates']['today'] ||= { 'date_format' => '%_I:%M%P', 'template' => '%date: %title%note', 'wrap_width' => 0 } @config['templates']['last'] ||= { 'date_format' => '%-I:%M%P on %a', 'template' => '%title (at %date)%odnote', 'wrap_width' => 88 } @config['templates']['recent'] ||= { 'date_format' => '%_I:%M%P', 'template' => '%shortdate: %title', 'wrap_width' => 88 } @config['views'] ||= { 'sample' => { 'date_format' => '%_I:%M%P', 'template' => '%date | %title%note', 'wrap_width' => 0, 'section' => 'section_name', 'count' => 5 }, 'color' => { 'date_format' => '%F %_I:%M%P', 'template' => '%boldblack%date %boldgreen| %boldwhite%title%default%note', 'wrap_width' => 0, 'section' => 'Currently', 'count' => 10, } } @doing_file = File.(config['doing_file']) @current_section = config['current_section'] @default_template = config['templates']['default']['template'] @default_date_format = config['templates']['default']['date_format'] @config[:include_notes] ||= true File.open(File.(DOING_CONFIG), 'w') { |yf| YAML::dump(config, yf) } if input.nil? create(@doing_file) unless File.exists?(@doing_file) input = IO.read(@doing_file) input = input.force_encoding('utf-8') if input.respond_to? :force_encoding elsif File.exists?(File.(input)) && File.file?(File.(input)) && File.stat(File.(input)).size > 0 input = IO.read(File.(input)) input = input.force_encoding('utf-8') if input.respond_to? :force_encoding @doing_file = File.(input) elsif input.length < 256 create(input) end @other_content_top = [] @other_content_bottom = [] section = "Uncategorized" lines = input.split(/\n/) current = 0 lines.each {|line| next if line =~ /^\s*$/ if line =~ /^(\w[\w ]+):\s*(@\S+\s*)*$/ section = $1 @content[section] = {} @content[section]['original'] = line @content[section]['items'] = [] current = 0 elsif line =~ /^\s*- (\d{4}-\d\d-\d\d \d\d:\d\d) \| (.*)/ date = Time.parse($1) title = $2 @content[section]['items'].push({'title' => title, 'date' => date}) current += 1 else # if content[section]['items'].length - 1 == current if current == 0 @other_content_top.push(line) else if line =~ /^\S/ @other_content_bottom.push(line) else unless @content[section]['items'][current - 1].has_key? 'note' @content[section]['items'][current - 1]['note'] = [] end @content[section]['items'][current - 1]['note'].push(line) end end # end end } end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
16 17 18 |
# File 'lib/doing/wwid.rb', line 16 def config @config end |
#content ⇒ Object
Returns the value of attribute content.
16 17 18 |
# File 'lib/doing/wwid.rb', line 16 def content @content end |
#current_section ⇒ Object
Returns the value of attribute current_section.
16 17 18 |
# File 'lib/doing/wwid.rb', line 16 def current_section @current_section end |
#doing_file ⇒ Object
Returns the value of attribute doing_file.
16 17 18 |
# File 'lib/doing/wwid.rb', line 16 def doing_file @doing_file end |
#sections ⇒ Object
Returns the value of attribute sections.
16 17 18 |
# File 'lib/doing/wwid.rb', line 16 def sections @sections end |
Instance Method Details
#add_item(title, section = nil, opt = {}) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/doing/wwid.rb', line 150 def add_item(title,section=nil,opt={}) section ||= @current_section add_section(section) unless @content.has_key?(section) opt[:date] ||= Time.now opt[:note] ||= "" entry = {'title' => title.strip.cap_first, 'date' => opt[:date]} unless opt[:note] =~ /^\s*$/s entry['note'] = opt[:note] end @content[section]['items'].push(entry) end |
#add_section(title) ⇒ Object
146 147 148 |
# File 'lib/doing/wwid.rb', line 146 def add_section(title) @content[title.cap_first] = {'original' => "#{title}:", 'items' => []} end |
#all(order = "") ⇒ Object
351 352 353 354 355 |
# File 'lib/doing/wwid.rb', line 351 def all(order="") order = "asc" if order == "" cfg = @config['templates']['default_template'] list_section({:section => @current_section, :wrap_width => cfg['wrap_width'], :count => 0, :format => cfg['date_format'], :template => cfg['template'], :order => order}) end |
#archive(section = nil, count = 10) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/doing/wwid.rb', line 298 def archive(section=nil,count=10) section = choose_section if section.nil? || section =~ /choose/i if sections.include?(section) items = @content[section]['items'] return if items.length < 10 @content[section]['items'] = items[0..5] add_section('Archive') unless sections.include?('Archive') @content['Archive']['items'] += items[count..-1] write(@doing_file) end end |
#choose_section ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/doing/wwid.rb', line 185 def choose_section sections.each_with_index {|section, i| puts "% 3d: %s" % [i+1, section] } print "> " num = STDIN.gets return false if num =~ /^[a-z ]*$/i return sections[num.to_i - 1] end |
#colors ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/doing/wwid.rb', line 312 def colors color = {} color['black'] = "\033[30m" color['red'] = "\033[31m" color['green'] = "\033[32m" color['yellow'] = "\033[33m" color['blue'] = "\033[34m" color['magenta'] = "\033[35m" color['cyan'] = "\033[36m" color['white'] = "\033[37m" color['bgblack'] = "\033[40m" color['bgred'] = "\033[41m" color['bggreen'] = "\033[42m" color['bgyellow'] = "\033[43m" color['bgblue'] = "\033[44m" color['bgmagenta'] = "\033[45m" color['bgcyan'] = "\033[46m" color['bgwhite'] = "\033[47m" color['boldblack'] = "\033[1;30m" color['boldred'] = "\033[1;31m" color['boldgreen'] = "\033[1;32m" color['boldyellow'] = "\033[1;33m" color['boldblue'] = "\033[1;34m" color['boldmagenta'] = "\033[1;35m" color['boldcyan'] = "\033[1;36m" color['boldwhite'] = "\033[1;37m" color['boldbgblack'] = "\033[1;40m" color['boldbgred'] = "\033[1;41m" color['boldbggreen'] = "\033[1;42m" color['boldbgyellow'] = "\033[1;43m" color['boldbgblue'] = "\033[1;44m" color['boldbgmagenta'] = "\033[1;45m" color['boldbgcyan'] = "\033[1;46m" color['boldbgwhite'] = "\033[1;47m" color['default']="\033[0;39m" color end |
#create(filename = nil) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/doing/wwid.rb', line 125 def create(filename=nil) filename = @doing_file unless File.exists?(filename) && File.stat(filename).size > 0 File.open(filename,'w+') do |f| f.puts @current_section + ":" end end end |
#get_view(title) ⇒ Object
195 196 197 198 199 200 |
# File 'lib/doing/wwid.rb', line 195 def get_view(title) if @config['views'].has_key?(title) return @config['views'][title] end false end |
#last ⇒ Object
368 369 370 371 |
# File 'lib/doing/wwid.rb', line 368 def last cfg = @config['templates']['last'] list_section({:section => @current_section, :wrap_width => cfg['wrap_width'], :count => 1, :format => cfg['date_format'], :template => cfg['template']}) end |
#list_section(opt = {}) ⇒ Object
202 203 204 205 206 207 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/doing/wwid.rb', line 202 def list_section(opt={}) opt[:count] ||= 0 count = opt[:count] - 1 opt[:section] ||= nil opt[:format] ||= @default_date_format opt[:template] ||= @default_template opt[:order] ||= "desc" opt[:today] ||= false if opt[:section].nil? opt[:section] = @content[choose_section] elsif opt[:section].class == String if @content.has_key? opt[:section] opt[:section] = @content[opt[:section]] else $stderr.puts "Section '#{opt[:section]}' not found" return end end if opt[:section].class != Hash $stderr.puts "Invalid section object" return end items = opt[:section]['items'].sort_by{|item| item['date'] } if opt[:today] items.delete_if {|item| item['date'] < Date.today.to_time }.reverse! else items = items.reverse[0..count] end items.reverse! if opt[:order] =~ /^asc/i out = "" items.each {|item| if (item.has_key?('note') && !item['note'].empty?) && @config[:include_notes] note_lines = item['note'].delete_if{|line| line =~ /^\s*$/ }.map{|line| "\t\t" + line.sub(/^\t\t/,'') } if opt[:wrap_width] && opt[:wrap_width] > 0 width = opt[:wrap_width] note_lines.map! {|line| line.strip.gsub(/(.{1,#{width}})(\s+|\Z)/, "\t\\1\n") } end note = "\n#{note_lines.join("\n").chomp}" else note = "" end output = opt[:template].dup output.gsub!(/%[a-z]+/) do |m| if colors.has_key?(m.sub(/^%/,'')) colors[m.sub(/^%/,'')] else m end end output.sub!(/%date/,item['date'].strftime(opt[:format])) output.sub!(/%shortdate/) { if item['date'] > Date.today.to_time item['date'].strftime('%_I:%M%P') elsif item['date'] > (Date.today - 7).to_time item['date'].strftime('%a %-I:%M%P') elsif item['date'].year == Date.today.year item['date'].strftime('%b %d, %-I:%M%P') else item['date'].strftime('%b %d %Y, %-I:%M%P') end } output.sub!(/%title/) {|m| if opt[:wrap_width] && opt[:wrap_width] > 0 item['title'].gsub(/(.{1,#{opt[:wrap_width]}})(\s+|\Z)/, "\\1\n\t ").strip else item['title'].strip end } output.sub!(/%note/,note) output.sub!(/%odnote/,note.gsub(/\t\t/,"\t")) output.gsub!(/%hr(_under)?/) do |m| o = "" `tput cols`.to_i.times do o += $1.nil? ? "-" : "_" end o end out += output + "\n" } return out end |
#read_config ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/doing/wwid.rb', line 134 def read_config if File.exists? File.(DOING_CONFIG) return YAML.load_file(File.(DOING_CONFIG)) else return {} end end |
#recent(count = 10, section = nil) ⇒ Object
362 363 364 365 366 |
# File 'lib/doing/wwid.rb', line 362 def recent(count=10,section=nil) cfg = @config['templates']['recent'] section ||= @current_section list_section({:section => section, :wrap_width => cfg['wrap_width'], :count => count, :format => cfg['date_format'], :template => cfg['template'], :order => "asc"}) end |
#today ⇒ Object
357 358 359 360 |
# File 'lib/doing/wwid.rb', line 357 def today cfg = @config['templates']['today'] list_section({:section => @current_section, :wrap_width => cfg['wrap_width'], :count => 0, :format => cfg['date_format'], :template => cfg['template'], :order => "asc", :today => true}) end |
#write(file = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/doing/wwid.rb', line 163 def write(file=nil) if @other_content_top.empty? output = "" else output = @other_content_top.join("\n") + "\n" end @content.each {|title, section| output += section['original'] + "\n" output += list_section({:section => title, :template => "\t- %date | %title%note"}) } output += @other_content_bottom.join("\n") if file.nil? $stdout.puts output else if File.exists?(File.(file)) File.open(File.(file),'w+') do |f| f.puts output end end end end |