rbnotes 0.4.8 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ module Rbnotes::Commands
7
7
  class Help < Command
8
8
 
9
9
  def description # :nodoc:
10
- "Provide help on each command"
10
+ "Print help of each command"
11
11
  end
12
12
 
13
13
  ##
@@ -20,8 +20,6 @@ module Rbnotes::Commands
20
20
  case cmd_name
21
21
  when nil
22
22
  self.help
23
- when "commands"
24
- print_commands
25
23
  else
26
24
  Commands.load(cmd_name).help
27
25
  end
@@ -59,40 +57,5 @@ Further information:
59
57
  HELP
60
58
  end
61
59
 
62
- # :stopdoc:
63
- private
64
-
65
- def print_commands
66
- Dir.glob("*.rb", :base => __dir__) { |rb|
67
- next if rb == "help.rb"
68
- require_relative rb
69
- }
70
- commands = Commands.constants.difference([:Builtins, :Command])
71
- builtins = Commands::Builtins.constants
72
-
73
- puts "#{Rbnotes::NAME.capitalize} Commands:"
74
- print_commands_desc(commands.sort)
75
- puts
76
- puts "for development purpose"
77
- print_builtins_desc(builtins.sort)
78
- end
79
-
80
- def print_commands_desc(commands)
81
- print_desc(Commands, commands)
82
- end
83
-
84
- def print_builtins_desc(builtins)
85
- print_desc(Commands::Builtins, builtins)
86
- end
87
-
88
- def print_desc(mod, commands)
89
- commands.map { |cmd|
90
- name = "#{cmd.to_s.downcase} "[0, 8]
91
- desc = mod.const_get(cmd, false).new.description
92
- puts " #{name} #{desc}"
93
- }
94
- end
95
-
96
- # :startdoc:
97
60
  end
98
61
  end
@@ -4,9 +4,12 @@ module Rbnotes::Commands
4
4
  # Imports a existing file which specified by the argument as a note.
5
5
  #
6
6
  # A timestamp is generated referring to the birthtime of the given
7
- # file. If birthtime is not available on the system, use mtime
7
+ # file. If birthtime is not available on the system, uses mtime
8
8
  # (modification time).
9
9
  #
10
+ # When the option, "-m" (or "--use-mtime") is specified, uses mtime
11
+ # instead of birthtime.
12
+ #
10
13
  # Occasionally, there is another note which has the same timestmap
11
14
  # in the repository. Then, tries to create a new timestamp with a
12
15
  # suffix. Unluckily, when such timestamp with a suffix already
@@ -25,11 +28,29 @@ module Rbnotes::Commands
25
28
  # execute([PATHNAME], Rbnotes::Conf or Hash) -> nil
26
29
 
27
30
  def execute(args, conf)
31
+ @opts = {}
32
+ while args.size > 0
33
+ arg = args.shift
34
+ case arg
35
+ when "-m", "--use-mtime"
36
+ @opts[:use_mtime] = true
37
+ else
38
+ args.unshift(arg)
39
+ break
40
+ end
41
+ end
42
+
28
43
  file = args.shift
29
44
  unless file.nil?
30
45
  st = File::Stat.new(file)
31
- btime = st.respond_to?(:birthtime) ? st.birthtime : st.mtime
32
- stamp = Textrepo::Timestamp.new(btime)
46
+ time = nil
47
+ if @opts[:use_mtime]
48
+ time = st.mtime
49
+ else
50
+ time = st.respond_to?(:birthtime) ? st.birthtime : st.mtime
51
+ end
52
+
53
+ stamp = Textrepo::Timestamp.new(time)
33
54
  puts "Import [%s] (timestamp [%s]) ..." % [file, stamp]
34
55
 
35
56
  repo = Textrepo.init(conf)
@@ -72,7 +93,7 @@ module Rbnotes::Commands
72
93
  puts "Cannot create a text into the repository with the" \
73
94
  " specified file [%s]." % file
74
95
  puts "For, the birthtime [%s] is identical to some notes" \
