Module: HTTY::CLI::Display

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

Instance Method Details

#breakObject



106
107
108
# File 'lib/htty/cli/display.rb', line 106

def break
  puts ''
end

#format(string, *attributes) ⇒ Object



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|
    "\e[#{FORMATS[a]}m"
  end
  segments << string
  segments << "\e[0m"
  segments.join ''
end

#formatted_prompt_for(request) ⇒ Object



85
86
87
# File 'lib/htty/cli/display.rb', line 85

def formatted_prompt_for(request)
  format_request_uri(request.uri) + normal('> ')
end

#indent(string, column = 2) ⇒ Object



58
59
60
# File 'lib/htty/cli/display.rb', line 58

def indent(string, column=2)
  "#{' ' * column}#{string}"
end

#logotypeObject



62
63
64
# File 'lib/htty/cli/display.rb', line 62

def logotype
  format ' htty ', :bold, :background_dark_red, :foreground_yellow
end

#normal(string) ⇒ Object



70
71
72
# File 'lib/htty/cli/display.rb', line 70

def normal(string)
  return string
end

#notice(string) ⇒ Object



66
67
68
# File 'lib/htty/cli/display.rb', line 66

def notice(string)
  "*** #{string}"
end

#pluralize(word, number) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/htty/cli/display.rb', line 74

def pluralize(word, number)
  case number
    when 0
      "no #{word}s"
    when 1
      "1 #{word}"
    else
      "#{number} #{word}s"
  end
end

#rescuing_from(*exception_classes) ⇒ Object



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.message))
  nil
end

#say(message, style = :normal) ⇒ Object



89
90
91
# File 'lib/htty/cli/display.rb', line 89

def say(message, style=:normal)
  puts send(style, notice(message))
end

#say_goodbyeObject



93
94
95
# File 'lib/htty/cli/display.rb', line 93

def say_goodbye
  say 'Happy Trails To You!'
end

#say_header(message, style = :normal) ⇒ Object



97
98
99
# File 'lib/htty/cli/display.rb', line 97

def say_header(message, style=:normal)
  puts send(style, notice('')) + highlight(message)
end

#say_helloObject



101
102
103
104
# File 'lib/htty/cli/display.rb', line 101

def say_hello
  puts normal(notice('Welcome to ')) + logotype + normal(', the ') +
       strong('HTTP TTY') + normal('. Heck To The Yeah!')
end

#show_headers(headers, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/htty/cli/display.rb', line 110

def show_headers(headers, options={})
  show_asterisk_next_to   = (options[:show_asterisk_next_to] || '').downcase
  show_mercantile_next_to = (options[:show_mercantile_next_to] || '').downcase

  asterisk_symbol, mercantile_symbol = nil, nil
  margin = headers.inject 0 do |result, header|
    header_name = header.first.downcase
    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.downcase == show_asterisk_next_to)   ? asterisk_symbol   : nil
    mercantile = (name.downcase == 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

#show_request(request) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/htty/cli/display.rb', line 132

def show_request(request)
  method = format(" #{request.request_method.to_s.upcase} ", :inverse)
  print "#{method} "
  cookies_asterisk = request.cookies.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) + cookies_asterisk,
        "#{body_size} body"].join(' -- ')
end

#show_response(response) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/htty/cli/display.rb', line 143

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 ' '
  cookies_asterisk = response.cookies.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) + cookies_asterisk,
        "#{body_size} body"].join(' -- '))
end

#strong(string) ⇒ Object



165
166
167
# File 'lib/htty/cli/display.rb', line 165

def strong(string)
  format string, :bold
end

#word_wrap(text, column = nil) ⇒ Object



169
170
171
# File 'lib/htty/cli/display.rb', line 169

def word_wrap(text, column=nil)
  word_wrap_indented(text, (0..(column || 80)))
end

#word_wrap_indented(text, columns = 2..80) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/htty/cli/display.rb', line 175

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