cr.rb 3.21.0 → 4.0.0.RC.1

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 +198 -139
  3. data/lib/cr.rb +18 -3
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e25ab95484c9ca0c70ca4ebbbdf0c6f01f975930e66c552424f5ba12a9e2a1f4
4
- data.tar.gz: ebf0410ef786f36f870fa2289d764593a1e50778e192861e87048fa01fdd4c1e
3
+ metadata.gz: 5ce431672d430390ccfd417228f9c503c2d76ff8b34ac6b5f9af767f6412fcaa
4
+ data.tar.gz: 51aa033e497ffa0a6e4c56cf29430951ddb756d1b21511b62cfa3dbd97719681
5
5
  SHA512:
6
- metadata.gz: 4a7aed4be85c661e46c58d326755de0e7d1a249a3e71834895fabee604f0a63a7ea6261f0fd9384247028fcdf51f879c17c28243c4ae5ed118311dc3b171dea5
7
- data.tar.gz: ae53f3a4ea09ba563693a781b96dd21dee0f3d5ded483db2c05ff0a07f5ddd02ebe6520fa70683aeb6eb1b3595fff122ec6ce4ceedc797c2f36e2d125dc1a832
6
+ metadata.gz: 6d8ddb4c90f742da36e3602444e1fc146080b6f73419d5688496d5414ffb0f54838f2ee6d3eeec3cce36cdf4bcb8d7ca46ac44f527772f54f19b5e0f05db795c
7
+ data.tar.gz: '029a468f7a80d22b1596be5a284f7e0e98cdf886b9543434720e9f3ad454bfcef29cfe8ddad3f8e66195d4c151ddf7f41ce0d7aa699916d36efb7e17af92178d'
data/bin/cr CHANGED
@@ -4,7 +4,7 @@
4
4
  # File : cr.rb
5
5
  # Authors : ccmywish <ccmywish@qq.com>
6
6
  # Created on : <2021-07-08>
7
- # Last modified : <2022-10-30>
7
+ # Last modified : <2022-11-27>
8
8
  #
9
9
  # cr:
10
10
  #
@@ -18,14 +18,62 @@ require 'cr'
18
18
  require 'tomlrb'
19
19
  require 'fileutils'
20
20
 
21
- CRYPTIC_RESOLVER_HOME = File.expand_path("~/.cryptic-resolver")
22
- CRYPTIC_DEFAULT_DICTS = {
23
- common: "https://github.com/cryptic-resolver/cryptic_common.git",
24
- computer: "https://github.com/cryptic-resolver/cryptic_computer.git",
25
- windows: "https://github.com/cryptic-resolver/cryptic_windows.git",
26
- linux: "https://github.com/cryptic-resolver/cryptic_linux.git",
27
- electronics: "https://github.com/cryptic-resolver/cryptic_electronics.git"
28
- }
21
+ CR_DEFAULT_LIBRARY = File.expand_path("~/.cryptic-resolver")
22
+
23
+ $CR_DEFAULT_DICTS = [
24
+ "https://github.com/cryptic-resolver/cryptic_common.git",
25
+ "https://github.com/cryptic-resolver/cryptic_computer.git",
26
+ "https://github.com/cryptic-resolver/cryptic_windows.git",
27
+ "https://github.com/cryptic-resolver/cryptic_linux.git",
28
+ "https://github.com/cryptic-resolver/cryptic_electronics"
29
+ ]
30
+
31
+ # The config file will override the default dicts, but not default library!
32
+ if ENV['CRYPTIC_RESOLVER_CONFIG']
33
+ file = ENV['CRYPTIC_RESOLVER_CONFIG']
34
+ if test('f', file)
35
+ config = Tomlrb.load_file file
36
+
37
+ CR_EXTRA_LIBRARY ||= config['EXTRA_LIBRARY']
38
+
39
+ if config['DEFAULT_DICTS']
40
+ CR_DEFAULT_DICTS = config['DEFAULT_DICTS']
41
+ else
42
+ CR_DEFAULT_DICTS = $CR_DEFAULT_DICTS
43
+ end
44
+
45
+ else
46
+ puts "FATAL: Your CRYPTIC_RESOLVER_CONFIG is NOT a file!"
47
+ exit 1
48
+ end
49
+
50
+ else
51
+ # if user doesn't specify, we use the hard-coded defaults
52
+ CR_EXTRA_LIBRARY = nil
53
+ CR_DEFAULT_DICTS = $CR_DEFAULT_DICTS
54
+ end
55
+
56
+
57
+ # Prevent runtime error
58
+ if not CR_EXTRA_LIBRARY.nil?
59
+ if ! test('d', CR_EXTRA_LIBRARY)
60
+ puts "FATAL: Your CRYPTIC_RESOLVER_CONFIG's option 'EXTRA_LIBRARY' is NOT a legal directory!"
61
+ exit 1
62
+ end
63
+ end
64
+
65
+
66
+ # This is used to display what you are pulling when adding dicts
67
+ CR_DEFAULT_DICTS_USER_AND_NAMES = CR_DEFAULT_DICTS.map do |e|
68
+ user, repo = e.split('/').last(2)
69
+ repo = repo.split('.').first
70
+ user + '/' + repo
71
+ end
72
+
73
+ # Same with the pulled repo dirs' names in CR_DEFAULT_LIBRARY
74
+ CR_DEFAULT_DICTS_NAMES = CR_DEFAULT_DICTS.map do |e|
75
+ e.split('/').last.split('.').first
76
+ end
29
77
 
