rbnotes 0.3.0 → 0.4.3

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.
@@ -0,0 +1,67 @@
1
+ module Rbnotes::Commands
2
+
3
+ ##
4
+ # Searches a given pattern in notes those have timestamps match a
5
+ # given timestamp pattern. The first argument is a pattern to search.
6
+ # It is a String object represents a portion of text or it may a
7
+ # String represents a regular expression. The second argument is
8
+ # optional and it is a timestamp pattern to specify the search target.
9
+ #
10
+ # A pattern for search is mandatory. If no pattern, raises
11
+ # Rbnotes::MissingArgumentError.
12
+ #
13
+ # Example of PATTERN for search:
14
+ #
15
+ # "rbnotes" (a word)
16
+ # "macOS Big Sur" (a few words)
17
+ # "2[0-9]{3}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])" (a regular expression)
18
+ #
19
+ # A timestamp pattern is optional. If no timestamp pattern, all notes
20
+ # in the repository would be target of search.
21
+ #
22
+ # See the document of `Rbnotes::Commands::List#execute` to know about
23
+ # a timestamp pattern.
24
+
25
+ class Search < Command
26
+
27
+ def description # :nodoc:
28
+ "Search a given pattern in notes"
29
+ end
30
+
31
+ def execute(args, conf)
32
+ pattern = args.shift
33
+ raise MissingArgumentError, args if pattern.nil?
34
+
35
+ timestamp_pattern = args.shift # `nil` is acceptable
36
+
37
+ repo = Textrepo.init(conf)
38
+ begin
39
+ result = repo.search(pattern, timestamp_pattern)
40
+ rescue Textrepo::InvalidSearchResultError => e
41
+ puts e.message
42
+ else
43
+ result.each { |stamp, num, match|
44
+ puts "#{stamp}:#{num}:#{match}"
45
+ }
46
+ end
47
+ end
48
+
49
+ def help # :nodoc:
50
+ puts <<HELP
51
+ usage:
52
+ #{Rbnotes::NAME} search PATTERN [STAMP_PATTERN]
53
+
54
+ PATTERN is a word (or words) to search, it may also be a regular
55
+ expression.
56
+
57
+ STAMP_PATTERN must be:
58
+
59
+ (a) full qualified timestamp (with suffix): "20201030160200"
60
+ (b) year and date part: "20201030"
61
+ (c) year and month part: "202010"
62
+ (d) year part only: "2020"
63
+ (e) date part only: "1030"
64
+ HELP
65
+ end
66
+ end
67
+ end
@@ -1,5 +1,23 @@
1
- module Rbnotes
2
- class Commands::Show < Commands::Command
1
+ module Rbnotes::Commands
2
+
3
+ ##
4
+ # Shows the content of the note specified by the argument. The
5
+ # argument must be a string which can be converted into
6
+ # Textrepo::Timestamp object.
7
+ #
8
+ # A string for Timestamp must be:
9
+ #
10
+ # "20201106112600" : year, date, time and sec
11
+ # "20201106112600_012" : with suffix
12
+ #
13
+ # If no argument is passed, reads the standard input for an argument.
14
+
15
+ class Show < Command
16
+
17
+ def description # :nodoc:
18
+ "Show the content of a note"
19
+ end
20
+
3
21
  def execute(args, conf)
4
22
  stamp = Rbnotes::Utils.read_timestamp(args)
5
23
 
@@ -17,5 +35,18 @@ module Rbnotes
17
35
  puts content
18
36
  end
19
37
  end
38
+
39
+ def help # :nodoc:
40
+ puts <<HELP
41
+ usage:
42
+ #{Rbnotes::NAME} show [TIMESTAMP]
43
+
44
+ Show the content of given note. TIMESTAMP must be a fully qualified
45
+ one, such "20201016165130" or "20201016165130_012" if it has a suffix.
46
+
47
+ The command try to read its argument from the standard input when no
48
+ argument was passed in the command line.
49
+ HELP
50
+ end
20
51
  end
