Class: Rbnotes::Commands::Add
- Defined in:
- lib/rbnotes/commands/add.rb
Overview
Adds a new note to the repository. If no options, a new timestamp is generated at the execution time, then it is attached to the note. If the timestamp has already existed in the repository, the command fails.
Accepts an option with ‘-t STAMP_PATTERN` (or `–timestamp`), a timestamp is generated according to `STAMP_PATTERN`.
STAMP_PATTERN could be one of followings:
"20201104172230_078" : full qualified timestamp string
"20201104172230" : full qualified timestamp string (no suffix)
"202011041722" : year, date and time (omit second part)
"11041722" : date and time (omit year and second part)
Also accepts an option with ‘-f FILENAME` (or `–template-file`) to specify a template file which will be the initial content of a new note.
This command starts the external editor program to prepare text to store. The editor program will be searched in the following order:
1. conf[:editor] (conf is the 1st arg of execute method)
2. ENV["EDITOR"]
3. "nano"
4. "vi"
If none of the above editor is available, the command fails.
TEMPLATE FILE:
This command search a file as its template which will be the initial conent of a new note when an option or a setting in the configuration file (‘:template`) is specified a template file.
Or, even if neither an option nor a setting about a template file is specified, it always searches the default directory to read a template file. The directory is:
- $XDG_CONIFG_HOME/rbnotes/templates (if $XDG_CONFIG_HOME is defined)
or
- $HOME/.config/rbnotes/templates
If a file, ‘default.md` exists in the above directory, it will used as a template.
A template file can be written in ERB syntax. See ERB manual to know about how to mix ruby code and text content in ERB syntax.
Instance Method Summary collapse
-
#description ⇒ Object
:nodoc:.
- #execute(args, conf) ⇒ Object
-
#help ⇒ Object
:nodoc:.
Instance Method Details
#description ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/rbnotes/commands/add.rb', line 57 def description # :nodoc: "Add a new note" end |
#execute(args, conf) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbnotes/commands/add.rb', line 61 def execute(args, conf) @opts = {} parse_opts(args) stamp = @opts[:timestamp] || Textrepo::Timestamp.new(Time.now) candidates = [conf[:editor], ENV["EDITOR"], "nano", "vi"].compact editor = Rbnotes.utils.find_program(candidates) raise Rbnotes::NoEditorError, candidates if editor.nil? template = read_template(conf) tmpfile = Rbnotes.utils.run_with_tmpfile(editor, stamp.to_s, template) unless FileTest.exist?(tmpfile) puts "Cancel adding, since nothing to store" return end text = File.readlines(tmpfile) repo = Textrepo.init(conf) begin repo.create(stamp, text) rescue Textrepo::DuplicateTimestampError => e puts e. puts "Just wait a second, then retry." rescue Textrepo::EmptyTextError => e puts e. rescue StandardError => e puts e. else puts "Add a note [%s]" % stamp.to_s ensure # Don't forget to remove the temporary file. File.delete(tmpfile) end end |
#help ⇒ Object
:nodoc:
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 |
# File 'lib/rbnotes/commands/add.rb', line 99 def help # :nodoc: puts <<HELP usage: #{Rbnotes::NAME} add [(-t|--timestamp) STAMP_PATTERN] Add a new note to the repository. If no options, a new timestamp is generated at the execution time, then it is attached to the note. OPTIONS: -t, --timestamp STAMP_PATTERN -f, --template-file FILENAME Accept an option with `-t STAMP_PATTERN` (or `--timestamp`), a timestamp of a new note is generated according to `STAMP_PATTERN`. Also accepts an option with `-f FILENAME` (or `--template-file`) to specify a template file which will be the initial content of a new note. See below section more about a template file. STAMP_PATTERN could be: "20201104172230_078" : full qualified timestamp string "20201104172230" : full qualified timestamp string (no suffix) "202011041722" : year, date and time (omit second part) "11041722" : date and time (omit year and second part) EDITOR: This command starts the external editor program to prepare text to store. The editor program will be searched in the following order: 1. configuration setting of ":editor" 2. ENV["EDITOR"] 3. "nano" 4. "vi" If none of the above editor is available, the execution fails. TEMPLATE FILE: This command search a file as its template which will be the initial conent of a new note when an option or a setting in the configuration file (`:template`) is specified a template file. Or, even if neither an option nor a setting about a template file is specified, it always searches the default directory to read a template file. The directory is: - $XDG_CONIFG_HOME/rbnotes/templates (if $XDG_CONFIG_HOME is defined) or - $HOME/.config/rbnotes/templates If a file, `default.md` exists in the above directory, it will used as a template. A template file can be written in ERB syntax. See ERB manual to know about how to mix ruby code and text content in ERB syntax. HELP end |