Class: Rbnotes::Commands::Update

Inherits:
Command
  • Object
show all
Defined in:
lib/rbnotes/commands/update.rb

Overview

Updates the content of the note associated with given timestamp.

Reads its argument from the standard input when no argument was passed in the command line.

The timestamp associated with the note will be updated to new one, which is generated while the command exection.

When “-k” (or “–keep”) option is specified, the timestamp will remain unchanged.

Actual modification is done interactively by the external editor.

The editor program will be searched as same as add command. If none of editors is available, the execution fails.

Instance Method Summary collapse

Instance Method Details

#descriptionObject

:nodoc:



22
23
24
# File 'lib/rbnotes/commands/update.rb', line 22

def description             # :nodoc:
  "Update the content of a note"
end

#execute(args, conf) ⇒ Object

The 1st and only one argument is the timestamp to speficy the note to update.

:call-seq:

"20201020112233" -> "20201021123400"


33
34
35
36
37
38
39
40
41
42
43
44
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/rbnotes/commands/update.rb', line 33

def execute(args, conf)
  @opts = {}
  parse_opts(args)

  target_stamp = Rbnotes.utils.read_timestamp(args)
  editor = Rbnotes.utils.find_editor(conf[:editor])
  repo = Textrepo.init(conf)

  text = nil
  begin
    text = repo.read(target_stamp)
  rescue Textrepo::MissingTimestampError => _
    raise Rbnotes::MissingTimestampError, target_stamp
  end

  tmpfile = Rbnotes.utils.run_with_tmpfile(editor, target_stamp.to_s, text)
  text = File.readlines(tmpfile, :chomp => true)

  unless text.empty?
    keep = @opts[:keep_timestamp] || false
    newstamp = nil
    begin
      newstamp = repo.update(target_stamp, text, keep)
    rescue StandardError => e
      puts e.message
    else
      if keep
        puts "Update the note content, the timestamp unchanged [%s]" % newstamp
      else
        puts "Update the note [%s -> %s]" % [target_stamp, newstamp] unless target_stamp == newstamp
      end
    ensure
      # Don't forget to remove the temporary file.
      File.delete(tmpfile)
    end
  else
    puts "Nothing is updated, since the specified content is empty."
  end
end

#helpObject

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rbnotes/commands/update.rb', line 73

def help                    # :nodoc:
  puts <<HELP
usage:
#{Rbnotes::NAME} update [-k|--keep] [TIMESTAMP]

Updates the content of the note associated with given timestamp.

Reads its argument from the standard input when no argument was passed
in the command line.

The timestamp associated with the note will be updated to new one,
which is generated while the command exection.

When "-k" (or "--keep") option is specified, the timestamp will remain
unchanged.

Actual modification is done interactively by the external editor.  The
editor program will be searched as same as add command.  If none of
editors is available, the execution fails.
HELP
end