Class: JotaCli

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

Instance Method Summary collapse

Instance Method Details

#do_closeObject

}}}1



46
47
48
49
50
51
52
# File 'lib/cli.rb', line 46

def do_close
#{{{1
  return if @data.nil?

  @data.close
  @data = nil
end

#do_create(args) ⇒ Object

}}}1



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cli.rb', line 54

def do_create(args)
#{{{1
  filename = args.to_s
  @data.close if @data
  begin
    @data = ClipArray.create(filename)
  rescue AppError => msg
    puts msg
    @data = nil
  end
end

#do_default(args) ⇒ Object

}}}1



180
181
182
183
184
185
186
187
188
189
# File 'lib/cli.rb', line 180

def do_default(args)
#{{{1
  if args.nil? or args == "" then
    puts "Default command is '#{@default_command}'"
  elsif args =~ /^(show|edit|page)$/ then
    @default_command = args
  else
    puts "Illegal default command. Valid are show, edit, and page"
  end
end

#do_delete(args) ⇒ Object

}}}1



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cli.rb', line 124

def do_delete(args)
#{{{1
  index = get_index(args)
  return if index.nil?

  @data.current_index = index

  if pref("deletesave_enable") and not @data.current.empty? then
    deletefile = expand_filename(pref("deletesave_file"),
      @data.dirname, @data.filename)
  end

  begin
    @data.current.append_as_mbox(deletefile)
  rescue SystemCallError => msg
    puts msg
  end

  @data.delete
end

#do_edit(args) ⇒ Object

}}}1



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cli.rb', line 78

def do_edit(args)
#{{{1
  index = get_index(args)
  return if index.nil?

  file = Tempfile.new("jotacli")
  filename = file.path
  print_verbose "creating temporary file '#{filename}'"

  file.puts @data[index].title
  file.puts @data[index].data
  file.close

  if ENV["EDITOR"] then
    cmdline = ENV["EDITOR"]
  else
    cmdline = "/usr/bin/vi"
  end
  cmdline = "%s %s" % [cmdline, filename]
  print_verbose "executing '#{cmdline}'"

  if system(cmdline) then
    file = File.open(filename,"r")
    lines = file.readlines
    @data[index].title = lines[0].chomp
    @data[index].data = lines[1..lines.length-1].join("")
    file.close
  else
    puts "Error executing '#{cmdline}' with exit code #{$?}"
  end

  File.unlink(filename)

  # because of this save we do not not need autosave for the cli version
  @data.save
end

#do_listObject

}}}1



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cli.rb', line 66

def do_list
#{{{1
  return if @data.nil?

  @data.each_index do | i |
    print "%2d %s %s\n" % [
      i+1, 
      type_to_char(@data[i].type),
      @data[i].title]
  end
end

#do_newObject

}}}1



145
146
147
148
149
150
# File 'lib/cli.rb', line 145

def do_new
#{{{1
  return if @data.nil?

  @data.new
end

#do_open(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cli.rb', line 34

def do_open(args)
#{{{1
  filename = args.to_s
  @data.close if @data
  begin
    @data = ClipArray.open(filename)
  rescue SystemCallError, AppError => msg
    puts msg
    @data = nil
  end
end

#do_page(args) ⇒ Object

}}}1



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cli.rb', line 152

def do_page(args)
#{{{1
  index = get_index(args)
  return if index.nil?

  file = Tempfile.new("jotacli")
  filename = file.path
  print_verbose "creating temporary file '#{filename}'"

  file.puts @data[index].title
  file.puts @data[index].data
  file.close

  if ENV["PAGER"] then
    cmdline = ENV["PAGER"]
  else
    cmdline = "/bin/more"
  end
  cmdline = "%s %s" % [cmdline, filename]
  print_verbose "executing '#{cmdline}'"

  if not system(cmdline) then
    puts "Error executing '#{cmdline}' with exit code #{$?}"
  end

  File.unlink(filename)
end

#do_show(args) ⇒ Object

}}}1



115
116
117
118
119
120
121
122
# File 'lib/cli.rb', line 115

def do_show(args)
#{{{1
  index = get_index(args)
  return if index.nil?

  puts @data[index].title
  puts @data[index].data
end