21
52
  end
@@ -1,37 +1,49 @@
1
1
  module Rbnotes::Commands
2
+
2
3
  ##
3
4
  # Updates the content of the note associated with given timestamp.
4
- # Actual modification is done interactively by the external editor.
5
+ #
6
+ # Reads its argument from the standard input when no argument was
7
+ # passed in the command line.
8
+ #
5
9
  # The timestamp associated with the note will be updated to new one,
6
10
  # which is generated while the command exection.
7
11
  #
8
- # A timestamp string must be specified as the only argument. It
9
- # must exactly match to the one of the target note in the
10
- # repository. When the given timestamp was not found, the command
11
- # fails.
12
- #
13
- # Timestamp which is associated to the target note will be newly
14
- # generated with the command execution time. That is, the timestamp
15
- # before the command exection will be obsolete.
12
+ # When "-k" (or "--keep") option is specified, the timestamp will
13
+ # remain unchanged.
16
14
  #
17
- # This command starts the external editor program to edit the
18
- # content of the note. The editor program will be searched as same
19
- # as add command.
15
+ # Actual modification is done interactively by the external editor.
20
16
  #
21
- # If none of editors is available, the command fails.
17
+ # The editor program will be searched as same as add command. If
18
+ # none of editors is available, the execution fails.
22
19
 
23
20
  class Update < Command
24
21
  include ::Rbnotes::Utils
25
22
 
23
+ def description # :nodoc:
24
+ "Update the content of a note"
25
+ end
26
+
26
27
  ##
27
28
  # The 1st and only one argument is the timestamp to speficy the
28
- # note to update. Returns the new timestamp which is associated
29
- # to the note updated.
29
+ # note to update.
30
30
  #
31
31
  # :call-seq:
32
32
  # "20201020112233" -> "20201021123400"
33
33
 
34
34
  def execute(args, conf)
35
+ @opts = {}
36
+ while args.size > 0
37
+ arg = args.shift
38
+ case arg
39
+ when "-k", "--keep"
40
+ @opts[:keep_timestamp] = true
41
+ else
42
+ args.unshift(arg)
43
+ break
44
+ end
45
+ end
46
+
35
47
  target_stamp = Rbnotes::Utils.read_timestamp(args)
36
48
  editor = find_editor(conf[:editor])
37
49
  repo = Textrepo.init(conf)
@@ -44,16 +56,21 @@ module Rbnotes::Commands
44
56
  end
45
57
 
46
58
  tmpfile = run_with_tmpfile(editor, target_stamp.to_s, text)
47
- text = File.readlines(tmpfile)
59
+ text = File.readlines(tmpfile, :chomp => true)
48
60
 
49
61
  unless text.empty?
62
+ keep = @opts[:keep_timestamp] || false
50
63
  newstamp = nil
51
64
  begin
52
- newstamp = repo.update(target_stamp, text)
65
+ newstamp = repo.update(target_stamp, text, keep)
53
66
  rescue StandardError => e
54
67
  puts e.message
55
68
  else
56
- puts "Update the note [%s -> %s]" % [target_stamp, newstamp]
69
+ if keep
70
+ puts "Update the note content, the timestamp unchanged [%s]" % newstamp
71
+ else
72
+ puts "Update the note [%s -> %s]" % [target_stamp, newstamp] unless target_stamp == newstamp
73
+ end
57
74
  ensure
58
75
  # Don't forget to remove the temporary file.
59
76
  File.delete(tmpfile)
@@ -62,5 +79,27 @@ module Rbnotes::Commands
62
79
  puts "Nothing is updated, since the specified content is empty."
63
80
  end
64
81
  end