30
78
 
31
79
  ####################
@@ -47,31 +95,31 @@ def cyan(str) "\e[36m#{str}\e[0m" end
47
95
  ####################
48
96
 
49
97
  def is_there_any_dict?
50
- unless Dir.exist? CRYPTIC_RESOLVER_HOME
51
- Dir.mkdir CRYPTIC_RESOLVER_HOME
98
+ unless Dir.exist? CR_DEFAULT_LIBRARY
99
+ Dir.mkdir CR_DEFAULT_LIBRARY
52
100
  end
53
101
 
54
- !Dir.empty? CRYPTIC_RESOLVER_HOME
102
+ !Dir.empty? CR_DEFAULT_LIBRARY
55
103
  end
56
104
 
57
105
 
58
106
  def add_default_dicts_if_none_exists
59
107
  unless is_there_any_dict?
60
- puts "cr: Adding default dictionaries..."
108
+ puts "cr: Adding default dicts..."
61
109
 
62
110
  begin
63
111
  if RUBY_PLATFORM.include? "mingw"
64
112
  # Windows doesn't have fork
65
- CRYPTIC_DEFAULT_DICTS.each do |key, dict|
66
- puts "cr: Pulling cryptic_#{key}..."
67
- `git -C #{CRYPTIC_RESOLVER_HOME} clone #{dict} -q`
113
+ CR_DEFAULT_DICTS_USER_AND_NAMES.each_with_index do |name, i|
114
+ puts "cr: Pulling #{name}..."
115
+ `git -C #{CR_DEFAULT_LIBRARY} clone #{CR_DEFAULT_DICTS[i]} -q`
68
116
  end
69
117
  else
70
- # *nix
71
- CRYPTIC_DEFAULT_DICTS.each do |key, dict|
118
+ # *nix-like
119
+ CR_DEFAULT_DICTS_USER_AND_NAMES.each_with_index do |name, i|
72
120
  fork do
73
- puts "cr: Pulling cryptic_#{key}..."
74
- `git -C #{CRYPTIC_RESOLVER_HOME} clone #{dict} -q`
121
+ puts "cr: Pulling #{name}..."
122
+ `git -C #{CR_DEFAULT_LIBRARY} clone #{CR_DEFAULT_DICTS[i]} -q`
75
123
  end
76
124
  end
77
125
  Process.waitall
@@ -85,7 +133,7 @@ def add_default_dicts_if_none_exists
85
133
  puts "cr: Add done"
86
134
  word_count(p: false)
87
135
  puts
88
- puts "#{$WordCount} words added"
136
+ puts "#{$TwoLibWordCount} words added"
89
137
 
90
138
  # Really added
91
139
  return true
@@ -95,26 +143,27 @@ def add_default_dicts_if_none_exists
95
143
  end
96
144
 
97
145
 
146
+ # Notice that we only update the Default library, not Extra library
98
147
  def update_dicts()
99
148
  return if add_default_dicts_if_none_exists
100
149
 
101
150
  word_count(p: false)
102
- old_wc = [$DefaultWordCount, $WordCount-$DefaultWordCount, $WordCount]
151
+ old_wc = $DefaultLibWordCount
103
152
 
104
- puts "cr: Updating all dictionaries..."
153
+ puts "cr: Updating all dicts in Default library..."
105
154
 
