Class: CLI

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

Instance Method Summary collapse

Instance Method Details

#add(*str) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jott/cli.rb', line 18

def add(*str)
  if str.empty? # open text editor
    tempfile = Tempfile.new
    system(editor, tempfile.path)
    text = File.read(tempfile.path)
    tempfile.unlink
  else # add memo from command line
    text = str.join(' ')
  end

  if text.chomp == ''
    puts 'Please enter text.'.colorize(:red)
    exit
  end

  title = text[0, 30]
  Memo.new.create(title:, body: text)
  puts "Added new memo: #{title}".colorize(:green)
end

#clearObject



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

def clear
  Memo.new.clear
  puts 'Clear all memos'.colorize(:green)
end

#configObject



94
95
96
# File 'lib/jott/cli.rb', line 94

def config
  puts "editor: #{editor}"
end

#edit(*args) ⇒ Object



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

def edit(*args)
  id = args.first
  id = Memo.new.last[0][0] if id.nil?
  str = args[1..]
  memo = Memo.new.find(id)

  if str.nil? || str.empty? # open text editor
    tempfile = Tempfile.new
    File.open(tempfile.path, 'w') do |f|
      f.puts memo[0][2]
    end
    system(editor, tempfile.path)
    text = File.read(tempfile.path)
    tempfile.unlink
  else # add memo from command line
    text = str.join(' ')
  end

  if text.chomp == ''
    puts 'Please enter text.'.colorize(:red)
    exit
  end

  title = text[0, 30]
  Memo.new.update(id:, title:, body: text)
  puts "Edited the memo: #{id}".colorize(:green)
end

#lsObject



74
75
76
77
78
79
# File 'lib/jott/cli.rb', line 74

def ls
  memos = Memo.new.all
  memos.each do |memo|
    puts "#{memo[0]}. #{memo[1]}"
  end
end

#rm(id) ⇒ Object



39
40
41
42
# File 'lib/jott/cli.rb', line 39

def rm(id)
  Memo.new.delete(id:)
  puts "Deleted the memo: #{id}".colorize(:green)
end

#set_editor(editor) ⇒ Object



88
89
90
91
# File 'lib/jott/cli.rb', line 88

def set_editor(editor)
  Config.new.set_editor(editor)
  puts "Set editor: #{editor}".colorize(:green)
end

#show(id) ⇒ Object



82
83
84
85
# File 'lib/jott/cli.rb', line 82

def show(id)
  memo = Memo.new.find(id)
  puts(memo[0][2])
end

#versionObject



99
100
101
# File 'lib/jott/cli.rb', line 99

def version
  puts "jott version #{Jott::VERSION}"
end