sqlbible 1.9.0 → 1.11.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b56764d9e3209aadc3d033fdb8870ebf7b32aeed2c036527ad0211f637890db4
4
- data.tar.gz: 61562839dd855f990b6eebcafd375453e1e70e66d38574c3755cbcbb4a0c9c86
3
+ metadata.gz: 00d0eddffface8289cb8f02462cb0a0c34a39cffbf67af85fd6f9296dbde400e
4
+ data.tar.gz: 53ca66961559c13d285388d474a7ca25c5212afde1d38c4aa7c9e1fd853f7b1f
5
5
  SHA512:
6
- metadata.gz: b3b396178ca096c2d153ffb241099c0d2bfdc7981d5b4bd8a0e033fd125e1a13231c4cf1f8c85309725fcd2067e37f63f31529149b5829aadf4744d534221c6c
7
- data.tar.gz: 2e5d47096a3023c4510f44359db1683ac3e4d08ba597c2178c9d04365d14f30a7158c0f292cf965b7bb73d8ccf50a32a5bf6924e473a29ccb0a437a3edcc8318
6
+ metadata.gz: 544453d67a715d6f93ece6ad6d2275a83bc847cfafd1a3af5f35874406378cc6038d2acca2143fc9893096bd378a3492caeba86f25f9ab2dc2ea79409579ddd8
7
+ data.tar.gz: b529a999009a32d33107153ba6fca72cec02b532cdb183c0dc44e98faa1a2d221bf45e7a6d088c18644161e60ad5baf2db74ea69c1747c11f7514d2174b93022
data/bin/sqlbible CHANGED
@@ -3,118 +3,233 @@
3
3
  # frozen_string_literal: false
4
4
 
5
5
  require 'optimist'
6
+ require 'reline'
6
7
  require 'scripref'
7
8
  require 'sqlbible'
8
9
  require 'sqlbible/ansicolor'
9
10
 
10
- default_bible_param = {}
11
- if b = Sqlbible.bible
12
- default_bible_param[:default] = b.to_s
13
- end
14
-
15
- $explizit_opts = Set.new
11
+ class Cli
16
12
 
17
- $opts = Optimist.options do
18
- synopsis 'Usage: sqlbible [options] [search regexes]*'
19
- opt :bible, 'Name of the bible module filename of an .sqlbible file', type: String, **default_bible_param
20
- opt :color, 'Colorize output', default: Sqlbible.color do |v|
21
- $explizit_opts << :color
13
+ def run
14
+ parse_args
15
+ process_by_opts
16
+ rescue SystemExit
17
+ rescue Exception => e
18
+ $stderr.puts e.message
19
+ exit 1
22
20
  end
23
- opt :info, 'Show infos for installed bibles'
24
- opt :lang, 'Language for parsing and formatting scripture references', type: String, permitted: %w(de en), default: Sqlbible.lang.to_s
25
- opt :output, 'Output filename', type: String
26
- opt :pager, 'Use pager for terminal output', default: Sqlbible.pager
27
- opt :reference, 'Reference to show or search, example: "John 3:16; 10:27-30"', type: String
28
- version format("sqlbible %s\nschema version %s", Sqlbible::VERSION, Sqlbible.schema_version)
29
- end
30
21
 
31
- unless $explizit_opts.include? :color
32
- if !$stdout.tty? || $opts[:output]
33
- $opts[:color] = false
34
- end
35
- end
22
+ private
36
23
 
37
- def determine_lang_mod
38
- case l = $opts[:lang]
39
- when 'en', nil
40
- Scripref::English
41
- when 'de'
42
- Scripref::German
43
- else
44
- fail format('lang option %s is not supported', l)
24
+ def parse_args
25
+ if ARGV.empty?
26
+ reline_args
27
+ @opts = parsed_opts
28
+ else
29
+ @opts = parsed_opts
30
+ if @opts[:interactive]
31
+ reline_args
32
+ opts = parsed_opts
33
+ opts.each_pair do |k, v|
34
+ if k.to_s =~ /^(.+)_given$/
35
+ new_k = $1.to_sym
36
+ @opts[new_k] = opts[new_k]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ @regexes = ARGV.map {|s| Regexp.new(s)}
45
42
  end
46
- end
47
43
 
48
- def open_out
49
- if fn = $opts[:output]
50
- File.open(fn, 'w') do |f|
51
- yield f
44
+ def reline_args
45
+ load_history
46
+ loop do
47
+ l = Reline.readline('> ', false)&.chomp
48
+ break if l.nil? || l =~ /^$/
49
+ if l =~ /^-/
50
+ l.split(/\s+/, 2).each do |s|
51
+ ARGV << s
52
+ end
53
+ else
54
+ ARGV << l
55
+ end
56
+ if Sqlbible.history && !Reline::HISTORY.include?(l)
57
+ Reline::HISTORY << l
58
+ end
52
59
  end