106
155
  begin
107
- Dir.chdir CRYPTIC_RESOLVER_HOME do
156
+ Dir.chdir CR_DEFAULT_LIBRARY do
108
157
 
109
158
  if RUBY_PLATFORM.include? "mingw"
110
159
  # Windows doesn't have fork
111
- Dir.children(CRYPTIC_RESOLVER_HOME).each do |dict|
160
+ Dir.children(CR_DEFAULT_LIBRARY).each do |dict|
112
161
  puts "cr: Wait to update #{dict}..."
113
162
  `git -C ./#{dict} pull -q`
114
163
  end
115
164
  else
116
165
  # *nix
117
- Dir.children(CRYPTIC_RESOLVER_HOME).each do |dict|
166
+ Dir.children(CR_DEFAULT_LIBRARY).each do |dict|
118
167
  fork do
119
168
  puts "cr: Wait to update #{dict}..."
120
169
  `git -C ./#{dict} pull -q`
@@ -130,21 +179,17 @@ def update_dicts()
130
179
  exit 1
131
180
  end
132
181
 
133
-
134
182
  puts "cr: Update done"
135
183
 
136
184
  # clear
137
- $DefaultWordCount, $WordCount = 0, 0
185
+ $DefaultLibWordCount = 0
138
186
  # recount
139
187
  word_count(p: false)
140
- new_wc = [$DefaultWordCount, $WordCount-$DefaultWordCount, $WordCount]
141
- diff = []
142
- new_wc.each_with_index do
143
- diff[_2] = _1 - old_wc[_2]
144
- end
188
+
189
+ new_wc = $DefaultLibWordCount
145
190
 
146
191
  puts
147
- puts "#{diff.[]2} words added: default/#{diff.[]0} user/#{diff.[]1}"
192
+ puts "#{new_wc - old_wc} words added in Default library"
148
193
 
149
194
  end
150
195
 
@@ -156,16 +201,20 @@ def add_dict(repo)
156
201
  end
157
202
 
158
203
  # Ensure the cr home dir exists
159
- FileUtils.mkdir_p(CRYPTIC_RESOLVER_HOME)
204
+ FileUtils.mkdir_p(CR_DEFAULT_LIBRARY)
160
205
 
161
206
  # Simplify adding dictionary
162
207
  if !repo.start_with?("https://") and !repo.start_with?("git@")
163
- repo = "https://github.com/#{repo}.git"
208
+ if repo.include?('/')
209
+ repo = "https://github.com/#{repo}.git"
210
+ else
211
+ repo = "https://github.com/cryptic-resolver/cryptic_#{repo}.git"
212
+ end
164
213
  end
165
214
 
166
215
  begin
167
216
  puts "cr: Adding new dictionary..."
168
- `git -C #{CRYPTIC_RESOLVER_HOME} clone #{repo} -q`
217
+ `git -C #{CR_DEFAULT_LIBRARY} clone #{repo} -q`
169
218
  rescue Interrupt
170
219
  puts "cr: Cancel add dict"
171
220
  exit 1
@@ -177,7 +226,7 @@ def add_dict(repo)
177
226
  dict = repo.split('/')[-1].delete_suffix('.git')
178
227
  count_dict_words(dict)
179
228
  puts
180
- puts "#$WordCount words added"
229
+ puts "#$TwoLibWordCount words added"
181
230
 
182
231
  end
183
232
 
@@ -187,7 +236,7 @@ def del_dict(repo)
187
236
  puts bold(red("cr: Need an argument!"))
188
237
  exit -1
189
238
  end
190
- Dir.chdir CRYPTIC_RESOLVER_HOME do
239
+ Dir.chdir CR_DEFAULT_LIBRARY do
191
240
  begin
192
241
  # Dir.rmdir repo # Can't rm a filled dir
193
242
  # FileUtils.rmdir repo # Can't rm a filled dir
@@ -195,14 +244,14 @@ def del_dict(repo)
195
244
  puts "cr: Delete dictionary #{bold(green(repo))} done"
196
245
  rescue Exception => e
197
246
  puts bold(red("cr: #{e}"))
198
- list_dictionaries
247
+ list_dicts
199
248
  end
200
249
  end
201
250
  end
202
251
 
203
252
 
204
- def load_sheet(dict, sheet_name)
205
- file = CRYPTIC_RESOLVER_HOME + "/#{dict}/#{sheet_name}.toml"
253
+ def load_sheet(library, dict, sheet_name)
254
+ file = library + "/#{dict}/#{sheet_name}.toml"
206
255
 
