cr.rb 4.0.3 → 4.1.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/cr +418 -646
  3. data/lib/cr/version.rb +16 -0
  4. data/lib/cr.rb +235 -14
  5. metadata +5 -4
data/lib/cr/version.rb ADDED
@@ -0,0 +1,16 @@
1
+ # ---------------------------------------------------------------
2
+ # File : version.rb
3
+ # Authors : ccmywish <ccmywish@qq.com>
4
+ # Created on : <2023-02-12>
5
+ # Last modified : <2023-02-13>
6
+ #
7
+ # version:
8
+ #
9
+ # Just for gemspec to quickly get version
10
+ # ---------------------------------------------------------------
11
+
12
+ module CrypticResolver
13
+
14
+ GEM_VERSION = "4.1.1"
15
+
16
+ end
data/lib/cr.rb CHANGED
@@ -2,30 +2,251 @@
2
2
  # File : cr.rb
3
3
  # Authors : ccmywish <ccmywish@qq.com>
4
4
  # Created on : <2022-04-15>
5
- # Last modified : <2023-02-05>
5
+ # Last modified : <2023-02-13>
6
6
  #
7
7
  # cr:
8
8
  #
9
9
  # This file is the lib of `cr.rb``
10
- #
11
10
  # ------------------------------------------------------
12
11
 
13
- CR_GEM_VERSION = "4.0.3"
12
+ require 'cr/version'
13
+ # require_relative "#{__dir__}/cr/version"
14
+
15
+ module CrypticResolver
16
+
17
+ module Color
18
+ def bold(str) "\e[1m#{str}\e[0m" end
19
+ def underline(str) "\e[4m#{str}\e[0m" end
20
+ def red(str) "\e[31m#{str}\e[0m" end
21
+ def green(str) "\e[32m#{str}\e[0m" end
22
+ def yellow(str) "\e[33m#{str}\e[0m" end
23
+ def blue(str) "\e[34m#{str}\e[0m" end
24
+ def purple(str) "\e[35m#{str}\e[0m" end
25
+ def cyan(str) "\e[36m#{str}\e[0m" end
26
+ end
27
+
28
+ end
29
+
30
+
31
+ class CrypticResolver::Resolver
32
+
33
+ include CrypticResolver::Color
14
34
 
35
+ require 'tomlrb'
36
+ require 'fileutils'
15
37
 
16
- CR_OFFICIAL_DICTS = <<-EOF
17
- Default:
18
- common computer windows
19
- linux technology
38
+ # attr_accessor :default_dicts # Default dictionaries lists
20
39
 
21
- Field:
22
- electronics economy medicine
23
- mechanical science math
40
+ DEFAULT_LIB_PATH = File.expand_path("~/.cryptic-resolver")
24
41
 
25
- Specific:
42
+ ORIGINAL_DEFAULT_DICTS = [
43
+ "https://github.com/cryptic-resolver/cryptic_common.git",
44
+ "https://github.com/cryptic-resolver/cryptic_computer.git",
45
+ "https://github.com/cryptic-resolver/cryptic_windows.git",
46
+ "https://github.com/cryptic-resolver/cryptic_linux.git",
47
+ "https://github.com/cryptic-resolver/cryptic_technology"
48
+ ]
49
+
50
+ extend CrypticResolver::Color
51
+
52
+ RECOMMENDED_DICTS = <<~EOF
53
+ #{yellow("Default:")}
54
+ common computer windows
55
+ linux technology
56
+
57
+ #{yellow("Field:")}
58
+ electronics economy medicine
59
+ mechanical science math
60
+
61
+ #{yellow("Specific:")}
26
62
  dos x86 signal
27
63
 