75
- " already exists in the reopsitory." % btime
96
+ " already exists in the reopsitory." % time
76
97
  puts "Change the birthtime of the target file, then retry."
77
98
  else
78
99
  puts "... Done."
@@ -86,7 +107,7 @@ module Rbnotes::Commands
86
107
  def help # :nodoc:
87
108
  puts <<HELP
88
109
  usage:
89
- #{Rbnotes::NAME} import FILE
110
+ #{Rbnotes::NAME} import [-m|--use-mtime] FILE
90
111
 
91
112
  Imports a existing file which specified by the argument as a note.
92
113
 
@@ -24,10 +24,12 @@ module Rbnotes::Commands
24
24
  #
25
25
  # A keyword must be one of them:
26
26
  #
27
- # - "today" (or "to")
28
- # - "yeasterday" (or "ye")
29
- # - "this_week" (or "tw")
30
- # - "last_week" (or "lw")
27
+ # - "today" (or "to")
28
+ # - "yeasterday" (or "ye")
29
+ # - "this_week" (or "tw")
30
+ # - "last_week" (or "lw")
31
+ # - "this_month" (or "tm")
32
+ # - "last_month" (or "lm")
31
33
  #
32
34
  # Here is several examples of timestamp patterns.
33
35
  #
@@ -51,18 +53,48 @@ module Rbnotes::Commands
51
53
  # execute(Array, Rbnotes::Conf or Hash) -> nil
52
54
 
53
55
  def execute(args, conf)
54
- patterns = Rbnotes.utils.expand_keyword_in_args(args)
56
+ @opts = {}
57
+ while args.size > 0
58
+ arg = args.shift
59
+ case arg
60
+ when "-w", "--week"
61
+ @opts[:enum_week] = true
62
+ when "-v", "--verbose"
63
+ @opts[:verbose] = true
64
+ else
65
+ args.unshift(arg)
66
+ break
67
+ end
68
+ end
69
+
70
+ utils = Rbnotes.utils
71
+ patterns = utils.read_timestamp_patterns(args, enum_week: @opts[:enum_week])
72
+
55
73
  @repo = Textrepo.init(conf)
56
- # newer stamp shoud be above
57
- Rbnotes.utils.find_notes(patterns, @repo).each { |timestamp|
58
- puts Rbnotes.utils.make_headline(timestamp, @repo.read(timestamp))
59
- }
74
+ notes = utils.find_notes(patterns, @repo)
75
+ output = []
76
+ if @opts[:verbose]
77
+ collect_timestamps_by_date(notes).each { |date, timestamps|
78
+ output << "#{date} (#{timestamps.size})"
79
+ timestamps.each { |timestamp|
80
+ pad = " "
81
+ output << utils.make_headline(timestamp,
82
+ @repo.read(timestamp), pad)
83
+ }
84
+ }
85
+ else
86
+ notes.each { |timestamp|
87
+ output << utils.make_headline(timestamp,
88
+ @repo.read(timestamp))
89
+ }
90
+ end
91
+ puts output
60
92
  end
61
93
 
62
94
  def help # :nodoc:
63
95
  puts <<HELP
64
96
  usage:
65
- #{Rbnotes::NAME} list [STAMP_PATTERN|KEYWORD]
97
+ #{Rbnotes::NAME} list [-w|--week][STAMP_PATTERN|KEYWORD]
66
98
 
67
99
  Show a list of notes. When no arguments, make a list with all notes
68
100
  in the repository. When specified STAMP_PATTERN, only those match the
@@ -83,9 +115,40 @@ KEYWORD:
83
115
  - "yeasterday" (or "ye")
84
116
  - "this_week" (or "tw")
85
117
  - "last_week" (or "lw")
118
+ - "this_month" (or "tm")
119
+ - "last_month" (or "lm")
86
120
 
121
+ An option "--week" is also acceptable. It specifies to enumerate all
122
+ days of a week. Typically, the option is used with a STAMP_PATTERN
123
+ which specifies a date, such "20201117", then it enumerates all days
124
+ of the week which contains "17th November 2020".
125
+
126
+ A STAMP_PATTERN other than (a) and (b) causes an error if it was used
127
+ with "--week" option.
128
+
129
+ When no STAMP_PATTERN was specified with "--week" option, the output
130
+ would be as same as the KEYWORD, "this_week" was specified.
87
131
  HELP