207
256
  if File.exist? file
208
257
  return Tomlrb.load_file file # gem 'tomlrb'
@@ -252,7 +301,7 @@ def pp_info(info)
252
301
  puts
253
302
  end
254
303
 
255
- # Print default cryptic_ dictionaries
304
+ # Print default cryptic_ dicts
256
305
  def pp_dict(dict)
257
306
  puts green("From: #{dict}")
258
307
  end
@@ -273,14 +322,14 @@ end
273
322
  # # category
274
323
  #
275
324
  # [blah]
276
- # same = "XDG downloader <=>xdg.Download" # this is correct
325
+ # same = "XDG downloader =>xdg.Download" # this is correct
277
326
  #
278
327
  # [blah]
279
328
  # name = "BlaH" # You may want to display a better name first
280
- # same = "XDG downloader <=>xdg.Download" # this is correct
329
+ # same = "XDG downloader =>xdg.Download" # this is correct
281
330
  #
282
331
  #
283
- def pp_same_info(dict, word, cache_content, same_key, own_name)
332
+ def pp_same_info(library, dict, word, cache_content, same_key, own_name)
284
333
 
285
334
  # If it's a synonym for anther word,
286
335
  # we should lookup into this dict again, but maybe with a different file
@@ -299,13 +348,13 @@ def pp_same_info(dict, word, cache_content, same_key, own_name)
299
348
  #
300
349
  # 'jump to' will output to user, so this is important not only inside our sheet.
301
350
  #
302
- # same = "XDM downloader<=>xdm.Download"
351
+ # same = "XDM downloader=>xdm.Download"
303
352
  #
304
- # We split 'same' key into two parts via spaceship symbol <=>, first part will
353
+ # We split 'same' key into two parts via spaceship symbol `=>`, first part will
305
354
  # output to user, the second part is for internal jump.
306
355
  #
307
356
 
308
- jump_to, same = same_key.split("<=>")
357
+ jump_to, same = same_key.split("=>")
309
358
  same = jump_to if same.nil?
310
359
 
311
360
  unless own_name
@@ -337,7 +386,7 @@ def pp_same_info(dict, word, cache_content, same_key, own_name)
337
386
  # No need to load another dictionary if match
338
387
  sheet_content = cache_content
339
388
  else
340
- sheet_content = load_sheet(dict, same.chr.downcase)
389
+ sheet_content = load_sheet(library, dict, same.chr.downcase)
341
390
  end
342
391
 
343
392
  if category.nil?
@@ -354,7 +403,7 @@ def pp_same_info(dict, word, cache_content, same_key, own_name)
354
403
  # double or more jumps
355
404
  elsif same_key = info['same']
356
405
  own_name = info['name']
357
- return pp_same_info(dict, same, cache_content, same_key, own_name)
406
+ return pp_same_info(library, dict, same, cache_content, same_key, own_name)
358
407
  else
359
408
  pp_info(info)
360
409
  return true
@@ -376,8 +425,8 @@ end
376
425
  # 2.2 with category specifier
377
426
  # [abcd.tYPe]
378
427
  #
379
- def lookup(dict, file, word)
380
- sheet_content = load_sheet(dict, file)
428
+ def lookup(library, dict, file, word)
429
+ sheet_content = load_sheet(library, dict, file)
381
430
  return false if sheet_content.nil?
382
431
 
383
432
  info = sheet_content[word]
@@ -392,7 +441,6 @@ def lookup(dict, file, word)
392
441
  end
393
442
 
394
443
 
395
-
396
444
  # Word with no category specifier
397
445
  # We call this meaning as type 1
398
446
  type_1_exist_flag = false
@@ -404,7 +452,7 @@ def lookup(dict, file, word)
404
452
  if same_key = info['same']
405
453
  own_name = info['name']
406
454
  pp_dict(dict)
407
- pp_same_info(dict, word, sheet_content, same_key, own_name)
455
+ pp_same_info(library, dict, word, sheet_content, same_key, own_name)
408
456
  # It's also a type 1
409
457
  type_1_exist_flag = true
410
458
  is_jump = true
@@ -441,7 +489,7 @@ def lookup(dict, file, word)
441
489
  info0 = sheet_content[word][meaning]
442
490
  if same_key = info0['same']
