Class: Cureutils::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/cureutils/cli.rb

Overview

The class represents the cli interface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cureutils/cli.rb', line 22

def exit_on_failure?
  true
end

Instance Method Details

#date(fmt = '+%F %H:%M:%S @P') ⇒ Object

Original date command’s default is ‘+%a %b %e %H:%M:%S %Z %Y @P’ However, I would like to adopt this setting.



135
136
137
138
139
140
# File 'lib/cureutils/cli.rb', line 135

def date(fmt = '+%F %H:%M:%S @P')
  # -d, --date=STRING (YYYY-MM-DD or +-N days)
  print_time = create_time_obj(options[:date])
  updated_fmt = update_fmt(print_time, fmt)
  puts print_time.strftime(updated_fmt)
end

#echoObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cureutils/cli.rb', line 111

def echo
  cure_name = options[:precure] || 'echo'
  message_mode = EchoMode::TRANSFORM
  message_mode = EchoMode::TRANSFORM if options[:transform]
  message_mode = EchoMode::ATTACK if options[:attack]
  Rubicure::Girl.sleep_sec = 0 if options[:quick]
  cure = Rubicure::Girl.config.find { |k, _v| k == cure_name.to_sym }
  unless cure
    $stderr.puts "No such precure #{cure_name}"
    exit(1)
  end
  if message_mode == EchoMode::TRANSFORM
    Cure.send(cure_name.to_sym).transform!
  elsif message_mode == EchoMode::ATTACK
    Cure.send(cure_name.to_sym).transform!
    Cure.send(cure_name.to_sym).attack!
  end
end

#girlsObject



38
39
40
41
42
# File 'lib/cureutils/cli.rb', line 38

def girls
  Rubicure::Girl.config.map { |_k, v| v[:human_name] }.uniq.each do |v|
    puts v
  end
end

#grep(pat = '[:precure_name:]', filename = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cureutils/cli.rb', line 54

def grep(pat = '[:precure_name:]', filename = nil)
  # Check whether the file is given or not
  @input = input_from(filename)
  extended_pat = options['extended-regexp'.to_sym]
  if extended_pat
    cure_pat = extended_pat
  else
    cure_pat = pregex2regex(pat)
  end
  # Check the file discriptor and check the pipe exists or not.
  pipe_flg = !$stdout.isatty
  if options['only-matching'.to_sym]
    if pipe_flg
      @input.each do |line|
        matched_strs = line.scan(/#{cure_pat}/)
        matched_strs.empty? || matched_strs.each do |str|
          puts str
        end
      end
    else
      @input.each do |line|
        matched_strs = line.scan(/#{cure_pat}/)
        matched_strs.empty? || matched_strs.each do |str|
          puts str.red
        end
      end
    end
  else
    if pipe_flg
      @input.each do |line|
        puts line.gsub(/#{cure_pat}/, '\0') if line =~ /#{cure_pat}/
      end
    else
      @input.each do |line|
        puts line.gsub(/#{cure_pat}/, '\0'.red) if line =~ /#{cure_pat}/
      end
    end
  end
end

#humanizeObject



33
34
35
# File 'lib/cureutils/cli.rb', line 33

def humanize
  print_converted_text($stdin, :precure_name, :human_name)
end

#jankenObject



143
144
145
146
# File 'lib/cureutils/cli.rb', line 143

def janken
  judge = JankenContoller.janken
  exit(judge.to_i)
end

#precuresObject



45
46
47
48
49
# File 'lib/cureutils/cli.rb', line 45

def precures
  Rubicure::Girl.config.map { |_k, v| v[:precure_name] }.uniq.each do |v|
    puts v
  end
end

#tr(pat_from = '[:precure_name:]', pat_to = '[:human_name:]') ⇒ Object



95
96
97
98
99
# File 'lib/cureutils/cli.rb', line 95

def tr(pat_from = '[:precure_name:]', pat_to = '[:human_name:]')
  pat_from = pregex2str(pat_from).to_sym
  pat_to = pregex2str(pat_to).to_sym
  print_converted_text($stdin, pat_from, pat_to)
end

#transformObject



28
29
30
# File 'lib/cureutils/cli.rb', line 28

def transform
  print_converted_text($stdin, :human_name, :precure_name)
end