cureutils 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc63c6680e85d32c4f482a6b7143ed547b5254ff
4
- data.tar.gz: 0312bf5915a8cc686893e57bad63b9f8a95de76a
3
+ metadata.gz: 638a3b1f4eb73a02514eece933d1e92e6df5a407
4
+ data.tar.gz: 5c183b238f20bda184636daf26159568c80f6428
5
5
  SHA512:
6
- metadata.gz: 2ff49a261c99d507eed2d0f8c3ded219c055d0ccb846e63f5cf22f75931980a3dad54c39664a57210ad8c8c2a2e886b4ad40ab0b63290412724f4f67d5a8839a
7
- data.tar.gz: a83f4bb95fd8ec8bde4e9019527ef5062d671078ac3900156346253223abbd3ffcef06d223ecc531ba387fae3af0749dcb458d4cf04b21a2d42442def613053b
6
+ metadata.gz: 4add95659a50a809e439fdd9b3aea5067a2e95ccfaaa32553dbd1e94db07bbac135a1e4593963dd611958dc6841a2352c77b7cd56d4a0f5e0c0ef7e9f811e711
7
+ data.tar.gz: 026b49dbdd0f189a5a2c5660704d7cec7152bcfdaf5debf735c21ef93324a558e3f29d24ea53260d01914600b6a5508b7d25cf76e8e7c471766f7d4977b39176
data/README.md CHANGED
@@ -5,11 +5,11 @@
5
5
  # Cureutils