443
491
  own_name = info0['name']
444
- pp_same_info(dict, word, sheet_content, same_key, own_name)
492
+ pp_same_info(library, dict, word, sheet_content, same_key, own_name)
445
493
  else
446
494
  pp_info(info0)
447
495
  end
@@ -460,15 +508,16 @@ end
460
508
 
461
509
  #
462
510
  # The main procedure of `cr`
463
- # 1. Search the default's first dict first
464
- # 2. Search the rest dictionaries in the cryptic dictionaries default dir
511
+ #
512
+ # 1. Search the default library first
513
+ # 2. Search the extra library if it does exist
465
514
  #
466
515
  # The `search` is done via the `lookup` function. It will print
467
516
  # the info while finding. If `lookup` always return false then
468
- # means lacking of this word in our dictionaries. So a welcomed
517
+ # means lacking of this word in our dicts. So a welcomed
469
518
  # contribution is printed on the screen.
470
519
  #
471
- def solve_word(word)
520
+ def resolve_word(word)
472
521
 
473
522
  add_default_dicts_if_none_exists
474
523
 
@@ -477,23 +526,24 @@ def solve_word(word)
477
526
  index = word.chr
478
527
  case index
479
528
  when '0'..'9'
480
- index = '0123456789'
529
+ index = '0-9'
481
530
  end
482
-
483
- # Default's first should be 1st to consider
484
- first_dict = "cryptic_" + CRYPTIC_DEFAULT_DICTS.keys[0].to_s # When Ruby3, We can use DICTS.key(0)
485
531
 
486
- # cache lookup results
532
+ # cache lookup's results
487
533
  results = []
488
- results << lookup(first_dict,index,word)
489
- # return if result == true # We should consider all dicts
490
-
491
- # Then else
492
- rest = Dir.children(CRYPTIC_RESOLVER_HOME)
493
- rest.delete first_dict
494
- rest.each do |dict|
495
- results << lookup(dict,index,word)
496
- # continue if result == false # We should consider all dicts
534
+
535
+ # First consider the default library
536
+ default = Dir.children(CR_DEFAULT_LIBRARY)
537
+ default.each do |dict|
538
+ results << lookup(CR_DEFAULT_LIBRARY,dict,index,word)
539
+ end
540
+
541
+ # Then is the extra library
542
+ if CR_EXTRA_LIBRARY
543
+ extra = Dir.children(CR_EXTRA_LIBRARY)
544
+ extra.each do |dict|
545
+ results << lookup(CR_EXTRA_LIBRARY,dict,index,word)
546
+ end
497
547
  end
498
548
 
499
549
  unless results.include? true
@@ -519,7 +569,7 @@ end
519
569
 
520
570
 
521
571
  #
522
- # The search word process is quite like `solve_word``
572
+ # This `search_word` routine is quite like `resolve_word`
523
573
  # Notice:
524
574
  # We handle two cases
525
575
  #
@@ -546,10 +596,10 @@ def search_word(pattern)
546
596
  found = false
547
597
 
548
598
  #
549
- # Try to match every word in all dictionaries
599
+ # Try to match every word in all dicts
550
600
  #
551
- Dir.children(CRYPTIC_RESOLVER_HOME).each do |dict|
552
- sheets = Dir.children(File.join(CRYPTIC_RESOLVER_HOME, dict)).select do
601
+ Dir.children(CR_DEFAULT_LIBRARY).each do |dict|
602
+ sheets = Dir.children(File.join(CR_DEFAULT_LIBRARY, dict)).select do
553
603
  _1.end_with?('.toml')
554
604
  end
555
605
 
@@ -585,19 +635,21 @@ end
585
635
 
586
636
  def help
587
637
  word_count(p: false)
588
- user_words = $WordCount - $DefaultWordCount
589
638
  puts <<-HELP
590
- cr: Cryptic Resolver v#{CR_GEM_VERSION} (#{$WordCount} words: default/#{$DefaultWordCount} user/#{user_words})
639
+ cr: Cryptic Resolver v#{CR_GEM_VERSION} (#{$TwoLibWordCount} words: Default lib/#{$DefaultLibWordCount} && Extra lib/#{$ExtraLibWordCount})
591
640
 
592
- usage:
641
+ Usage:
593
642
 
594
- cr emacs => Edit macros: a feature-rich editor
643
+ cr emacs => Edit macros: A feature-rich editor
595
644
  cr -c => Print word count
