class Rbnotes::Utils
Defines several utility methods those are intended to be used in Rbnotes
classes.
Constants
- TIMESTAMP_DELIMITERS
Acceptable delimiters to separate a timestamp string for human being to read and input easily.
Here is some examples:
- "2021-04-15 15:34:56" -> "20210415153456" (a timestamp string) - "2020-04-15_15:34:56" -> (same as above) - "2020-04-15-15-34-56" -> (same as above) - "2020 04 15 15 34 56" -> (same as above) - "2020-04-15" -> "20200415" (a timestamp pattern)
Public Instance Methods
Parses the given arguments and expand keywords if found. Each of the arguments is assumed to represent a timestamp pattern (or a keyword to be expand into several timestamp pattern). Returns an Array of timestamp partterns (each pattern is a String object).
A timestamp pattern looks like:
(a) full qualified timestamp (with suffix): "20201030160200" (b) year and date part: "20201030" (c) year and month part: "202010" (d) year part only: "2020" (e) date part only: "1030"
KEYWORD:
- "today" (or "to") - "yeasterday" (or "ye") - "this_week" (or "tw") - "last_week" (or "lw") - "this_month" (or "tm") - "last_month" (or "lm") - "all"
# File lib/rbnotes/utils.rb, line 256 def expand_keyword_in_args(args) patterns = [] while args.size > 0 arg = args.shift if arg == "all" return [nil] elsif KEYWORDS.include?(arg) patterns.concat(expand_keyword(arg)) else patterns << arg end end patterns.uniq.sort end
Finds a external editor program which is specified with the argument, then returns the absolute path of the editor. If the specified editor was not found, then search default editors in the command search paths (i.e. `ENV). See also the document for `find_program`.
The default editors to search in the search paths are:
-
“nano”
-
“vi”
When all the default editors were not found, returns `nil`.
# File lib/rbnotes/utils.rb, line 46 def find_editor(preferred_editor) find_program([preferred_editor, ENV["EDITOR"], "nano", "vi"].compact) end
Finds all notes those timestamps match to given patterns in the given repository. Returns an Array contains Timestamp objects. The returned Array is sorted by Timestamp.
# File lib/rbnotes/utils.rb, line 310 def find_notes(timestamp_patterns, repo) timestamp_patterns.map { |pat| repo.entries(pat) }.flatten.sort{ |a, b| b <=> a }.uniq end
Finds a executable program in given names. When the executable was found, it stops searching then returns an absolute path of the executable.
The actual searching is done in 2 cases. That is, a given name is:
-
an absolute path: returns the path itself if it exists and is executable.
-
just a program name: searchs the name in the search paths (ENV); if it is found in a path, construct an absolute path from the name and the path, then returns the path.
# File lib/rbnotes/utils.rb, line 70 def find_program(names) names.each { |name| pathname = Pathname.new(name) if pathname.absolute? return pathname.to_path if pathname.exist? && pathname.executable? else abs = search_in_path(name) return abs unless abs.nil? end } nil end
Makes a headline with the timestamp and subject of the notes, it looks like as follows:
|<--------------- console column size -------------------->| | |+-- timestamp ---+ +-subject (the 1st line of note) -+ | | | | | |20101010001000_123: I love Macintosh. [EOL] | |20100909090909_999: This is very very long looong subj[EOL] |<->| | | ^--- pad ++ ^--- delimiter (2 characters)
The subject part will truncate when it is long.
# File lib/rbnotes/utils.rb, line 286 def make_headline(timestamp, text, pad = nil) _, column = IO.console_size delimiter = ": " timestamp_width = timestamp.to_s.size subject_width = column - timestamp_width - delimiter.size - 1 subject_width -= pad.size unless pad.nil? subject = remove_heading_markup(text[0]) ts_part = "#{timestamp.to_s} "[0..(timestamp_width - 1)] ts_part.prepend(pad) unless pad.nil? sj_part = truncate_str(subject, subject_width) ts_part + delimiter + sj_part end
Generates multiple Textrepo::Timestamp objects from the command line arguments. When no argument is given, try to read from STDIN.
When multiple strings those point the identical time are included the arguments (passed or read form STDIN), the redundant strings will be removed.
The order of the arguments will be preserved into the return value, even if the redundant strings were removed.
# File lib/rbnotes/utils.rb, line 150 def read_multiple_timestamps(args) strings = args.size < 1 ? read_multiple_args($stdin) : args raise NoArgumentError if (strings.nil? || strings.empty?) strings.uniq.map { |str| str = remove_delimiters_from_timestamp_string(str) Textrepo::Timestamp.parse_s(str) } end
Generates a Textrepo::Timestamp object from a String which comes from the command line arguments. When no argument is given, then reads from STDIN.
# File lib/rbnotes/utils.rb, line 127 def read_timestamp(args) str = args.shift || read_arg($stdin) raise NoArgumentError if str.nil? str = remove_delimiters_from_timestamp_string(str) Textrepo::Timestamp.parse_s(str) end
Reads timestamp patterns in an array of arguments. It supports keywords expansion and enumeration of week. The function is intended to be used from Commands::List#execute
and Commands::Pick#execute
.
# File lib/rbnotes/utils.rb, line 165 def read_timestamp_patterns(args, enum_week: false) patterns = nil if enum_week args.unshift(Time.now.strftime("%Y%m%d")) if args.size == 0 patterns = [] while args.size > 0 arg = args.shift begin patterns.concat(timestamp_patterns_in_week(arg.dup)) rescue InvalidTimestampPatternAsDateError => _e raise InvalidTimestampPatternAsDateError, args.unshift(arg) end end else patterns = expand_keyword_in_args(args) end patterns end
Executes the program with passing the given filename as argument. The file will be created into `Dir.tmpdir`.
If initial_content is not nil, it must be an array of strings then it provides the initial content of a temporary file.
# File lib/rbnotes/utils.rb, line 94 def run_with_tmpfile(prog, filename, initial_content = nil) tmpfile = File.expand_path(add_extension(filename), Dir.tmpdir) unless initial_content.nil? File.open(tmpfile, "w") {|f| f.print(initial_content.join("\n"))} end rc = system(prog, tmpfile) raise ProgramAbortError, [prog, tmpfile] unless rc tmpfile end
Enumerates all timestamp patterns in a week which contains a given timestamp as a day of the week.
The argument must be one of the followings:
- "yyyymodd" (eg. "20201220") - "yymoddhhmiss" (eg. "20201220120048") - "yymoddhhmiss_sfx" (eg. "20201220120048_012") - "modd" (eg. "1220") (assums in the current year) - nil (assumes today)
# File lib/rbnotes/utils.rb, line 198 def timestamp_patterns_in_week(arg) date_str = nil if arg date_str = remove_delimiters_from_timestamp_string(arg) else date_str = Textrepo::Timestamp.now[0, 8] end case date_str.size when "yyyymodd".size # nothing to do when "yyyymoddhhmiss".size, "yyyymoddhhmiss_sfx".size date_str = date_str[0, 8] when "modd".size this_year = Time.now.year.to_s date_str = "#{this_year}#{date_str}" else raise InvalidTimestampPatternAsDateError, arg end begin date = Date.parse(date_str) rescue Date::Error => _e raise InvalidTimestampPatternAsDateError, arg end dates_in_week(date).map { |date| timestamp_pattern(date) } end