Module: Keybox::TermIO

Included in:
Application::PasswordSafe
Defined in:
lib/keybox/term_io.rb

Overview

including this module assumes that the class included has This module also assumes that stty is available

Constant Summary collapse

ESCAPE =
"\e"
BOLD_ON =
"[1m"
RESET =
"[0m"
FG_BLACK =
"[30m"
FG_RED =
"[31m"
FG_GREEN =
"[32m"
FG_YELLOW =
"[33m"
FG_BLUE =
"[34m"
FG_MAGENTA =
"[35m"
FG_CYAN =
"[36m"
FG_WHITE =
"[37m"
COLORS =
{ 
:black     => FG_BLACK,
:red       => FG_RED,
:green     => FG_GREEN,
:yellow    => FG_YELLOW,
:blue      => FG_BLUE,
:magenta   => FG_MAGENTA,
:cyan      => FG_CYAN,
:white     => FG_WHITE,
}
VALID_COLORS =
COLORS.keys()
STTY =
"stty"
STTY_SAVE_CMD =
"#{STTY} -g"
STTY_RAW_CMD =
"#{STTY} raw -echo isig"
EOL_CHARS =

ā€˜nā€™

[10, # '\n'
             13, # '\r'
]

Instance Method Summary collapse

Instance Method Details

#color_print(text, color, bold = true) ⇒ Object



159
160
161
# File 'lib/keybox/term_io.rb', line 159

def color_print(text,color, bold = true)
    @stdout.print colorize_if_io_isatty(@stdout,text,color,bold)
end

#color_puts(text, color, bold = true) ⇒ Object



155
156
157
# File 'lib/keybox/term_io.rb', line 155

def color_puts(text, color, bold = true)
    @stdout.puts colorize_if_io_isatty(@stdout,text,color,bold)
end

#colorize(text, color, bold = true) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/keybox/term_io.rb', line 137

def colorize(text,color,bold=true)
    before = ""
    after  = ""
    if VALID_COLORS.include?(color) then
        before = ESCAPE + COLORS[color]
        before = ESCAPE + BOLD_ON + before if bold
        after  = ESCAPE + RESET
    end
    "#{before}#{text}#{after}"
end

#colorize_if_io_isatty(io, text, color, bold) ⇒ Object



148
149
150
151
152
153
# File 'lib/keybox/term_io.rb', line 148

def colorize_if_io_isatty(io,text,color,bold)
    if io.tty? then
        text = colorize(text,color,bold)
    end
    text
end

#get_one_charObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/keybox/term_io.rb', line 87

def get_one_char
    stty_original = %x{#{STTY_SAVE_CMD}}
    char = nil
    begin
        system STTY_RAW_CMD
        char = @stdin.getc
    ensure
        system "#{STTY} #{stty_original}"
    end

    return char
end

#has_stty?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/keybox/term_io.rb', line 133

def has_stty?
    system "which stty > /dev/null 2>&1"
end

#prompt(p, echo = true, validate = false, width = 20) ⇒ Object

prompt for input, returning what was typed. If echo is false, then ā€˜*ā€™ is printed out for each character typed in. If it is any other character then that is output instead.

If validate is set to true, then it will prompt twice and make sure that the two values match



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/keybox/term_io.rb', line 52

def prompt(p,echo = true, validate = false, width = 20) 
    validated = false
    line = ""
    extra_prompt = " (again)"
    original_prompt = p
    validation_prompt = original_prompt + extra_prompt
    width += extra_prompt.length

    until validated do
        line = prompt_and_return(original_prompt.rjust(width),echo)

        # if we are validating then prompt again to validate
        if validate then
            v = prompt_and_return(validation_prompt.rjust(width),echo)
            if v != line then
                color_puts "Entries do not match, try again.", :red
            else
                validated = true
            end
        else 
            validated = true
        end
    end
    return line
end

#prompt_and_return(the_prompt, echo) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/keybox/term_io.rb', line 100

def prompt_and_return(the_prompt,echo)
    line = ""
    color_print "#{the_prompt} : ", :white
    if echo != true then

        echo_char = echo || '*'

        if has_stty? then
            stty_original = %x{#{STTY_SAVE_CMD}}

            begin
                system STTY_RAW_CMD
                while char = @stdin.getc
                    line << char
                    break if EOL_CHARS.include? char 
                    @stdout.putc echo_char
                end
            ensure
                system "#{STTY} #{stty_original}"
            end
            @stdout.puts
        end
    else
        line = @stdin.gets
    end

    # if we got end of file or some other input resulting in
    # line becoming nil then set it to the empty string
    line = line || ""

    return line.rstrip
end

#prompt_y_n(p) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/keybox/term_io.rb', line 78

def prompt_y_n(p)
    response = prompt(p)
    if response.size > 0 and response.downcase[0].chr == 'y' then
        true
    else
        false
    end
end