53
- elsif $opts[:pager]
54
- IO.popen(Sqlbible.pager_command, 'w') do |p|
55
- yield p
60
+ rescue Interrupt
61
+ exit
62
+ ensure
63
+ write_history
64
+ end
65
+
66
+ def load_history
67
+ if Sqlbible.history && File.exist?(Sqlbible.history_file)
68
+ File.readlines(Sqlbible.history_file, chomp: true).each do |line|
69
+ Reline::HISTORY << line unless line.empty?
70
+ end
56
71
  end
57
- else
58
- yield $stdout
59
72
  end
60
- end
61
73
 
62
- def format_verse_ref f, v
63
- ref = f.format(v.scripref_passage)
64
- if $opts[:color]
65
- Ansicolor.apply(ref, Sqlbible.color_reference)
66
- else
67
- ref
74
+ def write_history
75
+ max_history = Reline::HISTORY.to_a.last(100)
76
+ return if max_history.empty?
77
+ File.write(Sqlbible.history_file, max_history.join("\n") + "\n")
68
78
  end
69
- end
70
79
 
71
- def format_text v, regexes
72
- text = v.plaintext.gsub("\n", ' ¶')
73
- if $opts[:color]
74
- regexes.each do |re|
75
- text = text.gsub(re, Ansicolor.apply('\0', Sqlbible.color_emphasize))
80
+ def parsed_opts
81
+ default_bible_param = {}
82
+ if b = Sqlbible.bible
83
+ default_bible_param[:default] = b.to_s
84
+ end
85
+ opts = Optimist.options do
86
+ synopsis 'Usage: sqlbible [options] [search regexes]*'
87
+ opt :bible, 'Name of the bible module or filename of an .sqlbible file', type: String, **default_bible_param
88
+ opt :color, 'Colorize output', default: Sqlbible.color do |v|
89
+ explizit_opts << :color
90
+ end
91
+ opt :env, 'Show infos of the sqlbible environment: installed bibles with schema versions'
92
+ opt :interactive, 'Interactive mode'
93
+ opt :lang, 'Language for parsing and formatting scripture references', type: String, permitted: %w(de en), default: Sqlbible.lang.to_s
94
+ opt :output, 'Output filename', type: String
95
+ opt :pager, 'Use pager for terminal output', default: Sqlbible.pager
96
+ opt :reference, 'Reference to show or search, example: "John 3:16; 10:27-30"', type: String
97
+ opt :stat, 'Statistics level', type: Integer, permitted: (0..2), default: Sqlbible.stat_level
98
+ version format("sqlbible %s\nschema version %s", Sqlbible::VERSION, Sqlbible.schema_version)
76
99
  end
100
+ unless opts[:color_given]
101
+ if !$stdout.tty? || opts[:output]
102
+ opts[:color] = false
103
+ end
104
+ end
105
+ opts
77
106
  end
78
- text
79
- end
80
107
 
81
- def format_verse_line f, v, regexes=[]
82
- format('%s: %s', format_verse_ref(f, v), format_text(v, regexes))
83
- end
108
+ def process_by_opts
109
+ if @opts[:env]
110
+ show_env
111
+ else
112
+ init_processing
113
+ do_processing
114
+ end
115
+ end
84
116
 
85
- begin
86
- if $opts[:info]
117
+ def show_env
87
118
  names = Sqlbible.bible_names
88
119
  max_size = names.map(&:size).max
89
120
  names.each do |n|
90
121
  puts format("%-#{max_size}s (schema version: %s)", n, Sqlbible.new(n).schema_version)
91
122
  end
92
- return
93
123
  end
94
- lang_mod = determine_lang_mod
95
- p = Scripref::Parser.new(lang_mod)
96
- f = Scripref::Formatter.new(lang_mod, abbrev_level: 1)
97
- if r = $opts[:reference]
98
- ref = p.parse(r)
99
- else
100
- ref = nil
124
+
125
+ def init_processing
126
+ m = case l = @opts[:lang]
127
+ when 'en', nil
128
+ Scripref::English
129
+ when 'de'
130
+ Scripref::German
131
+ else
132
+ fail format('lang option %s is not supported', l)
133
+ end
134
+ @parser = Scripref::Parser.new(m)
135
+ @formatter = Scripref::Formatter.new(m, abbrev_level: 1)
136
+ if r = @opts[:reference]
137
+ @reference = @parser.parse(r)
138
+ else
139
+ @reference = nil
140
+ end
141
+ @bible = Sqlbible.new(@opts[:bible])
142
+ @verse_count = 0
143
+ @matches = []
101
144
  end
