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