Class: Toodledo::CommandLine::Client

Inherits:
Object
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/toodledo/command_line/client.rb

Overview

The toodledo client. This provides a command line based client to the user and gives a good overview of the capabilities of the API as well.

Author

Will Sargent ([email protected])

Copyright

Copyright © 2008 Will Sargent

License

GLPL v3

Constant Summary collapse

HOME =
ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
TOODLEDO_D =
File::join(HOME, ".toodledo")
CONFIG_F =
File::join(TOODLEDO_D, "user-config.yml")
CONFIG =

We must use __FILE__ instead of DATA because this is now a library and DATA is relative to $0, not __FILE__.

File.read(__FILE__).split(/__END__/).last.gsub(/#\{(.*)\}/) { eval $1 }

Constants included from ParserHelper

ParserHelper::CONTEXT_REGEXP, ParserHelper::FOLDER_REGEXP, ParserHelper::GOAL_REGEXP, ParserHelper::LEVEL_REGEXP, ParserHelper::PRIORITY_REGEXP, ParserHelper::REGEXP_LIST

Instance Method Summary collapse

Methods included from ParserHelper

#parse_context, #parse_folder, #parse_goal, #parse_level, #parse_priority, #parse_remainder, #strip_brackets

Constructor Details

#initialize(userconfig = CONFIG_F, opts = {}) ⇒ Client

Creates the client object.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/toodledo/command_line/client.rb', line 64

def initialize(userconfig=CONFIG_F, opts={})
  @filters = {}
  @debug = false
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::FATAL
  
  @userconfig = test(?e, userconfig) ? IO::read(userconfig) : CONFIG
  @userconfig = YAML.load(@userconfig).merge(opts)
  @formatters = { 
    :task => TaskFormatter.new,
    :goal => GoalFormatter.new,
    :context => ContextFormatter.new,
    :folder => FolderFormatter.new
  }
end

Instance Method Details

#add_context(session, input) ⇒ Object

Adds context.



425
426
427
428
429
430
431
432
# File 'lib/toodledo/command_line/client.rb', line 425

def add_context(session, input)
  
  title = input.strip
  
  context_id = session.add_context(title)
  
  print "Context #{context_id} added."
end

#add_folder(session, input) ⇒ Object



455
456
457
458
459
460
461
462
# File 'lib/toodledo/command_line/client.rb', line 455

def add_folder(session, input)
  
  title = input.strip
  
  folder_id = session.add_folder(title)
  
  print "Folder #{folder_id} added."
end

#add_goal(session, input) ⇒ Object

Adds goal.



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/toodledo/command_line/client.rb', line 437

def add_goal(session, input)
  input.strip!
  
  # Assume that a goal is short, medium or life, and
  # don't stick a symbol on it.
  level = parse_level(input)
  if (level == nil)
    level = Toodledo::Goal::SHORT_LEVEL
  else
    input = clean(LEVEL_REGEXP, input)
    input.strip!
  end
  
  goal_id = session.add_goal(input, level)
  
  print "Goal #{goal_id} added."
end

#add_task(session, line) ⇒ Object

Adds a single task, using toodledo symbols. This is the most general way to add a task right now. If you have symbols which have spaces, then you must encase them in square brackets.

The order of symbols does not matter, but the title must be the last thing on the line.

add @[Deep Space] !top *Action ^[For Great Justice] Take off every Zig



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/toodledo/command_line/client.rb', line 388

def add_task(session, line)        
  context = parse_context(line)
  folder = parse_folder(line)
  goal = parse_goal(line)
  priority = parse_priority(line)
  title = parse_remainder(line)
  
  params = {}
  if (priority != nil)
    params.merge!({ :priority => priority })
  end
  
  if (folder != nil)
    params.merge!({ :folder => folder })
  end
  
  if (context != nil)
    params.merge!({ :context => context })
  end
  
  if (goal != nil)
    params.merge!({ :goal => goal })
  end
  
  # If we got nothing but 'add' then ask for it explicitly.
  if (title == nil)
    title = ask("Task name: ") { |q| q.readline = true }
  end
  
  task_id = session.add_task(title, params)
  
  print "Task #{task_id} added."
end

#archive_folder(session, line) ⇒ Object

Archives a folder.



467
468
469
470
471
472
473
474
475
476
# File 'lib/toodledo/command_line/client.rb', line 467

def archive_folder(session, line)
  
  line.strip!
  
  folder_id = line
  params = { :archived => 1 }
  session.edit_folder(folder_id, params)
  
  print "Folder #{folder_id} archived."
end

#archive_goal(session, line) ⇒ Object



478
479
480
# File 'lib/toodledo/command_line/client.rb', line 478

def archive_goal(session, line)
  # Not implemented!  No way to edit a goal.
end

#clean(regexp, input) ⇒ Object



658
659
660
# File 'lib/toodledo/command_line/client.rb', line 658

def clean(regexp, input)
  return input.sub(regexp, '')
end

#complete_task(session, line) ⇒ Object

Masks the task as completed. Uses a task id as argument.

complete 123



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/toodledo/command_line/client.rb', line 531

def complete_task(session, line)        
  task_id = line
  
  if (task_id == nil)
    task_id = ask("Task ID?: ") { |q| q.readline = true }  
  end
  
  task_id.strip!
          
  params = { :completed => 1 }
  if (session.edit_task(task_id, params))
    print "Task #{task_id} completed."
  else
    print "Task #{task_id} could not be completed!"      
  end
end

#debug=(is_debug) ⇒ Object

Sets the debugging on or off.



90
91
92
93
94
95
96
97
# File 'lib/toodledo/command_line/client.rb', line 90

def debug=(is_debug)
  @debug = is_debug
  if (@debug == true)
    @logger.level = Logger::DEBUG
  else
    @logger.level = Logger::FATAL
  end
end

#debug?Boolean

Returns debugging status.

Returns:

  • (Boolean)


83
84
85
# File 'lib/toodledo/command_line/client.rb', line 83

def debug?
  return @debug
end

#delete_context(session, line) ⇒ Object

Deletes context.



573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/toodledo/command_line/client.rb', line 573

def delete_context(session, line)
  logger.debug("delete_context #{line.inspect}")
  
  id = line
  
  id.strip!
  
  if (session.delete_context(id))
    print "Context #{id} deleted."
  else
    print "Context #{id} could not be deleted!"      
  end
end

#delete_folder(session, line) ⇒ Object

Deletes folder



605
606
607
608
609
610
611
612
613
614
615
# File 'lib/toodledo/command_line/client.rb', line 605

def delete_folder(session, line)
  id = line
  
  id.strip!
  
  if (session.delete_folder(id))
    print "Folder #{id} deleted."
  else
    print "Folder #{id} could not be deleted!"      
  end
end

#delete_goal(session, line) ⇒ Object

Deletes goal.



590
591
592
593
594
595
596
597
598
599
600
# File 'lib/toodledo/command_line/client.rb', line 590

def delete_goal(session, line)
  id = line
  
  id.strip!
  
  if (session.delete_goal(id))
    print "Goal #{id} deleted."
  else
    print "Goal #{id} could not be deleted!"      
  end
end

#delete_task(session, line) ⇒ Object

Deletes a task, using the task id.

delete 123



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/toodledo/command_line/client.rb', line 553

def delete_task(session, line)     
  logger.debug("delete_task: #{line.inspect}")
  task_id = line
  
  if (task_id == nil)
    task_id = ask("Task ID?: ") { |q| q.readline = true }
  end
  
  task_id.strip!
  
  if (session.delete_task(task_id))
    print "Task #{task_id} deleted."
  else
    print "Task #{task_id} could not be deleted!"      
  end
end

#edit_task(session, input) ⇒ Object

Edits a single task. This method allows you to change the symbols on a task. Note that you must specify the ID here.

edit *Action !top 12345



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/toodledo/command_line/client.rb', line 487

def edit_task(session, input)  
  logger.debug("edit_task: #{input.inspect}")
  
  context = parse_context(input)
  folder = parse_folder(input)
  goal = parse_goal(input)
  priority = parse_priority(input)
  task_id = parse_remainder(input)
  
  logger.debug("edit_task: task_id = #{task_id}")
  
  if (task_id == nil)
    task_id = ask("Task ID?: ") { |q| q.readline = true }
  end
  
  task_id.strip!
  
  params = {  }
  
  if (folder != nil)
    params.merge!({ :folder => folder })
  end
  
  if (context != nil)
    params.merge!({ :context => context })
  end
  
  if (goal != nil)
    params.merge!({ :goal => goal })
  end
  
  if (priority != nil)
    params.merge!({ :priority => priority })
  end
  
  session.edit_task(task_id, params)
  
  print "Task #{task_id} edited."
end

#execute_command(session, input) ⇒ Object



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/toodledo/command_line/client.rb', line 662

def execute_command(session, input)    
  case input
    when /^help/, /^\s*\?/
    help()
    
    when /^add/
    line = clean(/^add/, input)
    line.strip!
    case line
    when /folder/
      add_folder(session, clean(/folder/, line))
    when /context/
      add_context(session, clean(/context/, line))
    when /goal/
      add_goal(session, clean(/goal/, line))
    else
      add_task(session, line)
    end
    
    when /^edit/
    line = clean(/^edit/, input)
    edit_task(session, line)
    
    when /^delete/
    line = clean(/^delete/, input)
    line.strip!
    case line
    when /folder/
      delete_folder(session, clean(/folder/, line))
    when /context/
      delete_context(session, clean(/context/, line))
    when /goal/
      delete_goal(session, clean(/goal/, line))
    else
      delete_task(session, line)            
    end
    
    when /^archive/
    archive_folder(session, clean(/^archive/, input))
    
    when /^hotlist/
    line = clean(/^hotlist/, input)
    hotlist(session, line)
    
    when /^complete/
    line = clean(/^complete/, input)
    complete_task(session, line)
    
    when /^tasks/
    line = clean(/^(tasks)/, input)
    list_tasks(session, line)
    
    when /^folders/
    line = clean(/^folders/, input)
    list_folders(session,line)
    
    when /^goals/
    line = clean(/^goals/, input)
    list_goals(session,line)
    
    when /^contexts/
    line = clean(/^contexts/, input)
    list_contexts(session,line)
    
    when /^filters/
    list_filters()
    
    when /^filter/
    line = clean(/^filter/, input)
    set_filter(session, line)
    
    when /^config/
    show_config(session)
    
    when /^unfilter/
    unfilter()
    
    when /debug/
    self.debug = ! self.debug?
    
    when /^quit/, /^exit/
    exit 0
  else
    print "'#{input}' is not a command: type help for a list"
  end
end

#helpObject

Displays the help message.



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/toodledo/command_line/client.rb', line 631

def help()
  print "hotlist      Shows the hotlist"
  print "folders      Shows all folders"
  print "goals        Shows all goals"
  print "contexts     Shows all contexts"
  print "tasks        Shows tasks ('tasks *Action @Home')"
  print 
  print "add          Adds task ('add *Action @Home Eat breakfast')"
  print "  folder     Adds a folder ('add folder MyFolder')"
  print "  context    Adds a context ('add context MyContext')"
  print "  goal       Adds a goal ('add goal MyGoal')"
  print "edit         Edits a task ('edit *Action 1134')"
  print "complete     Completes a task ('complete 1234')"
  print "delete       Deletes a task ('delete 1134')"
  print "  folder     Deletes a folder ('delete folder 1')"
  print "  context    Deletes a context ('delete context 2')"
  print "  goal       Deletes a goal ('delete goal 3')"
  print 
  print "archive      Archives a folder ('archive 1234')"
  print "filter       Defines filters ('filter *Action @Someday')"
  print "unfilter     Removes all filters"
  print "filters      Displays the list of filters"
  print
  print "help or ?    Displays this help message"
  print "quit or exit Leaves the application"
end

#hotlist(session, input) ⇒ Object

Displays the ‘hotlist’ of tasks. This shows all the uncompleted items with priority set to 3 or 2. There’s no facility in the API for this, so we have to cheat a bit.

It may be worthwhile to allow the ability to tweak what constitutes a ‘hotlist’ but that’ll come by demand. Or patches. Fully documented patches, mmmm.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/toodledo/command_line/client.rb', line 215

def hotlist(session, input)
  logger.debug("hotlist: #{input}")
  
  # See if there's input following the command.
  context = parse_context(input)
  folder = parse_folder(input)
  goal = parse_goal(input)
  priority = parse_priority(input)
  
  params = { :notcomp => true }
  
  # If there are, they override what we have set.
  if (folder != nil)
    params.merge!({ :folder => folder })
  end
  
  if (context != nil)
    params.merge!({ :context => context })
  end
  
  if (goal != nil)
    params.merge!({ :goal => goal })
  end
  
  if (priority != nil)
    params.merge!({ :priority => priority })
  end
        
  tasks = session.get_tasks(params)
  
  # Highest priority first
  tasks.sort! do |a, b|
    b.priority <=> a.priority
  end
  
  # filter on our end.
  # Surprisingly, we can't search for "greater than 0 priority" with the API.
  not_important = Priority::MEDIUM
  
  for task in tasks
    if (task.priority > not_important)
      print @formatters[:task].format(task)
    end
  end
end

#list_contexts(session, input) ⇒ Object

Lists the contexts.



356
357
358
359
360
361
362
363
364
# File 'lib/toodledo/command_line/client.rb', line 356

def list_contexts(session, input)
  params = { }
  
  contexts = session.get_contexts()
  
  for context in contexts
    print @formatters[:context].format(context)
  end
end

#list_filtersObject

Shows all the filters.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/toodledo/command_line/client.rb', line 183

def list_filters()        
  if (@filters == nil || @filters.empty?)
    print "No filters."
    return
  end
  
  @filters.each do |k, v|
    if (v.respond_to? :name)
      name = v.name
    else
      name = v
    end
    print "#{k}: #{name}\n"
  end
end

#list_folders(session, input) ⇒ Object

Lists the folders.



369
370
371
372
373
374
375
376
377
# File 'lib/toodledo/command_line/client.rb', line 369

def list_folders(session, input)
  params = { }
  
  folders = session.get_folders()
  
  for folder in folders
    print @formatters[:folder].format(folder)
  end
end

#list_goals(session, input) ⇒ Object

Lists the goals. Takes an optional argument of ‘short’, ‘medium’ or ‘life’.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/toodledo/command_line/client.rb', line 324

def list_goals(session, input)
  
  input.strip!
  input.downcase!
          
  goals = session.get_goals()
  
  goals.sort! do |a, b|
    a.level <=> b.level
  end
  
  level_filter = nil
  case input
  when 'short'
    level_filter = Goal::SHORT_LEVEL
  when 'medium'
    level_filter = Goal::MEDIUM_LEVEL
  when 'life'
    level_filter = Goal::LIFE_LEVEL
  end
  
  for goal in goals
    if (level_filter != nil && goal.level != level_filter)
      next # skip this goal if it doesn't meet the filter
    end
    print @formatters[:goal].format(goal)
  end
end

#list_tasks(session, input) ⇒ Object

Lists tasks (subject to any filters that may be present).



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/toodledo/command_line/client.rb', line 264

def list_tasks(session, input)
  logger.debug("list_tasks(#{input})")
  
  params = { :notcomp => true }
  
  params.merge!(@filters)
  
  # See if there's input following the 'tasks' command.
  context = parse_context(input)
  folder = parse_folder(input)
  goal = parse_goal(input)
  priority = parse_priority(input)
  
  # If there are, they override what we have set.
  if (folder != nil)
    params.merge!({ :folder => folder })
  end
  
  if (context != nil)
    params.merge!({ :context => context })
  end
  
  if (goal != nil)
    params.merge!({ :goal => goal })
  end
  
  if (priority != nil)
    params.merge!({ :priority => priority })
  end
          
  tasks = session.get_tasks(params)
  
  # Highest priority first
  tasks.sort! do |a, b|
    b.priority <=> a.priority
  end
  
  for task in tasks  
    print @formatters[:task].format(task)
  end
end

#list_tasks_by_context(session, line) ⇒ Object

Prints all active tasks nested by context.



309
310
311
312
313
314
315
316
317
318
# File 'lib/toodledo/command_line/client.rb', line 309

def list_tasks_by_context(session, line)
  folder = parse_folder(line)
  
  session.get_contexts().each do |context|
      criteria = { :folder => folder, :context => context, :notcomp => true }
      tasks = session.get_tasks(criteria)        
      print "#{context.name}" if (! tasks.empty?)
      tasks.each { |task| print "  " + @formatters[:task].format(task) }
  end
end

#loggerObject

Returns the logger.



102
103
104
# File 'lib/toodledo/command_line/client.rb', line 102

def logger
  return @logger
end

#mainObject

Runs the client main command. This is what gets run from ‘toodledo’. Ironically doesn’t do much except for set up the commands and parse arguments from the command line. The MainCommand class does the actual command loop.



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# File 'lib/toodledo/command_line/client.rb', line 755

def main()                
  # Set the configuration from the YAML file.
  Toodledo.set_config(@userconfig)
  
  # Set up the command parser.
  graceful_exception = true
  partial_cmd_matching = true
  cmd = CmdParse::CommandParser.new(graceful_exception, partial_cmd_matching)
  cmd.program_name = "toodledo"
  cmd.program_version = Toodledo::VERSION
  
  # Options (must be before help and version are added)
  cmd.options = CmdParse::OptionParserWrapper.new do |opt|
    opt.separator "Global options:"
    opt.on("--debug", "Print debugging information") {|t| self.debug = true }
  end

  # this is the default command if we don't receive any options.
  cmd.add_command(InteractiveCommand.new(self), true)
  
  cmd.add_command(StdinCommand.new(self))
  
  cmd.add_command(AddTaskCommand.new(self))
  
  cmd.add_command(ListTasksCommand.new(self))
  cmd.add_command(ListFoldersCommand.new(self))
  cmd.add_command(ListGoalsCommand.new(self))
  cmd.add_command(ListContextsCommand.new(self))
  
  cmd.add_command(EditCommand.new(self))
  cmd.add_command(CompleteCommand.new(self))
  cmd.add_command(DeleteTaskCommand.new(self))
  cmd.add_command(HotlistCommand.new(self))
  cmd.add_command(SetupCommand.new(self))
          
  cmd.add_command(CmdParse::HelpCommand.new)
  cmd.add_command(CmdParse::VersionCommand.new)
  
  cmd.parse
  
  # Return a good exit status.
  return 0      
rescue InvalidConfigurationError => e
  logger.debug(e)
  print "The client is missing (or cannot use) the user id or password it needs to connect."
  print "Run 'toodledo setup' and save the file to fix this."
  return -1
rescue ServerError => e
  print "The server returned a fatal error: #{e.message}"
  return -1
end

Prints out a single line.



620
621
622
623
624
625
626
# File 'lib/toodledo/command_line/client.rb', line 620

def print(line = nil)
  if (line == nil)
    puts
  else
    puts line
  end
end

#set_filter(session, input) ⇒ Object

Sets the context filter. Subsequent calls to show tasks will only show tasks that have this context.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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/toodledo/command_line/client.rb', line 136

def set_filter(session, input)
  logger.debug("set_filter(#{input})")
  
  input.strip!
  
  context = parse_context(input)
  if (context != nil)
    c = session.get_context_by_name(context)
    if (c == nil)
      print "No such context: #{context}"
      return
    end
    @filters[:context] = c
  end
  
  goal = parse_goal(input)
  if (goal != nil)
    g = session.get_goal_by_name(goal)
    if (g == nil)
      print "No such goal: #{goal}"
      return
    end
    @filters[:goal] = g
  end
  
  folder = parse_folder(input)
  if (folder != nil)
    f = session.get_folder_by_name(folder)
    if (f == nil)
      print "No such folder: #{folder}"
    end
    @filters[:folder] = f
  end
  
  priority = parse_priority(input)
  if (priority != nil)
    @filters[:priority] = priority
  end
  
  if (logger)
    logger.debug("@filters = #{@filters.inspect}")
  end
end

#setupObject

Invites the user to setup the YAML file.



109
110
111
112
113
114
115
116
117
# File 'lib/toodledo/command_line/client.rb', line 109

def setup
  FileUtils::mkdir_p TOODLEDO_D, :mode => 0700 unless test ?d, TOODLEDO_D
  test ?e, CONFIG_F and FileUtils::mv CONFIG_F, "#{CONFIG_F}.bak"
  config = CONFIG[/\A.*(?=^\# AUTOCONFIG)/m]
  open(CONFIG_F, "w") { |f| f.write config }
    
  edit = (ENV["EDITOR"] || ENV["EDIT"] || "vi") + " '#{CONFIG_F}'"
  system edit or puts "edit '#{CONFIG_F}'"
end

#show_config(session) ⇒ Object

Displays the configuration information that the session is currently using.



123
124
125
126
127
128
129
130
131
# File 'lib/toodledo/command_line/client.rb', line 123

def show_config(session)
  base_url = session.base_url
  user_id = session.user_id
  proxy = session.proxy
  
  print "base_url = #{base_url}"    
  print "user_id = #{user_id}"
  print "proxy = #{proxy.inspect}"
end

#unfilterObject

Clears all the filters.



202
203
204
205
# File 'lib/toodledo/command_line/client.rb', line 202

def unfilter()
  @filters = {}
  print "Filters cleared.\n"
end