102
- bible = Sqlbible.new($opts[:bible])
103
- if ARGV.size > 0
104
- regexes = ARGV.map {|s| Regexp.new(s)}
145
+
146
+ def do_processing
105
147
  open_out do |out|
106
- bible.search(regexes, range: ref).each do |v|
107
- out.puts format_verse_line(f, v, regexes)
148
+ if @regexes.size > 0
149
+ @bible.search(@regexes, range: @reference).each do |v|
150
+ process_verse out, v
151
+ end
152
+ else
153
+ @bible.reference(Array(@reference)).flatten.each do |v|
154
+ process_verse out, v
155
+ end
108
156
  end
157
+ show_stats out
109
158
  end
110
- else
111
- open_out do |out|
112
- bible.reference(Array(ref)).flatten.each do |v|
113
- out.puts format_verse_line(f, v)
159
+ end
160
+
161
+ def open_out
162
+ if fn = @opts[:output]
163
+ File.open(fn, 'w') do |f|
164
+ yield f
114
165
  end
166
+ elsif @opts[:pager]
167
+ IO.popen(Sqlbible.pager_command, 'w') do |p|
168
+ yield p
169
+ end
170
+ else
171
+ yield $stdout
172
+ end
173
+ end
174
+
175
+ def process_verse out, v
176
+ show_verse out, v
177
+ @verse_count += 1
178
+ @regexes.each do |re|
179
+ @matches += v.plaintext.scan(re)
180
+ end
181
+ end
182
+
183
+ def show_verse out, v
184
+ out.puts format_verse_line(v)
185
+ end
186
+
187
+ def format_verse_line v
188
+ format('%s: %s', format_verse_ref(v), format_text(v))
189
+ end
190
+
191
+ def format_verse_ref v
192
+ ref = @formatter.format(v.scripref_passage)
193
+ if @opts[:color]
194
+ Ansicolor.apply(ref, Sqlbible.color_reference)
195
+ else
196
+ ref
115
197
  end
116
198
  end
117
- rescue Exception => e
118
- $stderr.puts e.message
119
- exit 1
199
+
200
+ def format_text v
201
+ text = v.plaintext.gsub("\n", ' ¶')
202
+ if @opts[:color]
203
+ @regexes.each do |re|
204
+ text = text.gsub(re, Ansicolor.apply('\0', Sqlbible.color_emphasize))
205
+ end
206
+ end
207
+ text
208
+ end
209
+
210
+ def show_stats out
211
+ l = @opts[:stat]
212
+ if l < 1
213
+ return
214
+ end
215
+ if l >= 2 and !@matches.empty?
216
+ h = @matches.tally
217
+ arr = h.to_a.sort_by(&:last).reverse
218
+ unless arr.empty?
219
+ out.puts
220
+ end
221
+ arr.each do |e|
222
+ out.puts format('%s: %d', e[0], e[1])
223
+ end
224
+ if arr.size > 1
225
+ out.puts nil, format('%d total', arr.map(&:last).sum)
226
+ end
227
+ end
228
+ if @verse_count > 0
229
+ out.puts nil, format('%d verses', @verse_count)
230
+ end
231
+ end
232
+
120
233
  end
234
+
235
+ Cli.new.run
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class Sqlbible
5
- VERSION = '1.9.0'
5
+ VERSION = '1.11.0'
6
6
  end
data/lib/sqlbible.rb CHANGED
@@ -84,13 +84,16 @@ class Sqlbible
84
84
  @bibles_dir = File.join(Dir.home, '.sqlbible/bibles')
85
85
  @color_reference = :blue
86
86
  @color_emphasize = :underline
87
+ @history = true
88
+ @history_file = File.join(Dir.home, '.sqlbible/history')
87
89
  @lang = :en
88
90
  @pager_command = 'less -FRSX'
91
+ @stat_level = 1
89
92
 
90
93
  class << self
91
94
 
92
95
  attr_reader :bibles_dir
93
- attr_accessor :color, :color_reference, :color_emphasize, :bible, :lang, :pager, :pager_command
96
+ attr_accessor :bible, :color, :color_emphasize, :color_reference, :history, :history_file, :lang, :pager, :pager_command, :stat_level
94
97
 
95
98
  # Get the database schema as string
96
99
  def db_schema
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlbible
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Friedrich
@@ -99,28 +99,28 @@ dependencies:
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: 2.5.0
102
+ version: '2.5'
103
103
  type: :development
104
104
  prerelease: false
105
105
  version_requirements: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: 2.5.0
109
+ version: '2.5'
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: test-unit
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: 3.7.0
116
+ version: '3.7'
117
117
  type: :development
118
118
  prerelease: false
119
119
  version_requirements: !ruby/object:Gem::Requirement
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: 3.7.0
123
+ version: '3.7'
124
124
  email: janfri26@gmail.com
125
125
  executables:
126
126
  - sqlbible