Module: HTTY::CLI::Display
- Included in:
- HTTY::CLI, Command, Commands::BodySet, Commands::Cookies, Commands::CookiesUse, Commands::HeadersRequest, Commands::HeadersResponse, Commands::Help, Commands::History, Commands::HistoryVerbose, Commands::QueryAdd, Commands::QueryRemove, Commands::QuerySet, Commands::Reuse, Commands::SslVerification, Commands::Status, HTTPMethodCommand, UrlEscaping
- Defined in:
- lib/htty/cli/display.rb
Overview
Encapsulates the display logic of htty's command-line interface.
Constant Summary
- FORMATS =
{:bold => '1', :underlined => '4', :blinking => '5', :inverse => '7', :foreground_black => '30', :foreground_dark_red => '31', :foreground_dark_green => '32', :foreground_dark_yellow => '33', :foreground_dark_blue => '34', :foreground_dark_magenta => '35', :foreground_dark_cyan => '36', :foreground_light_gray => '37', :foreground_dark_default => '39', :foreground_dark_gray => '1;30', :foreground_red => '1;31', :foreground_green => '1;32', :foreground_yellow => '1;33', :foreground_blue => '1;34', :foreground_magenta => '1;35', :foreground_cyan => '1;36', :foreground_white => '1;37', :foreground_default => '1;39', :background_black => '40', :background_dark_red => '41', :background_dark_green => '42', :background_dark_yellow => '43', :background_dark_blue => '44', :background_dark_magenta => '45', :background_dark_cyan => '46', :background_light_gray => '47', :background_default => '49'}
Instance Method Summary (collapse)
- - (Object) format(string, *attributes)
- - (Object) indent(string, column = 2)
- - (Object) logotype
- - (Object) normal(string)
- - (Object) notice(string)
- - (Object) pluralize(word, number)
- - (Object) prompt(request)
- - (Object) rescuing_from(*exception_classes)
- - (Object) say(message, style = :normal)
- - (Object) say_goodbye
- - (Object) say_header(message, style = :normal)
- - (Object) say_hello
- - (Object) show_headers(headers, options = {})
- - (Object) show_request(request)
- - (Object) show_response(response)
- - (Object) strong(string)
- - (Object) word_wrap(text, column = nil)
- - (Object) word_wrap_indented(text, columns = 2..80)
Instance Method Details
- (Object) format(string, *attributes)
40 41 42 43 44 45 46 47 |
# File 'lib/htty/cli/display.rb', line 40 def format(string, *attributes) segments = attributes.collect do |a| "\x1b[#{FORMATS[a]}m" end segments << string segments << "\x1b[0m" segments.join '' end |
- (Object) indent(string, column = 2)
58 59 60 |
# File 'lib/htty/cli/display.rb', line 58 def indent(string, column=2) "#{' ' * column}#{string}" end |
- (Object) logotype
62 63 64 |
# File 'lib/htty/cli/display.rb', line 62 def logotype format ' htty ', :bold, :background_dark_red, :foreground_yellow end |
- (Object) normal(string)
70 71 72 73 |
# File 'lib/htty/cli/display.rb', line 70 def normal(string) return string # format string, :foreground_dark_default end |
- (Object) notice(string)
66 67 68 |
# File 'lib/htty/cli/display.rb', line 66 def notice(string) "*** #{string}" end |
- (Object) pluralize(word, number)
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/htty/cli/display.rb', line 75 def pluralize(word, number) case number when 0 "no #{word}s" when 1 "1 #{word}" else "#{number} #{word}s" end end |
- (Object) prompt(request)
86 87 88 |
# File 'lib/htty/cli/display.rb', line 86 def prompt(request) format_request_uri(request.uri) + normal('> ') end |
- (Object) rescuing_from(*exception_classes)
49 50 51 52 53 54 55 56 |
# File 'lib/htty/cli/display.rb', line 49 def rescuing_from(*exception_classes) yield rescue Interrupt nil rescue *exception_classes => e $stderr.puts notice(sentence_case(e.)) nil end |
- (Object) say(message, style = :normal)
90 91 92 |
# File 'lib/htty/cli/display.rb', line 90 def say(, style=:normal) puts send(style, notice()) end |
- (Object) say_goodbye
94 95 96 |
# File 'lib/htty/cli/display.rb', line 94 def say_goodbye say 'Happy Trails To You!' end |
- (Object) say_header(message, style = :normal)
98 99 100 |
# File 'lib/htty/cli/display.rb', line 98 def say_header(, style=:normal) puts send(style, notice('')) + highlight() end |
- (Object) say_hello
102 103 104 105 |
# File 'lib/htty/cli/display.rb', line 102 def say_hello puts normal(notice('Welcome to ')) + logotype + normal(', the ') + strong('HTTP TTY') + normal('. Heck To The Yeah!') end |
- (Object) show_headers(headers, options = {})
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/htty/cli/display.rb', line 107 def show_headers(headers, ={}) show_asterisk_next_to = [:show_asterisk_next_to] show_mercantile_next_to = [:show_mercantile_next_to] asterisk_symbol, mercantile_symbol = nil, nil margin = headers.inject 0 do |result, header| header_name = header.first asterisk_symbol ||= (header_name == show_asterisk_next_to) ? '*' : nil mercantile_symbol ||= (header_name == show_mercantile_next_to) ? '@' : nil asterisk = (header_name == show_asterisk_next_to) ? asterisk_symbol : '' mercantile = (header_name == show_mercantile_next_to) ? mercantile_symbol : '' [(header_name.length + [asterisk.length, mercantile.length].max), result].max end headers.each do |name, value| asterisk = (name == show_asterisk_next_to) ? asterisk_symbol : nil mercantile = (name == show_mercantile_next_to) ? mercantile_symbol : nil justified_name = name.rjust margin - [asterisk.to_s.length, mercantile.to_s.length].max puts "#{justified_name}:#{strong(asterisk || mercantile)} " + value end end |
- (Object) show_request(request)
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/htty/cli/display.rb', line 130 def show_request(request) method = format(" #{request.request_method.to_s.upcase} ", :inverse) print "#{method} " = request..empty? ? '' : strong('*') body_length = request.body.to_s.length body_size = body_length.zero? ? 'empty' : "#{body_length}-character" puts [format_request_uri(request.uri), pluralize('header', request.headers.length) + , "#{body_size} body"].join(' -- ') end |
- (Object) show_response(response)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/htty/cli/display.rb', line 141 def show_response(response) code, description = response.status case code.to_i when 100...200, 300...400 print format(" #{code} ", :background_dark_blue, :foreground_white) when 200...300 print format(" #{code} ", :background_dark_green, :foreground_black) when 500...600 print format(" #{code} ", :inverse, :blinking, :background_black, :foreground_yellow) else print format(" #{code} ", :background_dark_red, :foreground_white) end print ' ' = response..empty? ? '' : strong('*') body_length = response.body.to_s.length body_size = body_length.zero? ? 'empty' : "#{body_length}-character" puts([description, pluralize('header', response.headers.length) + , "#{body_size} body"].join(' -- ')) end |
- (Object) strong(string)
163 164 165 |
# File 'lib/htty/cli/display.rb', line 163 def strong(string) format string, :bold end |
- (Object) word_wrap(text, column = nil)
167 168 169 |
# File 'lib/htty/cli/display.rb', line 167 def word_wrap(text, column=nil) word_wrap_indented(text, (0..(column || 80))) end |
- (Object) word_wrap_indented(text, columns = 2..80)
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/htty/cli/display.rb', line 173 def word_wrap_indented(text, columns=2..80) indent_by, wrap_at = columns.min, columns.max - columns.min text.split("\n").collect do |line| (wrap_at < line.length) ? line.gsub(/(.{1,#{wrap_at}})(\s+|$)/, "\\1\n").strip : line end.join("\n").split("\n").collect do |line| indent line, indent_by end.join "\n" end |