cr.rb 4.0.2 → 4.0.3

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 +104 -90
  3. data/lib/cr.rb +10 -6
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2811ddb169f1ca4949a47d60e08006bd28917364665a3a671f5636d125a77748
4
- data.tar.gz: 72da546f8d2b6db90048a80c9262656753336562c5818df03adbe9664b087049
3
+ metadata.gz: acfa8f2eec1e398a8c0cdd160f06011caf9f47222a03ee6782cd2bc34c39f472
4
+ data.tar.gz: 6e551fc99f60f805f4f625f3bc4b98504b7bce1f03b600a350fcc69062f7c9cf
5
5
  SHA512:
6
- metadata.gz: f33e9d3f280699821c93d6af0e01230a8718205568abb5d7a20246d0196dc96ce0312a2648e21ef2a15b5cd04946e256caee1edac947eb2d3d8b8efeef334ebd
7
- data.tar.gz: 22c013e6d42eb7db35a7dcf258d018e373d7f1a659a374abf0ece8b8085c2ef5fd7916d9132528af2802e96434fe57b59e145ee641434a661ac502868779eeed
6
+ metadata.gz: af63a8b63dbd7b0f218508f0e84d6183330349132957745f65ef054864b0e7c9500269012015f973f4981a68fc82bd7e26bb7aa33a9a5e6c8681591b3e06b426
7
+ data.tar.gz: cedfc4de30d4d7f6a15ddbd671da52917469524ed87b901692f388add29d1d6eaae627a95c1194bc65e29910beca3fd7f0fa50112797139c86e5260d53c2f573
data/bin/cr CHANGED
@@ -1,16 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
- # coding: utf-8
3
2
  # ------------------------------------------------------
4
3
  # File : cr.rb
5
4
  # Authors : ccmywish <ccmywish@qq.com>
6
5
  # Created on : <2021-07-08>
7
- # Last modified : <2022-11-29>
6
+ # Last modified : <2023-02-05>
8
7
  #
9
8
  # cr:
10
9
  #
11
10
  # This file is used to explain a CRyptic command
12
- # or an acronym's real meaning in computer world or
13
- # other fields.
11
+ # or an acronym's real meaning in computer world or
12
+ # other fields.
14
13
  #
15
14
  # ------------------------------------------------------
16
15
 
@@ -25,15 +24,15 @@ $CR_DEFAULT_DICTS = [
25
24
  "https://github.com/cryptic-resolver/cryptic_computer.git",
26
25
  "https://github.com/cryptic-resolver/cryptic_windows.git",
27
26
  "https://github.com/cryptic-resolver/cryptic_linux.git",
28
- "https://github.com/cryptic-resolver/cryptic_electronics"
27
+ "https://github.com/cryptic-resolver/cryptic_technology"
29
28
  ]
30
29
 
31
30
  # The config file will override the default dicts, but not default library!
32
- if ENV['CRYPTIC_RESOLVER_CONFIG']
31
+ if ENV['CRYPTIC_RESOLVER_CONFIG']
33
32
  file = ENV['CRYPTIC_RESOLVER_CONFIG']
34
33
  if test('f', file)
35
- config = Tomlrb.load_file file
36
-
34
+ config = Tomlrb.load_file file
35
+
37
36
  CR_EXTRA_LIBRARY ||= config['EXTRA_LIBRARY']
38
37
 
39
38
  if config['DEFAULT_DICTS']
@@ -99,7 +98,7 @@ def is_there_any_dict?
99
98
  Dir.mkdir CR_DEFAULT_LIBRARY
100
99
  end
101
100
 
102
- !Dir.empty? CR_DEFAULT_LIBRARY
101
+ !Dir.empty? CR_DEFAULT_LIBRARY
103
102
  end
104
103
 
105
104
 
@@ -117,24 +116,24 @@ def add_default_dicts_if_none_exists
117
116
  else
118
117
  # *nix-like
119
118
  CR_DEFAULT_DICTS_USER_AND_NAMES.each_with_index do |name, i|
120
- fork do
119
+ fork do
121
120
  puts "cr: Pulling #{name}..."
122
121
  `git -C #{CR_DEFAULT_LIBRARY} clone #{CR_DEFAULT_DICTS[i]} -q`
123
122
  end
124
123
  end
125
- Process.waitall
124
+ Process.waitall
126
125
  end
127
126
 
128
127
  rescue Interrupt
129
128
  puts "cr: Cancel add default dicts"
130
129
  exit 1
131
130
  end
132
-
131
+
133
132
  puts "cr: Add done"
134
133
  word_count(p: false)
