Class: AM::Ui

Inherits:
Object
  • Object
show all
Defined in:
lib/ui.rb

Instance Method Summary collapse

Instance Method Details

#add_command_with_last_history(command) ⇒ Object



34
35
36
37
38
# File 'lib/ui.rb', line 34

def add_command_with_last_history(command)
  puts "add command is #{quot(command.strip)}"
  alias_name   = get_alias
  {alias_name => quot(command)}
end

#add_command_with_number(commands) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ui.rb', line 26

def add_command_with_number(commands)
  print 'please input add command number: '
  number     = please_input
  valid?(number, '^[^1-5]', '[error] input using number!')
  alias_name = get_alias
  {alias_name => quot(commands[number.to_i-1].strip)}
end

#del_command_with_number(arr) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/ui.rb', line 40

def del_command_with_number(arr)
  print 'please input delete command number: '
  number = please_input
  valid?(number, '^[^0-9]', '[error] input using number!')
  # return delete hash key
  arr[number.to_i-1][1] if arr.length >= number.to_i
end

#get_aliasObject



48
49
50
51
52
53
# File 'lib/ui.rb', line 48

def get_alias
  print "please input add command alias: "
  name = please_input
  valid?(name, '[^\w-]', '[error] input using a-z or 0-9 or _ or -')
  name.strip
end

#max(a, b) ⇒ Object



73
74
75
# File 'lib/ui.rb', line 73

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

#please_inputObject



62
63
64
65
66
67
# File 'lib/ui.rb', line 62

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


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ui.rb', line 6

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
  arr = []                                           #use delete number
  AM::p1("current commands of the config")
  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}"
    arr << [i,k]
  end unless config.al.empty?
  AM::p1
  arr
end


20
21
22
23
24
# File 'lib/ui.rb', line 20

def print_last_commands(commands)
  commands.each_with_index  do |c,i|
    puts " #{(i+1).to_s} : #{c.to_s}"
  end unless commands.empty?
end

#quot(val) ⇒ Object



69
70
71
# File 'lib/ui.rb', line 69

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

#valid?(val, pattern, message) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(val, pattern, message)
  if /#{pattern}/ =~ val || val.length == 0
    puts message
    exit
  end
end