596
- cr -l => List local dictionaries
597
- cr -u => Update all dictionaries
598
- cr -a repo.git => Add a new dictionary
599
- cr -a user/repo => Add a new dictionary on Github
600
- cr -d cryptic_xx => Delete a dictionary
645
+ cr -l => List local dicts and official dicts
646
+ cr -u => Update all dicts in default library
647
+
648
+ cr -a repo.git => Add a new dict
649
+ cr -a user/repo => Add a new dict from Github
650
+ cr -a reponame => Add an official dict from Github
651
+
652
+ cr -d cryptic_xx => Delete a dict
601
653
  cr -s pattern => Search words matched with pattern
602
654
  cr -v => Print version
603
655
  cr -h => Print this help
@@ -614,91 +666,98 @@ def print_version
614
666
  end
615
667
 
616
668
 
617
- def list_dictionaries
618
- Dir.chdir CRYPTIC_RESOLVER_HOME do
619
- Dir.children(CRYPTIC_RESOLVER_HOME).each_with_index do |dict,i|
669
+ #
670
+ # 1. List Default library's dicts
671
+ # 2. List Extra library's dicts
672
+ # 3. List official dicts
673
+ #
674
+ def list_dicts
675
+ Dir.chdir CR_DEFAULT_LIBRARY do
676
+ puts blue("=> Default library: #{CR_DEFAULT_LIBRARY}")
677
+ Dir.children(CR_DEFAULT_LIBRARY).each_with_index do |dict,i|
620
678
  puts "#{blue(i+1)}. #{bold(green(dict))}"
621
679
  end
622
680
  end
681
+
682
+ if CR_EXTRA_LIBRARY
683
+ puts
684
+ Dir.chdir CR_EXTRA_LIBRARY do
685
+ puts blue("=> Extra library: #{CR_EXTRA_LIBRARY}")
686
+ Dir.children(CR_EXTRA_LIBRARY).each_with_index do |dict,i|
687
+ puts "#{blue(i+1)}. #{bold(green(dict))}"
688
+ end
689
+ end
690
+ end
691
+
692
+ puts
693
+ puts blue("=> Official dicts: (Add it by 'cr -a xxx')")
694
+ puts CR_OFFICIAL_DICTS
695
+
623
696
  end
624
697
 
625
698
 
626
- # All dictionaries word count
627
- $WordCount = 0
628
- # Default dictionaries word count
629
- $DefaultWordCount = 0
699
+ # Two libraries word count (Default library + Extra library)
700
+ $TwoLibWordCount = 0
701
+ # Default library word count
702
+ $DefaultLibWordCount = 0
703
+ # Extra library word count
704
+ $ExtraLibWordCount = 0
630
705
 
631
- def count_dict_words(dict)
706
+ def count_dict_words(library, dict)
632
707
 
633
- dict_dir = CRYPTIC_RESOLVER_HOME + "/#{dict}"
708
+ dict_dir = library + "/#{dict}"
634
709
 
635
710
  wc = 0
636
711
 
637
712
  Dir.children(dict_dir).each do |entry|
638
713
  next unless entry.end_with?('.toml')
639
- sheet_content = load_sheet(dict, entry.delete_suffix('.toml'))
714
+ sheet_content = load_sheet(library, dict, entry.delete_suffix('.toml'))
640
715
  count = sheet_content.keys.count
641
-
642
- # puts "#{entry}: #{count}"
716
+
643
717
  wc = wc + count
644
718
  end
645
-
646
- $WordCount += wc
647
-
648
719
  return wc
649
-
650
720
  end
651
721
 
652
722
 
653
723
  def word_count(p:)
654
-
655
724
  # Always check before Dir.children (this method creates dir if not exists)
656
725
  is_there_any_dict?
657
726
 
658
- # real dicts in user's directory
659
- locals = []
660
- Dir.children(CRYPTIC_RESOLVER_HOME).each do |dict|
661
- locals << dict
662
- end
727
+ default_lib = Dir.children(CR_DEFAULT_LIBRARY)
663
728
 
