cr.rb 4.1.4 → 4.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/exe/cr +68 -0
  3. data/lib/cr/counter.rb +103 -0
  4. data/lib/cr/version.rb +3 -3
  5. data/lib/cr.rb +526 -116
  6. metadata +21 -6
  7. data/bin/cr +0 -597
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 624c2bec73d0e1c7af063aec76ce582a38a2b73c56d8680a3d67a8f293b8695b
4
- data.tar.gz: 49b1c1aa5c178087d94e70d3110c7a7e3b479478723be80c5ff67467c05d1634
3
+ metadata.gz: 7f2acb1b5dc93e60b954ae421a5e177694a14317b9883dfd54da40dbaefb2d97
4
+ data.tar.gz: e603d108e83f7f2b0ed1fc64d27ef2974a15a1ca44b7eb8de24b00869a2e5ef6
5
5
  SHA512:
6
- metadata.gz: 83b2810d78b2081006474f50df69ab8765327c8c9715d491204548ac4bd5f0c17edb30c5b551a9d30c0d825d93b8d709258a13dc359aa3fce9c9e9c0379893dd
7
- data.tar.gz: 98c6ac9a3eb0273097d24144013477c7d11cc20ed3818ac79c1779e72b0aad932573b0e39bda08ac1c8c14588ab6929dc2bad6e920e7e287df6e0454b15db9be
6
+ metadata.gz: b568278c452502f466d4c3730e842df0757409612a2fddb322df44aa090a20deefaa208d5006cb066f6f65911efd8173b4fbb3c74790e50261420b05de5a2583
7
+ data.tar.gz: f0e2510cd01776c54878964a6e4bdaf7c6c3002e121e7ccaa2cc5d9e6bdd28ab92bddcfffe678514a0bd18668309485c7651968346c241fc30c9e3835c0631d3
data/exe/cr ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+ # ------------------------------------------------------
3
+ # File : cr.rb
4
+ # Authors : Aoran Zeng <ccmywish@qq.com>
5
+ # Created on : <2021-07-08>
6
+ # Last modified : <2023-05-14>
7
+ #
8
+ # cr:
9
+ #
10
+ # This file is used to explain a CRyptic command
11
+ # or an acronym's real meaning in computer world or
12
+ # other fields.
13
+ # ------------------------------------------------------
14
+
15
+ require 'cr'
16
+
17
+ module CrypticResolver::CLI
18
+
19
+ def self.run
20
+ args = ARGV.dup
21
+
22
+ option = args.shift
23
+
24
+ case option when nil, '-h','--help' then help; exit end
25
+
26
+ reslvr = CrypticResolver::Resolver.new
27
+
28
+ reslvr.add_default_dicts_if_none_exists
29
+
30
+ case option
31
+ when '-l' then reslvr.list_dicts
32
+ when '-c' then reslvr.count_words
33
+ when '-u' then reslvr.update_dicts
34
+ when '-s' then reslvr.search_related_words args.shift
35
+ when '-a' then reslvr.add_dict args.shift
36
+ when '-d' then reslvr.del_dict args.shift
37
+ else reslvr.resolve_word option
38
+ end
39
+ end
40
+
41
+
42
+ def self.help
43
+ puts <<~HELP
44
+ cr: Cryptic Resolver v#{CrypticResolver::GEM_VERSION}
45
+
46
+ Usage:
47
+ cr <keyword> Search the keyword
48
+ cr emacs => Edit macros: A feature-rich editor
49
+
50
+ Options:
51
+ -c Print word count
52
+ -s <pattern> Search words matched with pattern
53
+ -h Print this help
54
+
55
+ Dictionaries:
56
+ -l List local dicts and official dicts
57
+ -u Update all dicts in Default library
58
+ -a <https://repo[.git]> Add a new dict
59
+ -a <user/repo> Add a new dict from Github
60
+ -a <dict_name> Add an official dict, use -l to see
61
+ -d <dict_name> Delete a dict
62
+
63
+ HELP
64
+ end
65
+
66
+ end
67
+
68
+ CrypticResolver::CLI.run
data/lib/cr/counter.rb ADDED
@@ -0,0 +1,103 @@
1
+ # ---------------------------------------------------------------
2
+ # File : counter.rb
3
+ # Authors : Aoran Zeng <ccmywish@qq.com>
4
+ # Created on : <2022-03-06>
5
+ # Last modified : <2023-05-14>
6
+ #
7
+ # counter:
8
+ #
9
+ # Count words in dictionaries.
10
+ # ---------------------------------------------------------------
11
+
12
+ class CrypticResolver::Resolver::Counter
13
+
14
+ using Rainbow
15
+
16
+ attr_accessor :word_count_of_two_libs, # def_lib + extra_lib
17
+ :word_count_of_def_lib,
18
+ :word_count_of_extra_lib,
19
+
20
+ :resolver
21
+
22
+ def initialize(resolver)
23
+ @word_count_of_two_libs = 0
24
+ @word_count_of_def_lib = 0
25
+ @word_count_of_extra_lib = 0
26
+ @resolver = resolver
27
+ end
28
+
29
+ # a.toml
30
+ # b.toml
31
+ # ...
32
+ def count_dict_words(library, dict)
33
+ dict_dir = library + "/#{dict}"
34
+ wc = 0
35
+ Dir.children(dict_dir).each do |entry|
36
+ next unless entry.end_with?('.toml')
37
+ next if File.directory? dict_dir + "/#{entry}"
38
+ sheet_content = @resolver.load_sheet(library, dict, entry.delete_suffix('.toml'))
39
+ count = sheet_content.keys.count
40
+
41
+ wc = wc + count
42
+ end
43
+ return wc
44
+ end
45
+
46
+
47
+ # Count default library
48
+ def count_def_lib(display: )
49
+ path = CrypticResolver::Resolver::DEFAULT_LIB_PATH
50
+ default_lib = Dir.children path
51
+ unless default_lib.empty?
52
+ puts "Default library: ".bold.green if display
53
+ default_lib.each do |s|
54
+ next if File.file? path + "/#{s}"
55
+ wc = count_dict_words(path,s)
56
+ @word_count_of_def_lib += wc
57
+ # With color, ljust not works, so we disable color
58
+ puts("#{wc.to_s.rjust(5)} #{s}") if display
59
+ end
60
+ end
61
+ return @word_count_of_def_lib
62
+ end
63
+
64
+
65
+ # Count extra library
66
+ def count_extra_lib(display: )
67
+ if path = @resolver.extra_lib_path
68
+ extra_lib = Dir.children path
69
+ unless extra_lib.empty?
70
+ puts "\nExtra library:".bold.green if display
71
+ extra_lib.each do |s|
72
+ next if File.file? path + "/#{s}"
73
+ wc = count_dict_words(path,s)
74
+ @word_count_of_extra_lib += wc
75
+ puts("#{wc.to_s.rjust(5)} #{s}") if display
76
+ end
77
+ end
78
+ end
79
+ return @word_count_of_extra_lib
80
+ end
81
+
82
+
83
+ def count!(display: )
84
+ count_def_lib(display: display)
85
+ count_extra_lib(display: display)
86
+ @word_count_of_two_libs = @word_count_of_def_lib + @word_count_of_extra_lib
87
+
88
+ if display
89
+ puts
90
+ puts "#{@word_count_of_def_lib.to_s.rjust(4)} words in Default library"
91
+ puts "#{@word_count_of_extra_lib.to_s.rjust(4) } words in Extra library"
92
+ puts "#{@word_count_of_two_libs.to_s.rjust(4) } words altogether"
93
+ end
94
+ end
95
+
96
+
97
+ def reset!
98
+ @word_count_of_two_libs = 0
99
+ @word_count_of_def_lib = 0
100
+ @word_count_of_extra_lib = 0
101
+ end
102
+
103
+ end
data/lib/cr/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # ---------------------------------------------------------------
2
2
  # File : version.rb
3
- # Authors : ccmywish <ccmywish@qq.com>
3
+ # Authors : Aoran Zeng <ccmywish@qq.com>
4
4
  # Created on : <2023-02-12>
5
- # Last modified : <2023-03-22>
5
+ # Last modified : <2023-05-15>
6
6
  #
7
7
  # version:
8
8
  #
@@ -11,6 +11,6 @@
11
11
 
12
12
  module CrypticResolver
13
13
 
14
- GEM_VERSION = "4.1.4"
14
+ GEM_VERSION = "4.1.6"
15
15
 
16
16
  end