Class: Logbook::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/logbook/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(*memory) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/logbook/cli.rb', line 61

def add(*memory)
  text = memory.join ' '
  j = current_book
  return unless j
  begin
    time = j.add_temporal text
    ok("at #{em(time.to_s)}")
  rescue
    error $!
  end
end

#allObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/logbook/cli.rb', line 74

def all
  j = current_book
  begin
    data = j.all
    say "[#{em data[:cover]}]\n"
    data[:entries].group_by{|g| g[:date]}.each do |key, entries|
      say "#{em key.strftime("%A, %d-%b-%Y")}"
      entries.each do |entry|
        say "#{entry[:content]}"
      end
    end
  rescue
    error $!
  end
end

#book(*params) ⇒ Object



18
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
# File 'lib/logbook/cli.rb', line 18

def book(*params)
  text = params.join ' '
  config[:books] ||= {}

  # no argument given: prompt user to switch books from a menu
  if text.empty? && !config[:books].empty?
    choices = config[:books].to_a
    choices = choices.map.with_index{ |a, i| [i+1, *a]}
    print_table choices
    selection = ask("Pick one:").to_i
    if selection == 0 || choices.size < selection
      error "No such option"
      return
    end

    selected_id = choices[selection-1][1]
    self.current_book = selected_id
    ok "selected"
    return
  end

  # we have an argument, switch to, or create a new one.
  if config[:books].has_key? text
    self.current_book = text
    ok "switched"
  else
    if yes? "Create #{em(text)}?"
      j = Logbook::Book.new
      begin
        id = j.create text
        self.current_book = id

        config[:books][id] = text
        config.save 
        ok "saved '#{text}' as #{em(id)}"
      rescue
        error $!
      end
    end
  end
end

#book?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/logbook/cli.rb', line 11

def book?
  j = current_book
  return unless j
  say config[:books][j.id]
end