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
473 474 475 476 477 478 479 480 481 |
# File 'lib/cli_helper.rb', line 473 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.
467 468 469 |
# File 'lib/cli_helper.rb', line 467 def default_columns @default_columns end |
Instance Method Details
#column(name, desc, *conf, &block) ⇒ Object
Fill column attributes
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/cli_helper.rb', line 493 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
636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/cli_helper.rb', line 636 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
520 521 522 523 524 |
# File 'lib/cli_helper.rb', line 520 def default(*args) args.map! {|a| a.to_sym } @default_columns = args end |
#describe_columns ⇒ Object
Show column description
593 594 595 596 597 598 599 |
# File 'lib/cli_helper.rb', line 593 def describe_columns str = '%-20s: %-20s' @columns.each do |column, d| puts format(str, column, d[:desc]) end end |
#helper ⇒ Object
Get the helper
484 485 486 |
# File 'lib/cli_helper.rb', line 484 def helper @ext end |
#max_size(index) ⇒ Integer
Get maximum string lenght in column
606 607 608 609 610 611 612 613 614 |
# File 'lib/cli_helper.rb', line 606 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
649 650 651 652 653 |
# File 'lib/cli_helper.rb', line 649 def print_tty_header CLIHelper.print_tty_header(header_str) puts end |
#show(data, options = {}, top = false) ⇒ Object
Show resource
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/cli_helper.rb', line 531 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
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/cli_helper.rb', line 570 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
621 622 623 624 625 626 627 628 629 |
# File 'lib/cli_helper.rb', line 621 def total_columns_size(columns) size = 0 columns.each do |c| size += @columns[c[:name]][:size] end size end |