28
- Feature:
29
- ccmywish/CRuby-Source-Code-Dictionary
64
+ #{yellow("Feature:")}
65
+ ccmywish/CRuby-Source-Code-Dictionary
66
+ ccmywish/mruby-Source-Code-Dictionary
67
+
68
+ EOF
69
+
70
+
71
+
72
+ attr_accessor :def_dicts, # default dictionaries lists
73
+ :extra_lib_path, # Extra library path
74
+ :counter # word counter
75
+
76
+ def initialize
77
+ @counter = Counter.new(self)
78
+
79
+ # if user doesn't specify, we use the hard-coded defaults
80
+ @def_dicts = ORIGINAL_DEFAULT_DICTS
81
+
82
+ # The config file will override the default dicts, but not default library!
83
+ if file = ENV['CRYPTIC_RESOLVER_CONFIG']
84
+
85
+ if !test('f', file)
86
+ abort "FATAL: Your CRYPTIC_RESOLVER_CONFIG is NOT a file!"
87
+ end
88
+
89
+ config = Tomlrb.load_file file
90
+
91
+ if ret = config['DEFAULT_DICTS']
92
+ @def_dicts = ret
93
+ end
94
+
95
+ @extra_lib_path ||= config['EXTRA_LIBRARY']
96
+ # Prevent runtime error
97
+ if ! test('d', @extra_lib_path)
98
+ abort "FATAL: Your CRYPTIC_RESOLVER_CONFIG's option 'EXTRA_LIBRARY' is NOT a legal directory!"
99
+ end
100
+ else
101
+ @extra_lib_path = nil
102
+ end
103
+
104
+ # Same with the pulled repo dirs' names in DEFAULT_LIB_PATH
105
+ # @def_dicts_names = @def_dicts.map do |e|
106
+ # e.split('/').last.split('.').first
107
+ # end
108
+
109
+
110
+ end
111
+
112
+
113
+ def is_there_any_dict?
114
+ # Ensure the cr home dir exists
115
+ FileUtils.mkdir_p(DEFAULT_LIB_PATH)
116
+ #unless Dir.exist? DEFAULT_LIB_PATH
117
+ # Dir.mkdir DEFAULT_LIB_PATH
118
+ #end
119
+ !Dir.empty? DEFAULT_LIB_PATH
120
+ end
121
+
122
+
123
+ def add_default_dicts_if_none_exists
124
+ unless is_there_any_dict?
125
+ puts "cr: Adding default dicts..."
126
+
127
+ # This is used to display what you are pulling when adding dicts
128
+ dicts_user_and_names = @def_dicts.map do |e|
129
+ user, repo = e.split('/').last(2)
130
+ repo = repo.split('.').first
131
+ user + '/' + repo
132
+ end
133
+
134
+ begin
135
+ if Gem.win_platform?
136
+ # Windows doesn't have fork
137
+ dicts_user_and_names.each_with_index do |name, i|
138
+ puts "cr: Pulling #{name}..."
139
+ `git -C #{DEFAULT_LIB_PATH} clone #{@def_dicts[i]} -q`
140
+ end
141
+ else
142
+ # *nix-like
143
+ dicts_user_and_names.each_with_index do |name, i|
144
+ fork do
145
+ puts "cr: Pulling #{name}..."
146
+ `git -C #{DEFAULT_LIB_PATH} clone #{@def_dicts[i]} -q`
147
+ end
148
+ end
149
+ Process.waitall
150
+ end
151
+
152
+ rescue Interrupt
153
+ abort "cr: Cancel add default dicts"
154
+ end
155
+
156
+ puts "cr: Add done" ; word_count
157
+ puts ; puts "#{$DefaultLibWordCount} words added"
158
+
159
+ # Really added
160
+ return true
161
+ end
162
+ # Not added
163
+ return false
164
+ end
165
+
166
+
167
+ end
168
+
169
+
170
+ class CrypticResolver::Resolver::Counter
171
+
172
+ include CrypticResolver::Color
173
+
174
+ attr_accessor :word_count_of_two_libs, # def_lib + extra_lib
175
+ :word_count_of_def_lib,
176
+ :word_count_of_extra_lib,
177
+
178
+ :resolver
179
+
180
+ def initialize(resolver)
181
+ @word_count_of_two_libs = 0
182
+ @word_count_of_def_lib = 0
183
+ @word_count_of_extra_lib = 0
184
+ @resolver = resolver
185
+ end
186
+
187
+
188
+ def count_dict_words(library, dict)
189
+ dict_dir = library + "/#{dict}"
190
+ wc = 0
191
+ Dir.children(dict_dir).each do |entry|
192
+ next if File.file? entry
193
+ next unless entry.end_with?('.toml')
194
+ sheet_content = @resolver.load_sheet(library, dict, entry.delete_suffix('.toml'))
195
+ count = sheet_content.keys.count
196
+
197
+ wc = wc + count
198
+ end
199
+ return wc
200
+ end
201
+
202
+
203
+ # Count default library
204
+ def count_def_lib(display: )
205
+ default_lib = Dir.children(CrypticResolver::Resolver::DEFAULT_LIB_PATH)
206
+ unless default_lib.empty?
207
+ puts bold(green("Default library: ")) if display
208
+ default_lib.each do |s|
209
+ next if File.file? s
210
+ wc = count_dict_words(CrypticResolver::Resolver::DEFAULT_LIB_PATH,s)
211
+ @word_count_of_def_lib += wc
212
+ # With color, ljust not works, so we disable color
213
+ puts("#{wc.to_s.rjust(5)} #{s}") if display
214
+ end
215
+ end
216
+ return @word_count_of_def_lib
217
+ end
218
+
219
+
220
+ # Count extra library
221
+ def count_extra_lib(display: )
222
+ if path = @resolver.extra_lib_path
223
+ extra_lib = Dir.children(path)
224
+ unless extra_lib.empty?
225
+ wc = 0
226
+ puts(bold(green("\nExtra library:"))) if display
227
+ extra_lib.each do |s|
228
+ next if File.file? s
229
+ wc = count_dict_words(path,s)
230
+ @word_count_of_extra_lib += wc
231
+ puts("#{wc.to_s.rjust(5)} #{s}") if display
232
+ end
233
+ end
234
+ end
235
+ return @word_count_of_extra_lib
236
+ end
237
+
238
+
239
+ def count!(display: )
240
+ count_def_lib(display: display)
241
+ count_extra_lib(display: display)
242
+ @word_count_of_two_libs = @word_count_of_def_lib + @word_count_of_extra_lib
243
+
244
+ if display
245
+ puts
246
+ puts "#{@word_count_of_def_lib.to_s.rjust(4)} words in Default library"
247
+ puts "#{@word_count_of_extra_lib.to_s.rjust(4) } words in Extra library"
248
+ puts "#{@word_count_of_two_libs.to_s.rjust(4) } words altogether"
249
+ end
250
+ end
30
251
 
31
- EOF
252
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cr.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.3
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - ccmywish
7
+ - Aoran Zeng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-05 00:00:00.000000000 Z
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -49,6 +49,7 @@ extra_rdoc_files: []
49
49
  files:
50
50
  - bin/cr
51
51
  - lib/cr.rb
52
+ - lib/cr/version.rb
52
53
  homepage: https://github.com/cryptic-resolver/cr.rb
53
54
  licenses:
54
55
  - MIT
@@ -70,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  - !ruby/object:Gem::Version
71
72
  version: '0'
72
73
  requirements: []
73
- rubygems_version: 3.3.26
74
+ rubygems_version: 3.4.1
74
75
  signing_key:
75
76
  specification_version: 4
76
77
  summary: 'cr: Cryptic Resolver'