cr.rb 4.0.2 → 4.1.0

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