135
- puts
134
+ puts
136
135
  puts "#{$DefaultLibWordCount} words added"
137
-
136
+
138
137
  # Really added
139
138
  return true
140
139
  end
@@ -153,8 +152,8 @@ def update_dicts()
153
152
  puts "cr: Updating all dicts in Default library..."
154
153
 
155
154
  begin
156
- Dir.chdir CR_DEFAULT_LIBRARY do
157
-
155
+ Dir.chdir CR_DEFAULT_LIBRARY do
156
+
158
157
  if RUBY_PLATFORM.include? "mingw"
159
158
  # Windows doesn't have fork
160
159
  Dir.children(CR_DEFAULT_LIBRARY).each do |dict|
@@ -164,7 +163,7 @@ def update_dicts()
164
163
  else
165
164
  # *nix
166
165
  Dir.children(CR_DEFAULT_LIBRARY).each do |dict|
167
- fork do
166
+ fork do
168
167
  puts "cr: Wait to update #{dict}..."
169
168
  `git -C ./#{dict} pull -q`
170
169
  end
@@ -195,7 +194,7 @@ end
195
194
 
196
195
 
197
196
  def add_dict(repo)
198
- if repo.nil?
197
+ if repo.nil?
199
198
  puts bold(red("cr: Need an argument!"))
200
199
  exit -1
201
200
  end
@@ -232,12 +231,12 @@ end
232
231
 
233
232
 
234
233
  def del_dict(repo)
235
- if repo.nil?
234
+ if repo.nil?
236
235
  puts bold(red("cr: Need an argument!"))
237
236
  exit -1
238
237
  end
239
- Dir.chdir CR_DEFAULT_LIBRARY do
240
- begin
238
+ Dir.chdir CR_DEFAULT_LIBRARY do
239
+ begin
241
240
  # Dir.rmdir repo # Can't rm a filled dir
242
241
  # FileUtils.rmdir repo # Can't rm a filled dir
243
242
  FileUtils.rm_rf repo
@@ -251,8 +250,8 @@ end
251
250
 
252
251
 
253
252
  def load_sheet(library, dict, sheet_name)
254
- file = library + "/#{dict}/#{sheet_name}.toml"
255
-
253
+ file = library + "/#{dict}/#{sheet_name}.toml"
254
+
256
255
  if File.exist? file
257
256
  return Tomlrb.load_file file # gem 'tomlrb'
258
257
  # return TOML.load_file file # gem 'toml'
@@ -267,21 +266,21 @@ end
267
266
  #
268
267
  # A info looks like this
269
268
  # emacs = {
270
- # name = "Emacs"
269
+ # name = "Emacs"
271
270
  # desc = "edit macros"
272
271
  # more = "a feature-rich editor"
273
272
  # see = ["Vim"]
274
273
  # }
275
274
  #
276
- # @param info [Hash] the information of the given word (mapped to a keyword in TOML)
275
+ # @param info [Hash] the information of the given word (mapped to a keyword in TOML)
277
276
  #
278
277
  def pp_info(info)
279
278
  name = info['name'] || red("No name!") # keyword `or` is invalid here in Ruby
280
-
279
+
281
280
  desc = info['desc']
282
281
  more = info['more']
283
-
284
- if desc
282
+
283
+ if desc
285
284
  puts "\n #{name}: #{desc}"
286
285
  print "\n ",more,"\n" if more
287
286
  else
@@ -292,12 +291,19 @@ def pp_info(info)
292
291
  if see_also = info['see']
293
292
  print "\n", purple("SEE ALSO ")
294
293
  if see_also.is_a?(Array)
295
- see_also.each {|x| print underline(x),' '}
294
+ last_ndx = see_also.size - 1
295
+ see_also.each_with_index do |x,i|
296
+ if last_ndx == i
297
+ print underline(x) # Last word doesn't show space
298
+ else
299
+ print underline(x),' '
300
+ end
301
+ end
296
302
  else
297
- print underline(see_also),' '
303
+ print underline(see_also)
298
304
  end
299
305
  puts
300
- end
306
+ end
301
307
  puts
302
308
  end
303
309
 
@@ -313,9 +319,9 @@ end
313
319
  # So we can directly lookup to it
314
320
  #
315
321
  # Notice that, we must jump to a specific word definition
316
- # So in the toml file, you must specify the precise word.
322
+ # So in the toml file, you must specify the precise word.
317
323
  # If it has multiple meanings, for example
318
- #
324
+ #
319
325
  # [blah]
320
326
  # same = "xdg" # this is wrong, because xdg has multiple