82
+
83
+ def help # :nodoc:
84
+ puts <<HELP
85
+ usage:
86
+ #{Rbnotes::NAME} update [TIMESTAMP]
87
+
88
+ Updates the content of the note associated with given timestamp.
89
+
90
+ Reads its argument from the standard input when no argument was passed
91
+ in the command line.
92
+
93
+ The timestamp associated with the note will be updated to new one,
94
+ which is generated while the command exection.
95
+
96
+ When "-k" (or "--keep") option is specified, the timestamp will remain
97
+ unchanged.
98
+
99
+ Actual modification is done interactively by the external editor. The
100
+ editor program will be searched as same as add command. If none of
101
+ editors is available, the execution fails.
102
+ HELP
103
+ end
65
104
  end
66
105
  end
@@ -9,7 +9,6 @@ module Rbnotes
9
9
  module ErrMsg
10
10
  MISSING_ARGUMENT = "missing argument: %s"
11
11
  MISSING_TIMESTAMP = "missing timestamp: %s"
12
- INVALID_TIMESTAMP_STRING = "invalid string as timestamp: %s"
13
12
  NO_EDITOR = "No editor is available: %s"
14
13
  PROGRAM_ABORT = "External program was aborted: %s"
15
14
  end
@@ -35,16 +34,6 @@ module Rbnotes
35
34
  end
36
35
  end
37
36
 
38
- ##
39
- # An error raised if an argument is invalid to convert a
40
- # Textrepo::Timestamp object.
41
-
42
- class InvalidTimestampStringError < Error
43
- def initialize(str)
44
- super(ErrMsg::INVALID_TIMESTAMP_STRING % str)
45
- end
46
- end
47
-
48
37
  ##
49
38
  # An error raised if no external editor is available to edit a note,
50
39
  # even "nano" or "vi".
@@ -96,11 +96,7 @@ module Rbnotes
96
96
 
97
97
  def read_timestamp(args)
98
98
  str = args.shift || read_arg($stdin)
99
- begin
100
- Textrepo::Timestamp.parse_s(str)
101
- rescue ArgumentError => _
102
- raise InvalidTimestampStringError, str
103
- end
99
+ Textrepo::Timestamp.parse_s(str)
104
100
  end
105
101
  module_function :read_timestamp
106
102
 
@@ -117,7 +113,11 @@ module Rbnotes
117
113
  # foo bar baz ...
118
114
  #
119
115
  # then, only the first string is interested
120
- io.gets.split(" ")[0]
116
+ begin
117
+ io.gets.split(":")[0].rstrip
118
+ rescue NoMethodError => _
119
+ nil
120
+ end
121
121
  end
122
122
  module_function :read_arg
123
123
 
@@ -1,4 +1,4 @@
1
1
  module Rbnotes
2
- VERSION = "0.3.0"
3
- RELEASE = '2020-10-29'
2
+ VERSION = "0.4.3"
3
+ RELEASE = "2020-11-08"
4
4
  end
@@ -25,6 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency "textrepo", "~> 0.4"
28
+ spec.add_dependency "textrepo", "~> 0.5.4"
29
29
  spec.add_dependency "unicode-display_width", "~> 1.7"
30
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbnotes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mnbi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-29 00:00:00.000000000 Z
11
+ date: 2020-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: textrepo
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.4'
19
+ version: 0.5.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.4'
26
+ version: 0.5.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: unicode-display_width
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -56,13 +56,19 @@ files:
56
56
  - Rakefile
57
57
  - bin/console
58
58
  - bin/setup
59
+ - conf/config.yml
60
+ - conf/config_deve.yml
61
+ - conf/config_test.yml
59
62
  - exe/rbnotes
60
63
  - lib/rbnotes.rb
61
64
  - lib/rbnotes/commands.rb
62
65
  - lib/rbnotes/commands/add.rb
63
66
  - lib/rbnotes/commands/delete.rb
67
+ - lib/rbnotes/commands/export.rb
68
+ - lib/rbnotes/commands/help.rb
64
69
  - lib/rbnotes/commands/import.rb
65
70
  - lib/rbnotes/commands/list.rb
71
+ - lib/rbnotes/commands/search.rb
66
72
  - lib/rbnotes/commands/show.rb
67
73
  - lib/rbnotes/commands/update.rb
68
74
  - lib/rbnotes/conf.rb