6
6
  [![Gem Version](https://badge.fury.io/rb/cureutils.svg)](https://badge.fury.io/rb/cureutils)
7
7
 
8
- Useful command line tool for Japanese battle heroine "Pretty Cure (Precure)".
8
+ Useful command line tool for Japanese battle heroine Pretty Cure (Precure).
9
9
 
10
10
  ## Installation
11
11
 
12
- Install it yourself as:
12
+ Install:
13
13
 
14
14
  $ gem install cureutils
15
15
 
@@ -18,47 +18,90 @@ After that, `cure` command is available.
18
18
 
19
19
  $ cure
20
20
 
21
- ## Usage
21
+ # Usage
22
22
 
23
- ### cure date
23
+ ```
24
+ $ cure help
25
+ Commands:
26
+ cure date [OPTIONS] [+FORMAT] # Display date, time and Precure related events.
27
+ cure echo [OPTIONS] PATTERN # Print messages of Precure.
28
+ cure girls # Print girls' name
29
+ cure grep [OPTIONS] PATTERN # Print lines matching a pattern.
30
+ cure help [COMMAND] # Describe available commands or one specific command
31
+ cure humanize # Change precure_name to human_name.
32
+ cure janken # Let's play "Pikarin Janken" !
33
+ cure precures # Print Precure names
34
+ cure tr PATTERN REPLACE # Translate Precure related parameters.
35
+ cure transform # Change human_name to precure_name
36
+ ```
37
+
38
+ ## `cure date`
24
39
  Display date, time and Precure related events.
25
40
 
41
+ ```
42
+ cure date [OPTIONS] [+FORMAT]
43
+ ```
44
+
26
45
  ```sh
27
46
  $ cure date
28
47
  2016-06-17 21:28:42 魔法つかいプリキュア!放映期間
29
48
  ```
30
49
 
31
- * %Y : Year
32
- * %m : Month 01 - 12
33
- * %d : Date 01 - 31
34
- * @P : Precure related events.
35
- * And other general date formats are usable.
50
+ ### Output format
51
+
52
+ | Format specifier | Description | Example |
53
+ | --- | --- | --- |
54
+ | %Y | Year | 2016 |
55
+ | %m | Month | 12 |
56
+ | %d | Date | 31 |
57
+ | @P | Precure related events. | 魔法つかいプリキュア!放映期間 |
36
58
 
37
- #### Options
59
+ And other general date formats are usable.
60
+
61
+ ### Options
38
62
 
39
63
  | Option | Description |
40
64
  | ------ | --------- |
41
65
  | -d | cure date -d STRING: display time described by STRING |
42
66
 
43
-
44
67
  ```sh
45
68
  $ cure date -d '3 years ago' '+%Y/%m @P'
46
69
  2013/06 ドキドキ!プリキュア放映期間
47
70
  ```
48
71
 
49
-
50
- ### cure echo
72
+ ## `cure echo`
51
73
  Print messages of Precure.
52
74
 
75
+ ```
76
+ cure echo [OPTIONS] PATTERN
77
+ ```
78
+
53
79
  ```sh
54
80
  $ cure echo
55
81
  みんなの思いを守るために
56
82
  心をひとつに!
57
83
  思いよ届け!キュアエコー!
84
+
85
+ $ cure echo -p happy
86
+ (レディ?)
87
+ プリキュア・スマイルチャージ!
88
+ (ゴー!ゴー!レッツ・ゴー!ハッピー!!)
89
+ キラキラ輝く未来の光! キュアハッピー!
90
+ 5つの光が導く未来!
91
+ 輝け!スマイルプリキュア!
58
92
  ```
59
93
 
60
- ### cure girls
61
- Print girls' name
94
+ ### Options
95
+
96
+ | Option | Description |
97
+ | ------ | ----------- |
98
+ | -a | Print attack message. |
99
+ | -q | Print messages immediately. |
100
+ | -p | Print the given PRECURE's message. |
101
+
102
+
103
+ ## `cure girls`
104
+ Print girls' name.
62
105
 
63
106
  ```sh
64
107
  $ cure girls
@@ -75,16 +118,23 @@ $ cure girls
75
118
  ...
76
119
  ```
77
120
 
78
- ### cure grep
121
+ ## `cure grep`
79
122
  Print lines matching a pattern.
80
123
 
124
+ ```
125
+ cure grep [OPTIONS] PATTERN
126
+ ```
127
+
81
128
  ```sh
82
129
  $ echo キュア{レッド,ピンク,ブラック,ブルー,ホワイト}"\n" | cure grep
83
130
  キュアブラック
84
131
  キュアホワイト
132
+
133
+ $ echo "こんにちは、 私はキュアレモネードです。" | cure grep '私は[:precure_name:]です。'
134
+ こんにちは、 私はキュアレモネードです。
85
135
  ```
86
136
 
87
- #### Options
137
+ ### Options
88
138
 
89
139
  | Option | Description |
90
140
  | ------ | ----------- |
@@ -92,7 +142,7 @@ $ echo キュア{レッド,ピンク,ブラック,ブルー,ホワイト}"\n" |
92
142
  | -o | Print only the matched (non-empty) parts of a matching line, with each such part on a separate out. |
93
143
 
94
144
 
95
- #### Folloing "Precure bracket expression" are available.
145
+ ### Folloing "Precure bracket expression"can be used.
96
146
 
97
147
  | Expression | Description |
98
148
  | ---------- | -------------- |
@@ -103,12 +153,7 @@ $ echo キュア{レッド,ピンク,ブラック,ブルー,ホワイト}"\n" |
103
153
  | [:precure_name:] | Precure name. |
104
154
 
105
155
 
106
- ```sh
107
- $ cat txt | cure grep '私は[:precure_name:]です。'
108
- こんにちは、 私はキュアレモネードです。
109
- ```
110
-
111
- ### cure tr
156
+ ## `cure tr`
112
157
  Translate Precure related parameters.
113
158
 
114
159
  ```sh
@@ -116,7 +161,7 @@ $ echo 相田マナ | cure tr '[:human_name:]' '[:cast_name:]'
116
161
  生天目仁美
117
162
  ```
118
163
 
119
- #### Folloing "Precure bracket expression" are available.
164
+ ### Folloing "Precure bracket expression" are available.
120
165
 
121
166
  | Expression | Description |
122
167
  | ---------- | -------------- |
@@ -127,15 +172,15 @@ $ echo 相田マナ | cure tr '[:human_name:]' '[:cast_name:]'
127
172
  | [:precure_name:] | Precure name. |
128
173
 
129
174
 
130
- ### cure transform
131
- Replace human_name to precure_name
175
+ ## `cure transform`
176
+ Change human_name to precure_name
132
177
 
133
178
  ```sh
134
179
  $ echo '私の名前は十六夜リコです。' | cure transform
135
180
  私の名前はキュアマジカルです。
136
181
  ```
137
182
 
138
- ### cure humanize
183
+ ## `cure humanize`
139
184
  Change precure_name to human_name.
140
185
 
141
186
  ```sh
@@ -143,7 +188,7 @@ $ echo キュア{ハート,エース} | cure humanize
143
188
  相田マナ 円亜久里
144
189
  ```
145
190
 
146
- ### cure janken
191
+ ## `cure janken`
147
192
  Let's play "Pikarin Janken" !
148
193
 
149
194
  ```sh
@@ -167,8 +212,8 @@ Exit status is changed depending on the result.
167
212
  | You lose | 1 |
168
213
  | Draw | 2 |
169
214
 
170
- ### cure precures
171
- Print Precure names
215
+ ## `cure precures`
216
+ Print Precure names.
172
217
 
173
218
  ```
174
219
  $ cure precures
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Cureutils::Version
9
9
  spec.authors = ['Yamada, Yasuhiro']
10
10
  spec.email = ['greengregson@gmail.com']
11
- spec.summary = 'Useful commands for battle heroine "Pretty Cure (Precure)".'
12
- spec.description = 'Commands which can smoothly deal with Precure-related data.'
11
+ spec.summary = 'Useful commands for battle heroine Pretty Cure (Precure).'
12
+ spec.description = 'Useful command line tool for Japanese battle heroine Pretty Cure (Precure).'
13
13
  spec.homepage = 'https://github.com/greymd/cureutils'
14
14
  spec.license = 'MIT'
15
15
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -1,7 +1,11 @@
1
1
  # coding: utf-8
2
2
  require 'cureutils/version'
3
- require 'cureutils/janken_controller'
4
- require 'cureutils/cure_date_checker'
3
+ require 'cureutils/common'
4
+ require 'cureutils/cure_janken_manager'
5
+ require 'cureutils/cure_date_manager'
6
+ require 'cureutils/cure_grep_manager'
7
+ require 'cureutils/cure_echo_manager'
8
+ require 'cureutils/cure_translate_manager'
5
9
  require 'active_support'
6
10
  require 'active_support/time'
7
11
  require 'time'
@@ -10,10 +14,6 @@ require 'rubicure'
10
14
  require 'colorize'
11
15
 
12
16
  module Cureutils
13
- module EchoMode
14
- TRANSFORM = 1
15
- ATTACK = 2
16
- end
17
17
  #
18
18
  # The class represents the cli interface
19
19
  #
@@ -26,12 +26,18 @@ module Cureutils
26
26
 
27
27
  desc 'transform', 'Change human_name to precure_name'
28
28
  def transform
29
- print_converted_text($stdin, :human_name, :precure_name)
29
+ manager = CureTranslateManager.new
30
+ manager.translate_from_to('[:human_name:]', '[:precure_name:]')
31
+ manager.in = $stdin
32
+ exit(manager.print_results)
30
33
  end
31
34
 
32
35
  desc 'humanize', 'Change precure_name to human_name.'
33
36
  def humanize
34
- print_converted_text($stdin, :precure_name, :human_name)
37
+ manager = CureTranslateManager.new
38
+ manager.translate_from_to('[:precure_name:]', '[:human_name:]')
39
+ manager.in = $stdin
40
+ exit(manager.print_results)
35
41
  end
36
42
 
37
43
  desc 'girls', "Print girls' name"
@@ -50,217 +56,67 @@ module Cureutils
50
56
 
51
57
  desc 'grep [OPTIONS] PATTERN', 'Print lines matching a pattern.'
52
58
  option 'extended-regexp', aliases: 'E',
53
- type: :boolean,
54
- desc: 'Disable Precure Bracket Expression.'
59
+ type: :boolean,
60
+ desc: 'Disable Precure Bracket Expression.'
55
61
  option 'only-matching', aliases: 'o',
56
- type: :boolean,
57
- desc: 'Print only the matched parts of a matching line.'
62
+ type: :boolean,
63
+ desc: 'Print only the matched parts.'
58
64
  def grep(default_pat = '[:precure_name:]', filename = nil)
59
- # Check whether the file is given or not
60
- @input = input_from(filename)
61
- pat = default_pat.clone
62
- pat = pregex2regex(default_pat) unless options['extended-regexp'.to_sym]
63
- # Check the file discriptor and check the pipe exists or not.
64
- enable_color = !$stdout.isatty
65
- if options['only-matching'.to_sym]
66
- if enable_color
67
- @input.each do |line|
68
- matched_strs = line.scan(/#{pat}/)
69
- matched_strs.empty? || matched_strs.each do |str|
70
- puts str
71
- end
72
- end
73
- else
74
- @input.each do |line|
75
- matched_strs = line.scan(/#{pat}/)
76
- matched_strs.empty? || matched_strs.each do |str|
77
- puts str.red
78
- end
79
- end
80
- end
81
- else
82
- if enable_color
83
- @input.each do |line|
84
- puts line.gsub(/#{pat}/, '\0') if line =~ /#{pat}/
85
- end
86
- else
87
- @input.each do |line|
88
- puts line.gsub(/#{pat}/, '\0'.red) if line =~ /#{pat}/
89
- end
90
- end
91
- end
65
+ manager = CureGrepManager.new
66
+ manager.source_input(filename)
67
+ manager.pattern(default_pat.clone, options['extended-regexp'.to_sym])
68
+ # Check the file discriptor to check the pipe exists or not.
69
+ manager.option_colorize($stdout.isatty)
70
+ manager.option_only(options['only-matching'.to_sym])
71
+ # Print matched lines.
72
+ exit(manager.print_results)
92
73
  end
93
74
 
94
75
  desc 'tr PATTERN REPLACE', 'Translate Precure related parameters.'
95
76
  def tr(pat_from = '[:precure_name:]', pat_to = '[:human_name:]')
96
- pat_from = pregex2str(pat_from).to_sym
97
- pat_to = pregex2str(pat_to).to_sym
98
- print_converted_text($stdin, pat_from, pat_to)
77
+ manager = CureTranslateManager.new
78
+ manager.in = $stdin
79
+ manager.translate_from_to(pat_from, pat_to)
80
+ exit(manager.print_results)
99
81
  end
100
82
 
101
- desc 'echo PATTERN', 'Print messages of Precure.'
83
+ desc 'echo [OPTIONS] PATTERN', 'Print messages of Precure.'
102
84
  option 'quick', aliases: 'q',
103
- type: :boolean,
104
- desc: 'Print messages immediately.'
85
+ type: :boolean,
86
+ desc: 'Print messages immediately.'
105
87
  option 'attack', aliases: 'a',
106
- type: :boolean, desc: 'Print attack message.'
88
+ type: :boolean,
89
+ desc: 'Print attack message.'
107
90
  option 'transform', aliases: 't',
108
- type: :boolean, desc: 'Print transform message.'
91
+ type: :boolean,
92
+ desc: 'Print transform message.'
109
93
  option 'precure', aliases: 'p',
110
- type: :string, desc: "Print the given PRECURE's message."
94
+ type: :string,
95
+ desc: "Print the given PRECURE's message."
111
96
  def echo
112
- cure_name = options[:precure] || 'echo'
113
- message_mode = EchoMode::TRANSFORM
114
- message_mode = EchoMode::TRANSFORM if options[:transform]
115
- message_mode = EchoMode::ATTACK if options[:attack]
116
- Rubicure::Girl.sleep_sec = 0 if options[:quick]
117
- cure = Rubicure::Girl.config.find { |k, _v| k == cure_name.to_sym }
118
- unless cure
119
- $stderr.puts "No such precure #{cure_name}"
120
- exit(1)
121
- end
122
- if message_mode == EchoMode::TRANSFORM
123
- Cure.send(cure_name.to_sym).transform!
124
- elsif message_mode == EchoMode::ATTACK
125
- Cure.send(cure_name.to_sym).transform!
126
- Cure.send(cure_name.to_sym).attack!
127
- end
97
+ manager = CureEchoManager.new
98
+ manager.precure(options[:precure])
99
+ manager.msg_attack(options[:attack])
100
+ manager.nosleep(options[:quick])
101
+ exit(manager.print_results)
128
102
  end
129
103
 
130
104
  desc 'date [OPTIONS] [+FORMAT]',
131
- 'Display date, time and Precure related events.'
105
+ 'Display date, time and Precure related events.'
132
106
  option 'date', aliases: 'd'
133
107
  # Original date command's default is '+%a %b %e %H:%M:%S %Z %Y @P'
134
108
  # However, I would like to adopt this setting.
135
109
  def date(fmt = '+%F %H:%M:%S @P')
136
- # -d, --date=STRING (YYYY-MM-DD or +-N days)
137
- print_time = create_time_obj(options[:date])
138
- updated_fmt = update_fmt(print_time, fmt)
139
- puts print_time.strftime(updated_fmt)
110
+ manager = CureDateManager.new
111
+ manager.datetime(options[:date])
112
+ manager.format = fmt
113
+ exit(manager.print_results)
140
114
  end
141
115
 
142
- desc 'janken', %Q(Let's play "Pikarin Janken" !)
116
+ desc 'janken', %q(Let's play "Pikarin Janken" !)
143
117
  def janken
144
- judge = JankenContoller.janken
145
- exit(judge.to_i)
146
- end
147
-
148
- private
149
-
150
- def input_from(filename)
151
- if filename.nil? || filename.empty?
152
- output = $stdin
153
- elsif filename =~ /^-$/
154
- # If the file name is "-", use STDIN.
155
- output = $stdin
156
- else
157
- begin
158
- output = File.open(filename)
159
- rescue SystemCallError => e
160
- puts %(class=[#{e.class}] message=[#{e.message}])
161
- rescue IOError => e
162
- puts %(class=[#{e.class}] message=[#{e.message}])
163
- end
164
- end
165
- output
166
- end
167
-
168
- def update_fmt(datetime, fmt)
169
- # Find precure related events
170
- date4check_event = time2date(datetime)
171
- found_event = CureDateChecker.events(date4check_event)
172
- checked_fmt = fmt
173
- if fmt =~ /^\+(.*)$/
174
- checked_fmt = Regexp.last_match(1)
175
- else
176
- puts "cure date: invalid date format '#{fmt}'"
177
- exit 1
178
- end
179
- # Find precure related events
180
- checked_fmt.gsub(/@P/, found_event)
181
- end
182
-
183
- def create_time_obj(time_str)
184
- time_str ? natural_lang2time(time_str) : Time.now
185
- end
186
-
187
- def natural_lang2time(time_str)
188
- updated_fmt = time_str.dup
189
- updated_fmt.gsub!(/yesterday/, '1 day ago')
190
- updated_fmt.gsub!(/tomorrow/, '-1 day ago')
191
- units = 'second|minute|hour|day|week|month|year'
192
- regulated_format = /(-?[0-9]+) *(#{units})s? *(ago)?/
193
- if updated_fmt =~ regulated_format
194
- diff_value = Regexp.last_match(1).to_i
195
- unit = Regexp.last_match(2).to_sym
196
- minus_flg = Regexp.last_match(3)
197
- if minus_flg.nil?
198
- Time.now + diff_value.send(unit)
199
- else
200
- Time.now - diff_value.send(unit)
201
- end
202
- else
203
- Time.parse(updated_fmt)
204
- end
205
- end
206
-
207
- # Convert string to precure regular expression
208
- def pregex2regex(regex, br_flg = false)
209
- this_regex = regex.dup
210
- br_ex = br_flg ? '' : '?:'
211
- %w(girl_name human_name precure_name cast_name color).each do |attr|
212
- expression = '\[:' + attr + ':\]'
213
- precures_ex = cure_list(attr.to_sym).join('|')
214
- replaced = "(#{br_ex}#{precures_ex})"
215
- this_regex.gsub!(/#{expression}/, replaced)
216
- end
217
- this_regex
218
- end
219
-
220
- def str2pregex(str)
221
- '[:' + str + ':]'
222
- end
223
-
224
- def pregex2str(str)
225
- str.gsub(/\[:(.*):\]/, '\1')
226
- end
227
-
228
- def cure_list(sym)
229
- list = Precure.all_stars.map(&sym)
230
- list << Cure.echo[sym]
231
- # Regulate cure princes human name
232
- list.map do |str|
233
- str.gsub!(/\(.+?\)/, '')
234
- str
235
- end
236
- list
237
- end
238
-
239
- def cure_table(to_sym, from_sym)
240
- to_arr = cure_list(to_sym)
241
- from_arr = cure_list(from_sym)
242
- hash = Hash[[to_arr, from_arr].transpose]
243
- hash
244
- end
245
-
246
- def time2date(timeObj)
247
- Date.parse(timeObj.strftime('%Y-%m-%d'))
248
- end
249
-
250
- def print_converted_text(input, from_sym, to_sym)
251
- # Create precure regular expression
252
- regex_tag = str2pregex(from_sym.to_s)
253
- # Get patterns
254
- cure_pattern = pregex2regex(regex_tag, true)
255
- # Get Key-Value
256
- table = cure_table(from_sym, to_sym)
257
- input.each do |line|
258
- updated_line = line.dup
259
- line.scan(/#{cure_pattern}/).each do |pat|
260
- updated_line.gsub!(/#{pat[0]}/, table[pat[0]])
261
- end
262
- puts updated_line
263
- end
118
+ manager = CureJankenManager.new
119
+ exit(manager.janken.to_i)
264
120
  end
265
121
  end
266
122
  end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ #
3
+ # Class includes common methos which are refered from multiple commands.
4
+ #
5
+ class Common
6
+ class << self
7
+ # Convert string to precure regular expression
8
+ def pregex2regex(regex, br_flg = false)
9
+ this_regex = regex.dup
10
+ br_ex = br_flg ? '' : '?:'
11
+ %w(girl_name human_name precure_name cast_name color).each do |attr|
12
+ expression = '\[:' + attr + ':\]'
13
+ precures_ex = cure_list(attr.to_sym).join('|')
14
+ replaced = "(#{br_ex}#{precures_ex})"
15
+ this_regex.gsub!(/#{expression}/, replaced)
16
+ end
17
+ this_regex
18
+ end
19
+
20
+ def cure_list(sym)
21
+ list = Precure.all_stars.map(&sym)
22
+ list << Cure.echo[sym]
23
+ # Regulate cure princes human name
24
+ list.map do |str|
25
+ str.gsub!(/\(.+?\)/, '')
26
+ str
27
+ end
28
+ list
29
+ end
30
+
31
+ def cure_table(to_sym, from_sym)
32
+ to_arr = cure_list(to_sym)
33
+ from_arr = cure_list(from_sym)
34
+ hash = Hash[[to_arr, from_arr].transpose]
35
+ hash
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,137 @@
1
+ # coding: utf-8
2
+ require 'date'
3
+
4
+ #
5
+ # Date class supports the precure format
6
+ #
7
+ class CureDateManager
8
+ def initialize
9
+ hashize_cure_date unless @birth_date || @created_date
10
+ hashize_movie_date unless @movie_started_date
11
+ @datetime = Time.now
12
+ @format = '+%F %H:%M:%S @P'
13
+ @out = $stdout
14
+ end
15
+
16
+ def source_output(output = $stdout)
17
+ @out = output
18
+ end
19
+
20
+ def datetime(datetime_str)
21
+ @datetime = datetime_str ? natural_lang2time(datetime_str) : Time.now
22
+ end
23
+
24
+ attr_writer :format
25
+
26
+ def print_results
27
+ updated_fmt = update_fmt(@datetime, @format)
28
+ @out.puts @datetime.strftime(updated_fmt)
29
+ 0
30
+ end
31
+
32
+ private
33
+
34
+ def natural_lang2time(time_str)
35
+ updated_fmt = time_str.dup
36
+ updated_fmt.gsub!(/yesterday/, '1 day ago')
37
+ updated_fmt.gsub!(/tomorrow/, '-1 day ago')
38
+ units = 'second|minute|hour|day|week|month|year'
39
+ regulated_format = /(-?[0-9]+) *(#{units})s? *(ago)?/
40
+ if updated_fmt =~ regulated_format
41
+ diff_value = Regexp.last_match(1).to_i
42
+ unit = Regexp.last_match(2).to_sym
43
+ minus_flg = Regexp.last_match(3)
44
+ if minus_flg.nil?
45
+ Time.now + diff_value.send(unit)
46
+ else
47
+ Time.now - diff_value.send(unit)
48
+ end
49
+ else
50
+ Time.parse(updated_fmt)
51
+ end
52
+ end
53
+
54
+ def update_fmt(datetime, fmt)
55
+ # Find precure related events
56
+ date4check_event = time2date(datetime)
57
+ found_event = events(date4check_event)
58
+ checked_fmt = fmt
59
+ if fmt =~ /^\+(.*)$/
60
+ checked_fmt = Regexp.last_match(1)
61
+ else
62
+ puts "cure date: invalid date format '#{fmt}'"
63
+ exit 1
64
+ end
65
+ # Find precure related events
66
+ checked_fmt.gsub(/@P/, found_event)
67
+ end
68
+
69
+ def events(date)
70
+ event_list = [birth_date?(date),
71
+ created_date?(date),
72
+ movie_date?(date),
73
+ series_between(date)].compact
74
+ event_list.join('/')
75
+ end
76
+
77
+ def time2date(time_obj)
78
+ Date.parse(time_obj.strftime('%Y-%m-%d'))
79
+ end
80
+
81
+ def birth_date?(date)
82
+ date_str = date.strftime('%m/%d')
83
+ result = @birth_date[date_str]
84
+ # format
85
+ result.nil? ? nil : "#{result[:human_name]}(#{result[:precure_name]})誕生日"
86
+ end
87
+
88
+ def created_date?(date)
89
+ date_str = date.strftime('%m/%d')
90
+ result = @created_date[date_str]
91
+ # format
92
+ result.nil? ? nil : "#{result[:precure_name]}初登場日"
93
+ end
94
+
95
+ def movie_date?(date)
96
+ result = @created_date[date]
97
+ # format
98
+ result.nil? ? nil : "#{result[:title]}映画公開日"
99
+ end
100
+
101
+ def series_between(date)
102
+ event_name = nil
103
+ Rubicure::Series.config.each do |_k, v|
104
+ if v[:started_date] == date
105
+ event_name = "#{v[:title]}初放映"
106
+ break
107
+ end
108
+ if v[:ended_date] == date
109
+ event_name = "#{v[:title]}最終回"
110
+ break
111
+ end
112
+ # Nobody expects the future. Precure series may be abolished today.
113
+ v[:ended_date] = Date.today + 1 if v[:ended_date].nil?
114
+ if v[:started_date] < date && date < v[:ended_date]
115
+ event_name = "#{v[:title]}放映期間"
116
+ break
117
+ end
118
+ end
119
+ event_name
120
+ end
121
+
122
+ def hashize_cure_date
123
+ @birth_date = {}
124
+ @created_date = {}
125
+ Rubicure::Girl.config.map do |_k, v|
126
+ v[:birthday].nil? || @birth_date[v[:birthday]] = v
127
+ v[:created_date].nil? || @created_date[v[:created_date]] = v
128
+ end
129
+ end
130
+
131
+ def hashize_movie_date
132
+ @movie_started_date = {}
133
+ Rubicure::Movie.config.map do |_k, v|
134
+ v[:started_date].nil? || @movie_started_date[v[:started_date]] = v
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,71 @@
1
+ # coding: utf-8
2
+
3
+ # Singleton class for cure echo
4
+ class CureEchoManager
5
+ module EchoMode
6
+ TRANSFORM = 1
7
+ ATTACK = 2
8
+ end
9
+
10
+ def initialize
11
+ @in = $stdin
12
+ @out = $stdout
13
+ @err = $stderr
14
+ @cure_name = 'echo'
15
+ @message_mode = EchoMode::TRANSFORM
16
+ end
17
+
18
+ def source_output(source = $stdout)
19
+ @out = source
20
+ end
21
+
22
+ def source_input(source = $stdin)
23
+ @in = source
24
+ end
25
+
26
+ def source_err(source = $stderr)
27
+ @err = source
28
+ end
29
+
30
+ def precure(cure_name = false)
31
+ @cure_name = cure_name || 'echo'
32
+ end
33
+
34
+ def msg_transform(flag = true)
35
+ @message_mode = EchoMode::TRANSFORM if flag
36
+ end
37
+
38
+ def msg_attack(flag = false)
39
+ @message_mode = EchoMode::ATTACK if flag
40
+ end
41
+
42
+ def nosleep(flag = true)
43
+ Rubicure::Girl.sleep_sec = 1
44
+ Rubicure::Girl.sleep_sec = 0 if flag
45
+ end
46
+
47
+ def print_results
48
+ return 1 unless existing_precure?
49
+ if @message_mode == EchoMode::TRANSFORM
50
+ Cure.send(@cure_name.to_sym).transform!
51
+ elsif @message_mode == EchoMode::ATTACK
52
+ Cure.send(@cure_name.to_sym).transform!
53
+ Cure.send(@cure_name.to_sym).attack!
54
+ end
55
+ 0
56
+ end
57
+
58
+ private
59
+
60
+ def existing_precure?
61
+ found_precure = Rubicure::Girl.config.find do |k, _v|
62
+ k == @cure_name.to_sym
63
+ end
64
+ if found_precure
65
+ true
66
+ else
67
+ @err.puts "No such precure #{@cure_name}"
68
+ false
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,75 @@
1
+ # coding: utf-8
2
+ require 'colorize'
3
+
4
+ # Singleton class for cure grep
5
+ # Managing the method for matching and printing.
6
+ class CureGrepManager
7
+ module ColorMode
8
+ NONE = :to_s
9
+ RED = :red
10
+ end
11
+
12
+ def initialize
13
+ @in = $stdin
14
+ @out = $stdout
15
+ @err = $stderr
16
+ @match_method = 'match'
17
+ end
18
+
19
+ def option_only(only_flag)
20
+ @only_matched = only_flag
21
+ @match_method = (only_flag ? 'scan' : 'match')
22
+ end
23
+
24
+ def option_colorize(colorize)
25
+ @str_color = (colorize ? ColorMode::RED : ColorMode::NONE)
26
+ end
27
+
28
+ def source_output(source = $stdout)
29
+ @out = source
30
+ end
31
+
32
+ def source_input(source = nil)
33
+ if source.nil? || source.empty?
34
+ @in = $stdin
35
+ elsif source =~ /^-$/
36
+ # If the file name is "-", use STDIN.
37
+ @in = $stdin
38
+ else
39
+ file(source)
40
+ end
41
+ end
42
+
43
+ def pattern(pat, is_exregex = false)
44
+ if is_exregex
45
+ @pattern = /#{pat}/
46
+ return
47
+ end
48
+ @pattern = /#{Common.pregex2regex(pat)}/
49
+ end
50
+
51
+ def print_results
52
+ exit_status = 1
53
+ @in.each do |line|
54
+ matched_strs = line.send(@match_method, @pattern)
55
+ next unless matched_strs
56
+ exit_status = 0
57
+ if @only_matched
58
+ matched_strs.each { |str| @out.puts str.send(@str_color) }
59
+ else
60
+ @out.puts line.gsub(@pattern, '\0'.send(@str_color))
61
+ end
62
+ end
63
+ exit_status
64
+ end
65
+
66
+ private
67
+
68
+ def file(filename)
69
+ @in = File.open(filename)
70
+ rescue SystemCallError => e
71
+ puts %(class=[#{e.class}] message=[#{e.message}])
72
+ rescue IOError => e
73
+ puts %(class=[#{e.class}] message=[#{e.message}])
74
+ end
75
+ end
@@ -0,0 +1,69 @@
1
+ # coding: utf-8
2
+ #
3
+ # Class of Pikarin Janken
4
+ #
5
+ class CureJankenManager
6
+ def initialize
7
+ # Set the sleep time 0
8
+ Rubicure::Girl.sleep_sec = 0
9
+ # 0: win, 1: lose, 2: aiko
10
+ @result_table = [[2, 0, 1, 1],
11
+ [1, 2, 0, 1],
12
+ [0, 1, 2, 1],
13
+ [0, 0, 0, 2]]
14
+ @result_idx = %w(あなたのかち あなたのまけ あいこ)
15
+ @te_idx = %w(グー チョキ パー グッチョッパー)
16
+ @te_hash = Hash[[@te_idx, (0..3).map(&:to_s)].transpose]
17
+ @buf = []
18
+ @out = $stdout
19
+ end
20
+
21
+ attr_writer :io
22
+
23
+ def source_output(output = $stdout)
24
+ @out = output
25
+ end
26
+
27
+ def puts(input)
28
+ @buf << input
29
+ end
30
+
31
+ def janken
32
+ Cure.peace.io = self
33
+ Cure.peace.janken
34
+ @buf[0..1].each do |msg|
35
+ @out.puts msg
36
+ end
37
+ judge
38
+ end
39
+
40
+ def generated_te
41
+ @buf.last =~ /(#{@te_idx.join('|')})/
42
+ @te_hash[Regexp.last_match(1)].to_i
43
+ end
44
+
45
+ def input_te
46
+ @out.print("1...グー, 2...チョキ, 3...パー : ")
47
+ # TODO: Check input and raise the error.
48
+ player_te = $stdin.gets
49
+ player_te.to_i - 1
50
+ end
51
+
52
+ def judge
53
+ cure_te = generated_te
54
+ player_te = input_te
55
+ result_num = @result_table[player_te][cure_te]
56
+ result = @result_idx[result_num]
57
+ print_results(@te_idx[player_te], @te_idx[cure_te], result)
58
+ result_num
59
+ end
60
+
61
+ def print_results(player_te, cure_te, result)
62
+ @out.puts
63
+ @out.puts 'あなた: ' + player_te
64
+ @out.puts 'キュアピース: ' + cure_te
65
+ @out.puts
66
+ @out.puts '[結果]'
67
+ @out.puts result
68
+ end
69
+ end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ #
3
+ # Class for managing cure tr (translate).
4
+ #
5
+ class CureTranslateManager
6
+ def initialize
7
+ @translate_from = :human_name
8
+ @translate_to = :precure_name
9
+ @out = $stdout
10
+ @in = $stdin
11
+ end
12
+
13
+ attr_writer :out, :in
14
+
15
+ def translate_from_to(from, to)
16
+ @translate_from = pregex2str(from).to_sym
17
+ @translate_to = pregex2str(to).to_sym
18
+ end
19
+
20
+ def str2pregex(str)
21
+ '[:' + str + ':]'
22
+ end
23
+
24
+ def pregex2str(str)
25
+ str.gsub(/\[:(.*):\]/, '\1')
26
+ end
27
+
28
+ def print_results
29
+ # Create precure regular expression
30
+ regex_tag = str2pregex(@translate_from.to_s)
31
+ # Get patterns
32
+ cure_pattern = Common.pregex2regex(regex_tag, true)
33
+ # Get Key-Value
34
+ table = Common.cure_table(@translate_from, @translate_to)
35
+ @in.each do |line|
36
+ updated_line = line.dup
37
+ line.scan(/#{cure_pattern}/).each do |pat|
38
+ updated_line.gsub!(/#{pat[0]}/, table[pat[0]])
39
+ end
40
+ puts updated_line
41
+ end
42
+ 0
43
+ end
44
+ end
@@ -2,7 +2,7 @@ module Cureutils
2
2
  class Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 4
5
+ PATCH = 5
6
6
  PRE = nil
7
7
 
8
8
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cureutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamada, Yasuhiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-26 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,7 +114,7 @@ dependencies:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
116
  version: 4.2.6
117
- description: Commands which can smoothly deal with Precure-related data.
117
+ description: Useful command line tool for Japanese battle heroine Pretty Cure (Precure).
118
118
  email:
119
119
  - greengregson@gmail.com
120
120
  executables:
@@ -137,8 +137,12 @@ files:
137
137
  - img/cureutils_logo.png
138
138
  - lib/cureutils.rb
139
139
  - lib/cureutils/cli.rb
140
- - lib/cureutils/cure_date_checker.rb
141
- - lib/cureutils/janken_controller.rb
140
+ - lib/cureutils/common.rb
141
+ - lib/cureutils/cure_date_manager.rb
142
+ - lib/cureutils/cure_echo_manager.rb
143
+ - lib/cureutils/cure_grep_manager.rb
144
+ - lib/cureutils/cure_janken_manager.rb
145
+ - lib/cureutils/cure_translate_manager.rb
142
146
  - lib/cureutils/version.rb
143
147
  homepage: https://github.com/greymd/cureutils
144
148
  licenses:
@@ -163,5 +167,5 @@ rubyforge_project:
163
167
  rubygems_version: 2.4.5
164
168
  signing_key:
165
169
  specification_version: 4
166
- summary: Useful commands for battle heroine "Pretty Cure (Precure)".
170
+ summary: Useful commands for battle heroine Pretty Cure (Precure).
167
171
  test_files: []
@@ -1,81 +0,0 @@
1
- # coding: utf-8
2
- require 'date'
3
- #
4
- # Date class supports the precure format
5
- #
6
- class CureDateChecker
7
- class << self
8
- def events(date)
9
- create_hash
10
- event_list = [birth_date?(date),
11
- created_date?(date),
12
- movie_date?(date),
13
- series_between(date)].compact
14
- event_list.join('/')
15
- end
16
-
17
- private
18
-
19
- def create_hash
20
- hashize_cure_date unless @birth_date || @created_date
21
- hashize_movie_date unless @movie_started_date
22
- end
23
-
24
- def birth_date?(date)
25
- date_str = date.strftime('%m/%d')
26
- result = @birth_date[date_str]
27
- # format
28
- result.nil? ? nil : "#{result[:human_name]}(#{result[:precure_name]})誕生日"
29
- end
30
-
31
- def created_date?(date)
32
- date_str = date.strftime('%m/%d')
33
- result = @created_date[date_str]
34
- # format
35
- result.nil? ? nil : "#{result[:precure_name]}初登場日"
36
- end
37
-
38
- def movie_date?(date)
39
- result = @created_date[date]
40
- # format
41
- result.nil? ? nil : "#{result[:title]}映画公開日"
42
- end
43
-
44
- def series_between(date)
45
- event_name = nil
46
- Rubicure::Series.config.each do |_k, v|
47
- if v[:started_date] == date
48
- event_name = "#{v[:title]}初放映"
49
- break
50
- end
51
- if v[:ended_date] == date
52
- event_name = "#{v[:title]}最終回"
53
- break
54
- end
55
- # Nobody expects the future. Precure series may be abolished today.
56
- v[:ended_date] = Date.today + 1 if v[:ended_date].nil?
57
- if v[:started_date] < date && date < v[:ended_date]
58
- event_name = "#{v[:title]}放映期間"
59
- break
60
- end
61
- end
62
- event_name
63
- end
64
-
65
- def hashize_cure_date
66
- @birth_date = {}
67
- @created_date = {}
68
- Rubicure::Girl.config.map do |_k, v|
69
- v[:birthday].nil? || @birth_date[v[:birthday]] = v
70
- v[:created_date].nil? || @created_date[v[:created_date]] = v
71
- end
72
- end
73
-
74
- def hashize_movie_date
75
- @movie_started_date = {}
76
- Rubicure::Movie.config.map do |_k, v|
77
- v[:started_date].nil? || @movie_started_date[v[:started_date]] = v
78
- end
79
- end
80
- end
81
- end
@@ -1,68 +0,0 @@
1
- # coding: utf-8
2
- #
3
- # Wrapper class of Pikarin Janken
4
- #
5
- class JankenContoller
6
- class << self
7
- def initialize
8
- # Set the sleep time 0
9
- Rubicure::Girl.sleep_sec = 0
10
- # 0: win, 1: lose, 2: aiko
11
- @result_table = [[2, 0, 1, 1],
12
- [1, 2, 0, 1],
13
- [0, 1, 2, 1],
14
- [0, 0, 0, 2]]
15
- @result_idx = %w(あなたのかち あなたのまけ あいこ)
16
- @te_idx = %w(グー チョキ パー グッチョッパー)
17
- @te_hash = Hash[[@te_idx, (0..3).map(&:to_s)].transpose]
18
- @buf = []
19
- @io = $stdout
20
- end
21
-
22
- attr_writer :io
23
-
24
- def puts(input)
25
- @buf << input
26
- end
27
-
28
- def janken
29
- initialize
30
- Cure.peace.io = self
31
- Cure.peace.janken
32
- @buf[0..1].each do |msg|
33
- @io.puts msg
34
- end
35
- judge
36
- end
37
-
38
- def generated_te
39
- @buf.last =~ /(#{@te_idx.join('|')})/
40
- @te_hash[Regexp.last_match(1)].to_i
41
- end
42
-
43
- def input_te
44
- @io.print("1...グー, 2...チョキ, 3...パー : ")
45
- # TODO: Check input and raise the error.
46
- player_te = $stdin.gets
47
- player_te.to_i - 1
48
- end
49
-
50
- def judge
51
- cure_te = generated_te
52
- player_te = input_te
53
- result_num = @result_table[player_te][cure_te]
54
- result = @result_idx[result_num]
55
- print_results(@te_idx[player_te], @te_idx[cure_te], result)
56
- result_num
57
- end
58
-
59
- def print_results(player_te, cure_te, result)
60
- @io.puts
61
- @io.puts 'あなた: ' + player_te
62
- @io.puts 'キュアピース: ' + cure_te
63
- @io.puts
64
- @io.puts '[結果]'
65
- @io.puts result
66
- end
67
- end
68
- end