321
327
  # # definitions, and all of them specify a
@@ -333,27 +339,27 @@ def pp_same_info(library, dict, word, cache_content, same_key, own_name)
333
339
 
334
340
  # If it's a synonym for anther word,
335
341
  # we should lookup into this dict again, but maybe with a different file
336
-
342
+
337
343
  # file name
338
344
  x = word.chr.downcase
339
-
340
- #
345
+
346
+ #
341
347
  # dictionary maintainer must obey the rule for xxx.yyy word:
342
348
  # xxx should be lower case
343
349
  # yyy can be any case
344
- #
350
+ #
345
351
  # Because yyy should clearly explain the category info, IBM is better than ibm
346
352
  # Implementation should not be too simple if we want to stress the function we
347
- # expect.
348
- #
353
+ # expect.
354
+ #
349
355
  # 'jump to' will output to user, so this is important not only inside our sheet.
350
- #
356
+ #
351
357
  # same = "XDM downloader=>xdm.Download"
352
358
  #
353
359
  # We split 'same' key into two parts via spaceship symbol `=>`, first part will
354
360
  # output to user, the second part is for internal jump.
355
361
  #
356
-
362
+
357
363
  jump_to, same = same_key.split("=>")
358
364
  same = jump_to if same.nil?
359
365
 
@@ -363,17 +369,17 @@ def pp_same_info(library, dict, word, cache_content, same_key, own_name)
363
369
  puts blue(bold(own_name)) + ' redirects to ' + blue(bold(jump_to))
364
370
 
365
371
  #
366
- # As '.' is used to delimit a word and a category, what if
372
+ # As '.' is used to delimit a word and a category, what if
367
373
  # we jump to a dotted word?
368
- #
374
+ #
369
375
  # [eg]
370
- # same = "e.g." # this must lead to a wrong resolution to
371
- # # word 'e', category 'g'
376
+ # same = "e.g." # this must lead to a wrong resolution to
377
+ # # word 'e', category 'g'
372
378
  #
373
379
  # All you need is to be like this:
374
- #
380
+ #
375
381
  # [eg]
376
- # same = "'e.g.'" # cr will notice the single quote
382
+ # same = "'e.g.'" # cr will notice the single quote
377
383
  #
378
384
 
379
385
  if same =~ /^'(.*)'$/
@@ -381,7 +387,7 @@ def pp_same_info(library, dict, word, cache_content, same_key, own_name)
381
387
  else
382
388
  same, category = same.split('.')
383
389
  end
384
-
390
+
385
391
  if same.chr == x
386
392
  # No need to load another dictionary if match
387
393
  sheet_content = cache_content
@@ -416,7 +422,7 @@ end
416
422
  # Lookup the given word in a sheet (a toml file) and also print.
417
423
  # The core idea is that:
418
424
  #
419
- # 1. if the word is `same` with another synonym, it will directly jump to
425
+ # 1. if the word is `same` with another synonym, it will directly jump to
420
426
  # a word in this dictionary, but maybe a different sheet.
421
427
  #
422
428
  # 2. load the toml file and check the given word
@@ -426,7 +432,7 @@ end
426
432
  # [abcd.tYPe]
427
433
  #
428
434
  def lookup(library, dict, file, word)
429
- sheet_content = load_sheet(library, dict, file)
435
+ sheet_content = load_sheet(library, dict, file)
430
436
  return false if sheet_content.nil?
431
437
 
432
438
  info = sheet_content[word]
@@ -435,9 +441,9 @@ def lookup(library, dict, file, word)
435
441
  # Warn if the info is empty. For example:
436
442
  # emacs = { }
437
443
  if info.size == 0
