Class: Aspera::Cli::Formatter
- Inherits:
-
Object
- Object
- Aspera::Cli::Formatter
- Defined in:
- lib/aspera/cli/formatter.rb
Overview
Take care of output
Constant Summary collapse
- ERROR_FLASH =
prefix to display error messages in user messages (terminal)
'ERROR:'.bg_red.gray.blink.freeze
- WARNING_FLASH =
'WARNING:'.bg_brown.black.blink.freeze
- HINT_FLASH =
'HINT:'.bg_green.gray.blink.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#check_row(row) ⇒ Object
for transfer spec table, build line for display.
- #declare_options(options) ⇒ Object
- #display_item_count(number, total) ⇒ Object
-
#display_message(message_level, message, hide_secrets: true) ⇒ Object
main output method data: for requested data, not displayed if level==error info: additional info, displayed if level==info error: always displayed on stderr.
-
#display_results(type:, data: nil, total: nil, fields: nil, name: nil) ⇒ Object
this method displays the results, especially the table format.
- #display_status(status, **kwopt) ⇒ Object
-
#hide_secrets(data) ⇒ Object
hides secrets in Hash or Array.
- #hide_secrets? ⇒ Boolean
-
#initialize ⇒ Formatter
constructor
initialize the formatter.
-
#long_operation_running(title = '') ⇒ Object
call this after REST calls if several api calls are expected.
- #long_operation_terminated ⇒ Object
-
#option_handler(option_symbol, operation, value = nil) ⇒ Object
method accessed by option manager options are: format, output, display, fields, select, table_style, flat_hash, multi_single.
-
#special_format(what) ⇒ Object
Highlight special values on terminal.
-
#status_image(blob) ⇒ Object
Text suitable to display an image from url.
Constructor Details
#initialize ⇒ Formatter
initialize the formatter
127 128 129 130 |
# File 'lib/aspera/cli/formatter.rb', line 127 def initialize = {} @spinner = nil end |
Class Method Details
.all_but(list) ⇒ Object
103 104 105 106 |
# File 'lib/aspera/cli/formatter.rb', line 103 def all_but(list) list = [list] unless list.is_a?(Array) return list.map{ |i| "#{FIELDS_LESS}#{i}"}.unshift(SpecialValues::ALL) end |
.tick(yes) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/aspera/cli/formatter.rb', line 108 def tick(yes) result = if Environment.instance.terminal_supports_unicode? if yes "\u2713" else "\u2717" end elsif yes 'Y' else ' ' end return result.green if yes return result.red end |
Instance Method Details
#check_row(row) ⇒ Object
for transfer spec table, build line for display
139 140 141 142 143 |
# File 'lib/aspera/cli/formatter.rb', line 139 def check_row(row) row.each_key do |k| row[k] = row[k].map{ |i| WordWrap.ww(i.to_s, 120).chomp}.join("\n") if row[k].is_a?(Array) end end |
#declare_options(options) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/aspera/cli/formatter.rb', line 161 def () default_table_style = if Environment.instance.terminal_supports_unicode? {border: :unicode_round} else {} end .declare(:format, 'Output format', values: DISPLAY_FORMATS, handler: {o: self, m: :option_handler}, default: :table) .declare(:output, 'Destination for results', types: String, handler: {o: self, m: :option_handler}) .declare(:display, 'Output only some information', values: DISPLAY_LEVELS, handler: {o: self, m: :option_handler}, default: :info) .declare( :fields, "Comma separated list of: fields, or #{SpecialValues::ALL}, or #{SpecialValues::DEF}", handler: {o: self, m: :option_handler}, types: [String, Array, Regexp, Proc], default: SpecialValues::DEF) .declare(:select, 'Select only some items in lists: column, value', types: [Hash, Proc], handler: {o: self, m: :option_handler}) .declare(:table_style, 'Table display style', types: [Hash], handler: {o: self, m: :option_handler}, default: default_table_style) .declare(:flat_hash, '(Table) Display deep values as additional keys', values: :bool, handler: {o: self, m: :option_handler}, default: true) .declare( :multi_single, '(Table) Control how object list is displayed as single table, or multiple objects', values: i[no yes single], handler: {o: self, m: :option_handler}, default: :no) .declare(:show_secrets, 'Show secrets on command output', values: :bool, handler: {o: self, m: :option_handler}, default: false) .declare(:image, 'Options for image display', types: Hash, handler: {o: self, m: :option_handler}, default: {}) end |
#display_item_count(number, total) ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/aspera/cli/formatter.rb', line 230 def display_item_count(number, total) number = number.to_i total = total.to_i return if total.eql?(0) && number.eql?(0) count_msg = "Items: #{number}/#{total}" count_msg = count_msg.bg_red unless number.eql?(total) display_status(count_msg) end |
#display_message(message_level, message, hide_secrets: true) ⇒ Object
main output method data: for requested data, not displayed if level==error info: additional info, displayed if level==info error: always displayed on stderr
216 217 218 219 220 221 222 223 224 |
# File 'lib/aspera/cli/formatter.rb', line 216 def (, , hide_secrets: true) = SecretHider.hide_secrets_in_string() if hide_secrets && .is_a?(String) && hide_secrets? case when :data then $stdout.puts() unless [:display].eql?(:error) when :info then $stdout.puts() if [:display].eql?(:info) when :error then $stderr.puts() else Aspera.error_unexpected_value() end end |
#display_results(type:, data: nil, total: nil, fields: nil, name: nil) ⇒ Object
this method displays the results, especially the table format
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/aspera/cli/formatter.rb', line 254 def display_results(type:, data: nil, total: nil, fields: nil, name: nil) Log.log.debug{"display_results: #{type} class=#{data.class}"} Log.log.trace1{"display_results:data=#{data}"} Aspera.assert_type(type, Symbol){'result must have type'} Aspera.assert(!data.nil? || i[empty nothing].include?(type)){'result must have data'} display_item_count(data.length, total) unless total.nil? hide_secrets(data) data = SecretHider.hide_secrets_in_string(data) if data.is_a?(String) && hide_secrets? case [:format] when :text (:data, data.to_s) when :nagios Nagios.process(data) when :ruby (:data, PP.pp(filter_list_on_fields(data), +'')) when :json (:data, JSON.generate(filter_list_on_fields(data))) when :jsonpp (:data, JSON.pretty_generate(filter_list_on_fields(data))) when :yaml (:data, YAML.dump(filter_list_on_fields(data))) when :image # assume it is an url url = data case type when :single_object, :object_list url = [url] if type.eql?(:single_object) raise 'image display requires a single result' unless url.length == 1 fields = compute_fields(url, fields) raise 'select a field to display' unless fields.length == 1 url = url.first raise 'no such field' unless url.key?(fields.first) url = url[fields.first] end raise "not url: #{url.class} #{url}" unless url.is_a?(String) (:data, status_image(url)) when :table, :csv case type when :object_list, :single_object obj_list = data if type.eql?(:single_object) obj_list = [obj_list] end Aspera.assert_type(obj_list, Array) Aspera.assert(obj_list.all?(Hash)){"expecting Array of Hash: #{obj_list.inspect}"} # :object_list is an array of hash tables, where key=colum name obj_list = obj_list.map{ |obj| Flattener.new(self).flatten(obj)} if [:flat_hash] display_table(obj_list, compute_fields(obj_list, fields), single: type.eql?(:single_object)) when :value_list # :value_list is a simple array of values, name of column provided in the :name display_table(data.map{ |i| {name => i}}, [name]) when :empty # no table (:info, special_format('empty')) return when :nothing # no result expected Log.log.debug('no result expected') when :status # no table # :status displays a simple message (:info, data) when :text # no table # :status displays a simple message (:data, data) else raise "unknown data type: #{type}" end else raise "not expected: #{@options[:format]}" end end |
#display_status(status, **kwopt) ⇒ Object
226 227 228 |
# File 'lib/aspera/cli/formatter.rb', line 226 def display_status(status, **kwopt) (:info, status, **kwopt) end |
#hide_secrets(data) ⇒ Object
hides secrets in Hash or Array
244 245 246 |
# File 'lib/aspera/cli/formatter.rb', line 244 def hide_secrets(data) SecretHider.deep_remove_secret(data) if hide_secrets? end |
#hide_secrets? ⇒ Boolean
239 240 241 |
# File 'lib/aspera/cli/formatter.rb', line 239 def hide_secrets? ![:show_secrets] && ![:display].eql?(:data) end |
#long_operation_running(title = '') ⇒ Object
call this after REST calls if several api calls are expected
146 147 148 149 150 151 152 153 154 |
# File 'lib/aspera/cli/formatter.rb', line 146 def long_operation_running(title = '') return unless Environment.terminal? if @spinner.nil? @spinner = TTY::Spinner.new('[:spinner] :title', format: :classic) @spinner.start end @spinner.update(title: title) @spinner.spin end |
#long_operation_terminated ⇒ Object
156 157 158 159 |
# File 'lib/aspera/cli/formatter.rb', line 156 def long_operation_terminated @spinner&.stop @spinner = nil end |
#option_handler(option_symbol, operation, value = nil) ⇒ Object
method accessed by option manager options are: format, output, display, fields, select, table_style, flat_hash, multi_single
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/aspera/cli/formatter.rb', line 186 def option_handler(option_symbol, operation, value=nil) Aspera.assert_values(operation, i[set get]) case operation when :set [option_symbol] = value # special handling of some options case option_symbol when :output $stdout = if value.eql?('-') STDOUT # rubocop:disable Style/GlobalStdStream else File.open(value, 'w') end when :image # get list if key arguments of method = Preview::Terminal.method(:build).parameters.select{ |i| i[0].eql?(:key)}.map{ |i| i[1]} # check that only supported options are given = value.keys.map(&:to_sym) - raise "Invalid parameter(s) for option image: #{unknown_options.join(', ')}, use #{allowed_options.join(', ')}" unless .empty? end when :get then return [option_symbol] else Aspera.error_unreachable_line end nil end |
#special_format(what) ⇒ Object
Highlight special values on terminal
133 134 135 136 |
# File 'lib/aspera/cli/formatter.rb', line 133 def special_format(what) result = "<#{what}>" return %w[null empty].any?{ |s| what.include?(s)} ? result.dim : result.reverse_color end |
#status_image(blob) ⇒ Object
Returns text suitable to display an image from url.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/aspera/cli/formatter.rb', line 326 def status_image(blob) # check if URL begin URI.parse(blob) url = blob unless Environment.instance.url_method.eql?(:text) Environment.instance.open_uri(url) return '' end blob = UriReader.read(url) rescue URI::InvalidURIError nil end # try base64 begin blob = Base64.strict_decode64(blob) rescue nil end return Preview::Terminal.build(blob, **[:image].symbolize_keys) end |