Class: Rbnotes::Commands::List

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

Overview

Defines ‘list` command for `rbnotes`. See the document of execute method to know about the behavior of this command.

Constant Summary collapse

DEFAULT_BEHAVIOR =

:nodoc:

"today"

Instance Method Summary collapse

Instance Method Details

#descriptionObject

:nodoc:



9
10
11
# File 'lib/rbnotes/commands/list.rb', line 9

def description             # :nodoc:
  "List notes"
end

#execute(args, conf) ⇒ Object

Shows a list of notes in the repository. Arguments are optional. If several args are passed, each of them must be a timestamp pattern or a keyword.

Any order of timestamp patterns and keywords mixture is acceptable. The redundant patterns or invalid patterns are just ignored.

A timestamp pattern is a string which would match several Timestamp objects. A timestamp is an instance of Textrepo::Timestamp class.

A keyword must be one of them:

- "today"      (or "to")
- "yeasterday" (or "ye")
- "this_week"  (or "tw")
- "last_week"  (or "lw")
- "this_month" (or "tm")
- "last_month" (or "lm")
- "recent"     (or "re")
- "all"

Here is several examples of timestamp patterns.

"20201027093600_012": a complete string to represent a timestamp
- this pattern would match exactly one Timestamp object

"20201027": specifies year and date
- all Timestamps those have the same year and date would match

"202011": specifies year and month
- all Timestamps those have the same year and month would match

"2020": specifies year only
- all Timestamps those have the same year would match

"1027": specifies date only
- all Timestamps those have the same date would match, even if
  they have the different year.

:call-seq:

execute(Array, Rbnotes::Conf or Hash) -> nil


60
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
# File 'lib/rbnotes/commands/list.rb', line 60

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

  if args.empty? and !@opts[:enum_week]
    default_behavior = conf[:list_default] || DEFAULT_BEHAVIOR
    args << default_behavior
  end

  utils = Rbnotes.utils
  patterns = utils.read_timestamp_patterns(args, enum_week: @opts[:enum_week])
  repo = Textrepo.init(conf)

  num_of_notes = utils.specified_recent?(args) ? conf[:number_of_recent_notes] : 0
  stamps = utils.find_notes(patterns, repo, num_of_notes)

  output = []
  if @opts[:verbose]
    collect_timestamps_by_date(stamps).each { |date, timestamps|
      output << "#{date} (#{timestamps.size})"
      timestamps.each { |timestamp|
        pad = "  "
        output << utils.make_headline(timestamp,
                                      repo.read(timestamp), pad)
      }
    }
  else
    stamps.each { |timestamp|
      output << utils.make_headline(timestamp, repo.read(timestamp))
    }
  end
  puts output
end

#helpObject

:nodoc:



94
95
96
97
98
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/list.rb', line 94

def help                    # :nodoc:
  puts <<HELP
usage:
#{Rbnotes::NAME} list [OPTIONS] [STAMP_PATTERN|KEYWORD]

Show a list of notes.  When specified several STAMP_PATTERNs, only
those match the pattern are listed.  Also, some KEYWORDs could be
used.

When no STAMP_PATTERN or KEYWORD was specified, the behavior of this
command could be specified with a configuration setting,
":list_default:".  The value must be one of valid keywords.  If no
settings was also given, this command would behave like "today" was
specified as the setting.

OPTIONS:
-v, --verbose
-w, --week

An option "--verbose" is acceptable.  It specifies to counts number of
notes by each day, then put it with the date before notes.  It looks
like as follows:

2021-04-19 (3)
  20210419134222: Foo
  20210419120235: Bar
  20210419110057: Baz
2021-04-18 (1)
  20210418125353: Hoge
      :

An option "--week" is also acceptable.  It specifies to enumerate all
days of a week.  Typically, the option is used with a STAMP_PATTERN
which specifies a date, such "20201117", then it enumerates all days
of the week which contains "17th November 2020".

STAMP_PATTERN must be:

(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"

A STAMP_PATTERN other than (a) and (b) causes an error if it was used
with "--week" option.

When no STAMP_PATTERN was specified with "--week" option, the output
would be as same as the KEYWORD, "this_week" was specified.

KEYWORD:

- "today"      (or "to")
- "yeasterday" (or "ye")
- "this_week"  (or "tw")
- "last_week"  (or "lw")
- "this_month" (or "tm")
- "last_month" (or "lm")
- "recent"     (or "re")
- "all"

The keyword, "recent" specifies to enumerate recent notes in the
repository.  The keyword, "all" specifies to enumerate all notes in
the repository.

HELP
end