438
- puts red("WARN: Lack of everything of the given word
444
+ puts red("WARN: Lack of everything of the given word
439
445
  Please consider fixing this in the dict `#{dict}`")
440
- exit
446
+ exit
441
447
  end
442
448
 
443
449
 
@@ -446,7 +452,7 @@ def lookup(library, dict, file, word)
446
452
  type_1_exist_flag = false
447
453
 
448
454
  # if already jump, don't check the word itself
449
- is_jump = false
455
+ is_jump = false
450
456
 
451
457
  # synonym info print
452
458
  if same_key = info['same']
@@ -462,8 +468,8 @@ def lookup(library, dict, file, word)
462
468
  # To developer:
463
469
  # The word should at least has one of `desc` and `more`
464
470
  # But when none exists, this may not be considered wrong,
465
- # Because the type2 make the case too.
466
- #
471
+ # Because the type2 make the case too.
472
+ #
467
473
  # So, just ignore it, even if it's really a mistake(insignificant)
468
474
  # by dictionary maintainers.
469
475
  #
@@ -493,7 +499,7 @@ def lookup(library, dict, file, word)
493
499
  else
494
500
  pp_info(info0)
495
501
  end
496
-
502
+
497
503
  # last meaning doesn't show this separate line
498
504
  print blue(bold("OR")),"\n" unless categories.last == meaning
499
505
  end
@@ -511,33 +517,33 @@ end
511
517
  #
512
518
  # 1. Search the default library first
513
519
  # 2. Search the extra library if it does exist
514
- #
520
+ #
515
521
  # The `search` is done via the `lookup` function. It will print
516
522
  # the info while finding. If `lookup` always return false then
517
523
  # means lacking of this word in our dicts. So a welcomed
518
524
  # contribution is printed on the screen.
519
525
  #
520
526
  def resolve_word(word)
521
-
527
+
522
528
  add_default_dicts_if_none_exists
523
529
 
524
530
  word = word.downcase # downcase! would lead to frozen error in Ruby 2.7.2
525
531
  # The index is the toml file we'll look into
526
532
  index = word.chr
527
- case index
528
- when '0'..'9'
533
+ case index
534
+ when '0'..'9'
529
535
  index = '0-9'
530
536
  end
531
537
 
532
538
  # cache lookup's results
533
539
  results = []
534
-
540
+
535
541
  # First consider the default library
536
542
  default = Dir.children(CR_DEFAULT_LIBRARY)
537
543
  default.each do |dict|
538
544
  results << lookup(CR_DEFAULT_LIBRARY,dict,index,word)
539
545
  end
540
-
546
+
541
547
  # Then is the extra library
542
548
  if CR_EXTRA_LIBRARY
543
549
  extra = Dir.children(CR_EXTRA_LIBRARY)
@@ -550,23 +556,28 @@ def resolve_word(word)
550
556
  puts <<-NotFound
551
557
  cr: Not found anything.
552
558
 
553
- You may
554
- 1. Use `cr -u` to update all dicts
559
+ You could
560
+
561
+ #{blue("case 1: Update all dicts")}
562
+
563
+ #{yellow("$cr -u")}
564
+
565
+ #{blue("case 2: List available official and feature dicts")}
566
+
567
+ #{yellow("$cr -l")}
568
+
569
+ #{yellow("$cr -a repo")} (Add a specific dict to default lib)
570
+
571
+ #{blue("case 3: Contribute to theses dicts")}
555
572
 
556
- 2. Use
557
-
558
- `cr -l` to list available official dicts
559
-
560
- `cr -a repo` to add some dicts
561
-
562
- 3. Contribute to theses dicts
573
+ Visit: https://github.com/cryptic-resolver
563
574
 
564
575
  NotFound
565
576
 
566
577
  else
567
578
  return
568
579
  end
569
-
580
+
570
581
  end
571
582
 
572
583
 
@@ -592,15 +603,15 @@ end
592
603
  # This `search_word_internal` routine is quite like `resolve_word`
593
604
  # Notice:
594
605
  # We handle two cases
595
- #
606
+ #
596
607
  # 1. the 'pattern' is the regexp itself
597
608
  # 2. the 'pattern' is like '/blah/'
598
- #
609
+ #
599
610
  # The second is what Ruby and Perl users like to do, handle it!
600
611
  #
601
612
  def search_word_internal(pattern, library)
602
613
 
603
- if pattern.nil?
614
+ if pattern.nil?
604
615
  puts bold(red("cr: Need an argument!"))
605
616
  exit -1
606
617
  end
@@ -612,7 +623,7 @@ def search_word_internal(pattern, library)
612
623
  else
613
624
  regexp = %r[#{pattern}]
614
625
  end
615
-
626
+
616
627
  found_or_not = false
617
628
 
618
629
  #
@@ -628,7 +639,7 @@ def search_word_internal(pattern, library)
628
639
  sheets.each do |sheet|
629
640
  sheet_content = load_sheet(library, dict, File.basename(sheet,'.toml'))
630
641
 
631
- sheet_content.keys.each do
642
+ sheet_content.keys.each do
632
643
  if _1 =~ regexp
633
644
  found_or_not = true
634
645
  similar_words_in_a_dict << _1
@@ -643,7 +654,7 @@ def search_word_internal(pattern, library)
643
654
  LsTable.ls(similar_words_in_a_dict) do |e|
644
655
  puts blue(e)
645
656
  end
646
- puts
657
+ puts
647
658
  end
648
659
  end
649
660
  return found_or_not
@@ -661,7 +672,7 @@ Usage:
661
672
  cr -c => Print word count
662
673
  cr -l => List local dicts and official dicts
663
674
  cr -u => Update all dicts in Default library
664
-
675
+
665
676
  cr -a repo.git => Add a new dict
666
677
  cr -a user/repo => Add a new dict from Github
667
678
  cr -a repo => Add an official dict from Github
@@ -687,18 +698,18 @@ end
687
698
  # 1. List Default library's dicts
688
699
  # 2. List Extra library's dicts
689
700
  # 3. List official dicts
690
- #
701
+ #
691
702
  def list_dicts
692
- Dir.chdir CR_DEFAULT_LIBRARY do
703
+ Dir.chdir CR_DEFAULT_LIBRARY do
693
704
  puts blue("=> Default library: #{CR_DEFAULT_LIBRARY}")
694
705
  Dir.children(CR_DEFAULT_LIBRARY).each_with_index do |dict,i|
695
706
  puts "#{blue(i+1)}. #{bold(green(dict))}"
696
707
  end
697
708
  end
698
709
 
699
- if CR_EXTRA_LIBRARY
710
+ if CR_EXTRA_LIBRARY
700
711
  puts
701
- Dir.chdir CR_EXTRA_LIBRARY do
712
+ Dir.chdir CR_EXTRA_LIBRARY do
702
713
  puts blue("=> Extra library: #{CR_EXTRA_LIBRARY}")
703
714
  Dir.children(CR_EXTRA_LIBRARY).each_with_index do |dict,i|
704
715
  puts "#{blue(i+1)}. #{bold(green(dict))}"
@@ -721,9 +732,9 @@ $DefaultLibWordCount = 0
721
732
  $ExtraLibWordCount = 0
722
733
 
723
734
  def count_dict_words(library, dict)
724
-
735
+
725
736
  dict_dir = library + "/#{dict}"
726
-
737
+
727
738
  wc = 0
728
739
 
729
740
  Dir.children(dict_dir).each do |entry|
@@ -757,6 +768,9 @@ def word_count(p:)
757
768
 
758
769
  if CR_EXTRA_LIBRARY
759
770
  extra_lib = Dir.children(CR_EXTRA_LIBRARY)
771
+ extra_lib.reject! do |f|
772
+ File.file? f
773
+ end
760
774
  # Count extra library
761
775
  unless extra_lib.empty?
762
776
  wc = 0
@@ -770,7 +784,7 @@ def word_count(p:)
770
784
  end
771
785
  $TwoLibWordCount = $DefaultLibWordCount + $ExtraLibWordCount
772
786
 
773
- if p
787
+ if p
774
788
  puts
775
789
  puts "#{$DefaultLibWordCount.to_s.rjust(4)} words in Default library"
776
790
  puts "#{$ExtraLibWordCount.to_s.rjust(4) } words in Extra library"
@@ -790,12 +804,12 @@ when '-v' then print_version
790
804
  when '-h' then help
791
805
  when '-l' then list_dicts
792
806
  when '-c' then word_count(p: true)
793
- when '-u' then update_dicts
807
+ when '-u' then update_dicts
794
808
  when '-s' then search_word ARGV.shift
795
809
  when '-a' then add_dict ARGV.shift
796
- when '-d' then del_dict ARGV.shift
797
- when '--help'
810
+ when '-d' then del_dict ARGV.shift
811
+ when '--help'
798
812
  help
799
813
  else
800
814
  resolve_word arg
801
- end
815
+ end
data/lib/cr.rb CHANGED
@@ -2,7 +2,7 @@
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-05>
6
6
  #
7
7
  # cr:
8
8
  #
@@ -10,18 +10,22 @@
10
10
  #
11
11
  # ------------------------------------------------------
12
12
 
13
- CR_GEM_VERSION = "4.0.2"
13
+ CR_GEM_VERSION = "4.0.3"
14
14
 
15
15
 
16
16
  CR_OFFICIAL_DICTS = <<-EOF
17
17
  Default:
18
18
  common computer windows
19
- linux electronics
19
+ linux technology
20
20
 
21
21
  Field:
22
- economy medicine math
23
- mechanical science
22
+ electronics economy medicine
23
+ mechanical science math
24
24
 
25
25
  Specific:
26
26
  dos x86 signal
27
- EOF
27
+
28
+ Feature:
29
+ ccmywish/CRuby-Source-Code-Dictionary
30
+
31
+ 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: 4.0.2
4
+ version: 4.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
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-05 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.3.26
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: 'cr: Cryptic Resolver'