Class: CLI

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

Instance Method Summary collapse

Instance Method Details

#add(*str) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jott/cli.rb', line 17

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
  title = text[0, 30]
  Memo.new.create(title:, body: text)
  puts "Added new memo: #{title}".colorize(:green)
end

#clearObject



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

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

#configObject



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

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

#edit(*args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jott/cli.rb', line 38

def edit(*args)
  id = args.first
  id = Memo.new.last[0][0] if id.nil?
  str = args[1..-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)
    data = File.read(tempfile.path)
    Memo.new.update(id: id, title: data[0, 30], body: data)
    tempfile.unlink
    puts "Edited the memo: #{id}".colorize(:green)
  else # add memo from command line
    text = str.join(" ")
    title = text[0, 30]
    Memo.new.update(id: id, title: title, body: text)
    puts "Edited the memo: #{id}".colorize(:green)
  end
end

#lsObject



63
64
65
66
67
68
# File 'lib/jott/cli.rb', line 63

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

#rm(id) ⇒ Object



32
33
34
35
# File 'lib/jott/cli.rb', line 32

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

#set_editor(editor) ⇒ Object



77
78
79
80
# File 'lib/jott/cli.rb', line 77

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

#show(id) ⇒ Object



71
72
73
74
# File 'lib/jott/cli.rb', line 71

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

#versionObject



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

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