664
- # pre-defined default
665
- defaults = CRYPTIC_DEFAULT_DICTS.keys.map do |s|
666
- "cryptic_#{s}"
667
- end
668
-
669
- # user may delete some default dicts
670
- defaults &= locals
671
-
672
- unless defaults.empty?
673
- puts(bold(green("Default dicts: "))) if p
674
- defaults.each do |s|
675
- wc = count_dict_words(s)
676
- $DefaultWordCount += wc
729
+ # Count default library
730
+ unless default_lib.empty?
731
+ puts(bold(green("Default library: "))) if p
732
+ default_lib.each do |s|
733
+ wc = count_dict_words(CR_DEFAULT_LIBRARY,s)
734
+ $DefaultLibWordCount += wc
677
735
  # With color, ljust not works, so we disable color
678
736
  puts(" #{s.ljust(17)}: #{wc}") if p
679
737
  end
680
738
  end
681
739
 
682
- users = locals - defaults
683
- user_words = 0
684
- unless users.empty?
685
- wc = 0
686
- puts(bold(blue("\nUser's dict:"))) if p
687
- users.each do |s|
688
- wc = count_dict_words(s)
689
- # no need to add to $WordCount,
690
- # because it's done in `count_dict_words` func
691
- puts(" #{s.ljust(17)}: #{wc}") if p
692
- end
693
740
 
694
- user_words = $WordCount - $DefaultWordCount
741
+ if CR_EXTRA_LIBRARY
742
+ extra_lib = Dir.children(CR_EXTRA_LIBRARY)
743
+ # Count extra library
744
+ unless extra_lib.empty?
745
+ wc = 0
746
+ puts(bold(blue("\nExtra library:"))) if p
747
+ extra_lib.each do |s|
748
+ wc = count_dict_words(CR_EXTRA_LIBRARY,s)
749
+ $ExtraLibWordCount += wc
750
+ puts(" #{s.ljust(17)}: #{wc}") if p
751
+ end
752
+ end
695
753
  end
754
+ $TwoLibWordCount = $DefaultLibWordCount + $ExtraLibWordCount
696
755
 
697
756
  if p
698
757
  puts
699
- puts "#{$DefaultWordCount.to_s.rjust(4)} words in default dictionaries"
700
- puts "#{user_words.to_s.rjust(4)} words in user's dictionaries"
701
- puts "#{$WordCount.to_s.rjust(4)} words altogether"
758
+ puts "#{$DefaultLibWordCount.to_s.rjust(4)} words in Default library"
759
+ puts "#{$ExtraLibWordCount.to_s.rjust(4) } words in Extra library"
760
+ puts "#{$TwoLibWordCount.to_s.rjust(4) } words altogether"
702
761
  end
703
762
  end
704
763
 
@@ -712,7 +771,7 @@ case arg
712
771
  when nil then help
713
772
  when '-v' then print_version
714
773
  when '-h' then help
715
- when '-l' then list_dictionaries
774
+ when '-l' then list_dicts
716
775
  when '-c' then word_count(p: true)
717
776
  when '-u' then update_dicts
718
777
  when '-s' then search_word ARGV.shift
@@ -721,6 +780,6 @@ when '-d' then del_dict ARGV.shift
721
780
  when '--help'
722
781
  help
723
782
  else
724
- solve_word arg
783
+ resolve_word arg
725
784
  end
726
785
 
data/lib/cr.rb CHANGED
@@ -2,12 +2,27 @@
2
2
  # File : cr.rb
3
3
  # Authors : ccmywish <ccmywish@qq.com>
4
4
  # Created on : <2022-04-15>
5
- # Last modified : <2022-10-30>
5
+ # Last modified : <2022-11-27>
6
6
  #
7
7
  # cr:
8
8
  #
9
- # This file is just a lib
9
+ # This file is the lib of `cr.rb``
10
10
  #
11
11
  # ------------------------------------------------------
12
12
 
13
- CR_GEM_VERSION = "3.21.0"
13
+ CR_GEM_VERSION = "4.0.0.RC.1"
14
+
15
+
16
+ CR_OFFICIAL_DICTS = <<-EOF
17
+ Default:
18
+ common computer windows
19
+ linux electronics
20
+
21
+ Field:
22
+ economy medicine math
23
+ mechanical science
24
+
25
+ Specific:
26
+ dos x86 signal
27
+
28
+ EOF
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: 3.21.0
4
+ version: 4.0.0.RC.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-30 00:00:00.000000000 Z
11
+ date: 2022-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -66,9 +66,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements:
69
- - - ">="
69
+ - - ">"
70
70
  - !ruby/object:Gem::Version
71
- version: '0'
71
+ version: 1.3.1
72
72
  requirements: []
73
73
  rubygems_version: 3.3.7
74
74
  signing_key: