Class: RecordOnChain::Cli

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

Constant Summary collapse

ATTR_OFF =
"\e[0m".freeze
ENHANCE =
"\e[1m".freeze
UNDERLINE =

style : bold

"\e[4m".freeze
ERROR =

style : underline

"\e[31m\e[1m".freeze
SUCCESS =

color : red style : bold

"\e[32m\e[1m".freeze
CAUTION =

color : green style : bold

"\e[43m\e[30m\e[1m".freeze
ATTENTION =

background : yellow color : black style : bold

"\e[40m\e[36m\e[1m"

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, output = $stdout) ⇒ Cli

Returns a new instance of Cli.



5
6
7
8
# File 'lib/record_on_chain/cli.rb', line 5

def initialize( input=$stdin , output=$stdout )
  @input  = input
  @output = output
end

Instance Method Details

#agree(what) ⇒ Object



56
57
58
# File 'lib/record_on_chain/cli.rb', line 56

def agree( what )
  return get_highline.agree( "Are you sure you want to #{what}? (y)es or (n)o" )
end

#blank_line(size = 1) ⇒ Object



52
53
54
# File 'lib/record_on_chain/cli.rb', line 52

def blank_line( size= 1 )
  puts( "\n"*size )
end

#decide_passwordObject



60
61
62
63
64
65
66
67
# File 'lib/record_on_chain/cli.rb', line 60

def decide_password
  user_password = password_operation_base( 5 )do |answer|
    # Enter pass again to see if it is equal
    conf_answer = get_highline.ask("- Please enter your password again (confirm)"){ |q| q.echo = "*" }
    answer == conf_answer
  end
  return user_password
end

#encrypt_with_password(decrypt_func) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/record_on_chain/cli.rb', line 69

def encrypt_with_password( decrypt_func )
  decrypted_secret = password_operation_base( 3 ) do |attempt|
    decrypted = decrypt_func.call( attempt )
    if decrypted.empty? then
      false # failure
    else
      # you can get decrypted data from attempt after valid_process
      attempt.replace( decrypted )
      true # success
    end
  end
  return decrypted_secret
end

#format_hash(hash, indent = 0, split_word = " : ") ⇒ Object

example

alice:“alice”,bob:“bob”,dylan:“dylan”,carol:“carol”

alice : alice bob : bob dylan : dylan carol : carol



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/record_on_chain/cli.rb', line 96

def format_hash( hash, indent= 0, split_word=" : "  )
  # get max :key word length
  max_key_length = hash.map{ |pair| pair.first.size }.max{ |a,b| a <=> b }
  # initialize output
  output = ""
  hash.each do |key,val|
    output << " " * indent
    output << padding( key.to_s , max_key_length )
    output << split_word
    output << val.to_s
    output << "\n"
  end
  return output
end

#inObject



10
# File 'lib/record_on_chain/cli.rb', line 10

def in ; return @input ; end

#outObject



11
# File 'lib/record_on_chain/cli.rb', line 11

def out; return @output; end

#puts_hash(hash, attribute = nil, indent = 0) ⇒ Object



83
84
85
86
87
# File 'lib/record_on_chain/cli.rb', line 83

def puts_hash( hash , attribute= nil, indent= 0 )
  formatted = format_hash( hash , indent )
  formatted = send( "#{attribute}_str" , formatted ) unless attribute.nil?
  @output.puts( formatted )
end