88
132
  end
89
133
 
134
+ # :stopdoc:
135
+
136
+ private
137
+
138
+ def collect_timestamps_by_date(timestamps)
139
+ result = {}
140
+ timestamps.map { |ts|
141
+ [ts.strftime("%Y-%m-%d"), ts]
142
+ }.reduce(result) { |r, pair|
143
+ date, stamp = pair
144
+ r[date] ||= []
145
+ r[date] << stamp
146
+ r
147
+ }
148
+ result
149
+ end
150
+
151
+ # :startdoc:
152
+
90
153
  end
91
154
  end
@@ -10,18 +10,36 @@ module Rbnotes::Commands
10
10
  end
11
11
 
12
12
  def execute(args, conf)
13
- patterns = Rbnotes.utils.expand_keyword_in_args(args)
13
+ @opts = {}
14
+ while args.size > 0
15
+ arg = args.shift
16
+ case arg
17
+ when "-w", "--week"
18
+ @opts[:enum_week] = true
19
+ else
20
+ args.unshift(arg)
21
+ break
22
+ end
23
+ end
24
+
25
+ utils = Rbnotes.utils
26
+ patterns = utils.read_timestamp_patterns(args, enum_week: @opts[:enum_week])
27
+
14
28
  @repo = Textrepo.init(conf)
15
29
 
16
30
  list = []
