Class: CLI
- Inherits:
-
Thor
- Object
- Thor
- CLI
- Defined in:
- lib/jott/cli.rb
Instance Method Summary collapse
- #add(*str) ⇒ Object
- #clear ⇒ Object
- #config ⇒ Object
- #edit(*args) ⇒ Object
- #ls ⇒ Object
- #rm(id) ⇒ Object
- #set_editor(editor) ⇒ Object
- #show(id) ⇒ Object
- #version ⇒ Object
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 |
#clear ⇒ Object
12 13 14 15 |
# File 'lib/jott/cli.rb', line 12 def clear Memo.new.clear puts 'Clear all memos'.colorize(:green) end |
#config ⇒ Object
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 |
#ls ⇒ Object
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 |