Class: AM::Ui

Inherits:
Object
  • Object
show all
Includes:
MessageControl, Validate
Defined in:
lib/ui.rb

Constant Summary

Constants included from MessageControl

MessageControl::ERROR_MESSAGE, MessageControl::NOTICE_MESSAGE, MessageControl::WARNING_MESSAGE

Instance Method Summary collapse

Methods included from Validate

#uniq?, #valid?

Methods included from MessageControl

#after_sepalate, #before_sepalate, #error, #notice, #print_message, #warning

Instance Method Details

#add_command_with_number(commands) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/ui.rb', line 32

def add_command_with_number(commands)
  number = get_number

  if number.to_i > TAIL_LINE
    warning(:validate_number_range, TAIL_LINE)
  else
    alias_name = get_alias
    {alias_name => quot(commands[number.to_i-1].strip)}
  end
end

#delete_command_with_number(config) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ui.rb', line 43

def delete_command_with_number(config)
  number = get_number
  if config.al.length >= number.to_i && config.al.key?(config.al.to_a[number.to_i-1][0])
    config.al.to_a[number.to_i-1][0]
  else
    warning(:empty_config_number)
  end
end

#get_aliasObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/ui.rb', line 63

def get_alias
  print "please input command alias name: "
  alias_name = get_stdin
  if valid?(alias_name, '^[\w-]+$')
    alias_name.strip
  else
    warning(:validate_alias)
    get_alias
  end
end

#get_numberObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/ui.rb', line 52

def get_number
  print 'please select number: '
  number = get_stdin
  if valid?(number, "^[^0-9]+$") || number.to_i <= 0
    warning(:validate_number)
    get_number
  else
    number
  end
end

#get_stdinObject



75
76
77
78
79
80
# File 'lib/ui.rb', line 75

def get_stdin
  while val = STDIN.gets
    break if /\n$/ =~ val
  end
  val.strip
end

#max(a, b) ⇒ Object



86
87
88
# File 'lib/ui.rb', line 86

def max(a, b)
  (a.to_i > b.to_i) ? a : b
end


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ui.rb', line 10

def print_current_config(config)
  aml = config.al.max_by{|c| c[0].length }[0].length #alias max length
  iml = config.al.length.to_s.length                 #index max length

  puts 'current registered alias'
  config.al.each_with_index do|(k,v),i|
    # 1: name=command
    puts " #{' '*(iml - (i+1).to_s.length)}#{(i+1).to_s} : #{k.to_s}#{' '*(aml-k.length)} = #{v.to_s}"
  end
  after_sepalate
end


22
23
24
25
26
27
28
29
30
# File 'lib/ui.rb', line 22

def print_last_commands(commands)
  iml = commands.length.to_s.length                 #index max length

  puts 'current history commands'
  commands.each_with_index  do |c,i|
    puts " #{' '*(iml - (i+1).to_s.length)}#{(i+1).to_s} : #{c.to_s}"
  end
  after_sepalate
end

#quot(val) ⇒ Object



82
83
84
# File 'lib/ui.rb', line 82

def quot(val)
  "'#{val.to_s}'"
end