17
- Rbnotes.utils.find_notes(patterns, @repo).each { |timestamp|
18
- list << Rbnotes.utils.make_headline(timestamp, @repo.read(timestamp))
31
+ utils.find_notes(patterns, @repo).each { |timestamp|
32
+ list << utils.make_headline(timestamp, @repo.read(timestamp))
19
33
  }
20
34
 
21
35
  picker = conf[:picker]
22
36
  unless picker.nil?
37
+ picker_opts = conf[:picker_option]
38
+ cmds = [picker]
39
+ cmds.concat(picker_opts.split) unless picker_opts.nil?
40
+
23
41
  require 'open3'
24
- result = Open3.pipeline_rw(picker) { |stdin, stdout, _|
42
+ result = Open3.pipeline_rw(cmds) { |stdin, stdout, _|
25
43
  stdin.puts list
26
44
  stdin.close
27
45
  stdout.read
@@ -1,52 +1,97 @@
1
1
  module Rbnotes::Commands
2
2
 
3
3
  ##
4
- # Shows the content of the note specified by the argument. The
4
+ # Shows the content of the notes specified by arguments. Each
5
5
  # argument must be a string which can be converted into
6
6
  # Textrepo::Timestamp object.
7
7
  #
8
- # A string for Timestamp must be:
8
+ # A string for Textrepo::Timestamp must be:
9
9
  #
10
10
  # "20201106112600" : year, date, time and sec
11
11
  # "20201106112600_012" : with suffix
12
12
  #
13
- # If no argument is passed, reads the standard input for an argument.
13
+ # If no argument is passed, reads the standard input for arguments.
14
14
 
15
15
  class Show < Command
16
16
 
17
17
  def description # :nodoc:
18
- "Show the content of a note"
18
+ "Show the content of notes"
19
19
  end
20
20
 
21
21
  def execute(args, conf)
22
- stamp = Rbnotes.utils.read_timestamp(args)
23
-
22
+ stamps = Rbnotes.utils.read_multiple_timestamps(args)
24
23
  repo = Textrepo.init(conf)
25
- content = repo.read(stamp)
24
+
25
+ content = stamps.map { |stamp| [stamp, repo.read(stamp)] }.to_h
26
26
 
27
27
  pager = conf[:pager]
28
28
  unless pager.nil?
29
- require 'open3'
30
- Open3.pipeline_w(pager) { |stdin|
31
- stdin.puts content
32
- stdin.close
33
- }
29
+ puts_with_pager(pager, make_output(content))
34
30
  else
35
- puts content
31
+ puts make_output(content)
36
32
  end
37
33
  end
38
34
 
39
35
  def help # :nodoc:
40
36
  puts <<HELP
41
37
  usage:
42
- #{Rbnotes::NAME} show [TIMESTAMP]
38
+ #{Rbnotes::NAME} show [TIMESTAMP...]
43
39
 
44
- Show the content of given note. TIMESTAMP must be a fully qualified
40
+ Show the content of given notes. TIMESTAMP must be a fully qualified
45
41
  one, such "20201016165130" or "20201016165130_012" if it has a suffix.
46
42
 
47
43
  The command try to read its argument from the standard input when no
48
44
  argument was passed in the command line.
49
45
  HELP
50
46
  end
47
+
48
+ # :stopdoc:
49
+
50
+ private
51
+
52
+ def puts_with_pager(pager, output)
53
+ require "open3"
54
+ Open3.pipeline_w(pager) { |stdin|
55
+ stdin.puts output
56
+ stdin.close
57
+ }
58
+ end
59
+
60
+ require "io/console/size"
61
+
62
+ def make_output(content)
63
+ if content.size <= 1
64
+ return content.values[0]
65
+ end
66
+
67
+ _, column = IO.console_size
68
+ output = content.map { |timestamp, text|
69
+ ary = [make_heading(timestamp, [column, 72].min)]
70
+ ary.concat(text)
71
+ ary
72
+ }
73
+
74
+ output = insert_delimiter(output, "")
75
+ output.flatten
76
+ end
77
+
78
+ def make_heading(timestamp, column)
79
+ stamp_str = timestamp.to_s
80
+ length = column - (stamp_str.size + 2)
81
+ "#{stamp_str} #{Array.new(length, '-').join}"
82
+ end
83
+
84
+ def insert_delimiter(ary, delimiter = "")
85
+ result = []
86
+ ary.each { |e|
87
+ result << e
88
+ result << delimiter
89
+ }
90
+ result.delete_at(-1)
91
+ result
92
+ end
93
+
94
+ # :startdoc:
95
+
51
96
  end
52
97
  end
@@ -0,0 +1,55 @@
1
+ module Rbnotes::Commands
2
+ ##
3
+ # Shows statistics.
4
+
5
+ class Statistics < Command
6
+
7
+ def description # :nodoc:
8
+ "Show statistics values"
9
+ end
10
+
11
+ def execute(args, conf)
12
+ report = :total
13
+ while args.size > 0
14
+ arg = args.shift
15
+ case arg
16
+ when "-y", "--yearly"
17
+ report = :yearly
18
+ break
19
+ when "-m", "--monthly"
20
+ report = :monthly
21
+ break
22
+ else
23
+ args.unshift(arg)
24
+ raise ArgumentError, "invalid option or argument: %s" % args.join(" ")
25
+ end
26
+ end
27
+
28
+ stats = Rbnotes::Statistics.new(conf)
29
+ case report
30
+ when :yearly
31
+ stats.yearly_report
32
+ when :monthly
33
+ stats.monthly_report
34
+ else
35
+ stats.total_report
36
+ end
37
+ end
38
+
39
+ def help
40
+ puts <<HELP
41
+ usage:
42
+ #{Rbnotes::NAME} statistics ([-y|--yearly]|[-m|--monthly])
43
+
44
+ option:
45
+ -y, --yearly : print yearly report
46
+ -m, --monthly : print monthly report
47
+
48
+ Show statistics.
49
+
50
+ In the version #{Rbnotes::VERSION}, only number of notes is supported.
51
+ HELP
52
+ end
53
+
54
+ end
55
+ end
@@ -82,7 +82,7 @@ module Rbnotes::Commands
82
82
  def help # :nodoc:
83
83
  puts <<HELP
84
84
  usage:
85
- #{Rbnotes::NAME} update [TIMESTAMP]
85
+ #{Rbnotes::NAME} update [-k|--keep] [TIMESTAMP]
86
86
 
87
87
  Updates the content of the note associated with given timestamp.
88
88