Class: Marginalia::IO::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/marginalia-io/cli.rb

Instance Method Summary collapse

Instance Method Details

#append(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/marginalia-io/cli.rb', line 81

def append(*args)
  id = args.shift
  raise Thor::Error.new "Expected an id" unless id

  body = args.join(" ")
  if body.strip == ""
    temp = Tempfile.new(['journal', '.md'])
    system(ENV['EDITOR'], temp.path)
    body = temp.read
  end

  if body.strip == ""
    exit 0
  end

  api.append(id, body)
end

#create(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/marginalia-io/cli.rb', line 100

def create(*args)
  title = args.join(" ")
  temp = Tempfile.new(['journal', '.md'])
  system(ENV['EDITOR'], temp.path)
  body = temp.read
  if body.strip == ""
    exit 0
  end

  resp = api.create(title, body).parsed_response
  puts "Created note #{resp['id']}"

end

#edit(id) ⇒ Object



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
# File 'lib/marginalia-io/cli.rb', line 54

def edit(id)
  note = api.get(id)
  if note.nil?
    raise Thor::Error.new "Unknown note id #{id}"
  end
  body = """title:   #{note['title']}
id:      #{note['id']}
created: #{note['created_at']}
updated: #{note['updated_at']}

#{note['body'].delete("\C-M")}
"""
  temp = Tempfile.new(["note", ".md"])
  temp.write body
  temp.flush

  system("#{ENV['EDITOR']} #{temp.path}")

  temp.rewind
  contents = temp.read

  headers, body = contents.split(/\n\n/, 2)

  api.update(id, body)
end

#listObject



23
24
25
26
27
# File 'lib/marginalia-io/cli.rb', line 23

def list
  api.all.sort_by{ |n| n['updated_at'] }.reverse.each do |note|
    puts "#{note['id'].to_s.rjust(4)} #{note['updated_at'][0,10]} #{note['title']}"
  end
end

#loginObject



10
11
12
13
14
# File 'lib/marginalia-io/cli.rb', line 10

def 
  auth = Marginalia::IO::Auth.new(options[:host])
  auth.delete_credentials
  auth.
end

#logoutObject



17
18
19
20
# File 'lib/marginalia-io/cli.rb', line 17

def logout
  auth = Marginalia::IO::Auth.new(options[:host])
  auth.delete_credentials
end

#search(query) ⇒ Object



30
31
32
33
34
# File 'lib/marginalia-io/cli.rb', line 30

def search(query)
  api.search(query).sort_by{|n|n['updated_at']}.reverse.each do |note|
    puts "#{note['id'].to_s.rjust(4)} #{note['updated_at'][0,10]} #{note['title']}"
  end
end

#show(id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/marginalia-io/cli.rb', line 37

def show(id)
  note = api.get(id)
  body = """title:   #{note['title']}
id:      #{note['id']}
created: #{note['created_at']}
updated: #{note['updated_at']}

#{note['body'].delete("\C-M")}
"""

  temp = Tempfile.new(["notes", ".md"])
  temp.write body
  temp.flush
  system(ENV['PAGER'], temp.path)
end