Class: CLIHelper::ShowTable
- Inherits:
-
Object
- Object
- CLIHelper::ShowTable
- Defined in:
- lib/cli_helper.rb
Overview
Show table
Instance Attribute Summary collapse
-
#default_columns ⇒ Object
readonly
Returns the value of attribute default_columns.
Instance Method Summary collapse
-
#column(name, desc, *conf, &block) ⇒ Object
Fill column attributes.
-
#columns_info(columns) ⇒ Array
Get columns information.
-
#default(*args) ⇒ Object
Get default columns.
-
#describe_columns ⇒ Object
Show column description.
-
#helper ⇒ Object
Get the helper.
-
#initialize(conf = nil, ext = nil, &block) ⇒ ShowTable
constructor
Class constructor.
-
#max_size(index) ⇒ Integer
Get maximum string lenght in column.
-
#print_tty_header ⇒ Object
Print tty header.
-
#show(data, options = {}, top = false) ⇒ Object
Show resource.
-
#top(options = {}) ⇒ Object
Show resource continuosly.
-
#total_columns_size(columns) ⇒ Integer
Get total size of all columns.
Constructor Details
#initialize(conf = nil, ext = nil, &block) ⇒ ShowTable
Class constructor
321 322 323 324 325 326 327 328 329 |
# File 'lib/cli_helper.rb', line 321 def initialize(conf = nil, ext = nil, &block) @columns = {} @default_columns = [] @ext = ext @conf = conf instance_eval(&block) end |
Instance Attribute Details
#default_columns ⇒ Object (readonly)
Returns the value of attribute default_columns.
315 316 317 |
# File 'lib/cli_helper.rb', line 315 def default_columns @default_columns end |
Instance Method Details
#column(name, desc, *conf, &block) ⇒ Object
Fill column attributes
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/cli_helper.rb', line 341 def column(name, desc, *conf, &block) column = {} column[:desc] = desc column[:size] = 5 conf.each do |c| case c when Symbol column[c] = true when Hash c.each do |key, value| column[key] = value end end end column[:proc] = block @columns[name.to_sym] = column @default_columns << name end |
#columns_info(columns) ⇒ Array
Get columns information
484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/cli_helper.rb', line 484 def columns_info(columns) ret = [] columns.each do |column| data = column.to_s.split('=') ret << { :name => data[0].upcase.to_sym, :prop => data[1] } end ret end |
#default(*args) ⇒ Object
Get default columns
368 369 370 371 372 |
# File 'lib/cli_helper.rb', line 368 def default(*args) args.map! {|a| a.to_sym } @default_columns = args end |
#describe_columns ⇒ Object
Show column description
441 442 443 444 445 446 447 |
# File 'lib/cli_helper.rb', line 441 def describe_columns str = '%-20s: %-20s' @columns.each do |column, d| puts format(str, column, d[:desc]) end end |
#helper ⇒ Object
Get the helper
332 333 334 |
# File 'lib/cli_helper.rb', line 332 def helper @ext end |
#max_size(index) ⇒ Integer
Get maximum string lenght in column
454 455 456 457 458 459 460 461 462 |
# File 'lib/cli_helper.rb', line 454 def max_size(index) sizes = [] @res_data.each do |d| sizes << d[index].size end sizes.max end |
#print_tty_header ⇒ Object
Print tty header
497 498 499 500 501 |
# File 'lib/cli_helper.rb', line 497 def print_tty_header CLIHelper.print_tty_header(header_str) puts end |
#show(data, options = {}, top = false) ⇒ Object
Show resource
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/cli_helper.rb', line 379 def show(data, = {}, top = false) update_columns() unless top if [:list] @cli_columns = [:list].collect {|o| o.upcase.to_sym } else @cli_columns = @default_columns end if data.is_a? Hash @data = data @data.extend(HashWithSearch) pool = @data.keys.first return print_table(data, ) unless pool element = pool.split('_')[0..-2].join('_') pool_data = @data.dsearch("#{pool}/#{element}") if pool_data pool_data = [pool_data].flatten else pool_data = [] end print_table(pool_data, ) else data ||= [] print_table(data, ) end end |
#top(options = {}) ⇒ Object
Show resource continuosly
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/cli_helper.rb', line 418 def top( = {}) delay = [:delay] || 1 top = false begin loop do data = yield CLIHelper.scr_cls CLIHelper.scr_move(0, 0) show(data, , top) sleep delay top = true end rescue SystemExit, Interrupt, StandardError => e CLIHelper.fail(e.) end end |
#total_columns_size(columns) ⇒ Integer
Get total size of all columns
469 470 471 472 473 474 475 476 477 |
# File 'lib/cli_helper.rb', line 469 def total_columns_size(columns) size = 0 columns.each do |c| size += @columns[c[:name]][:size] end size end |