Class: CLIHelper::ShowTable

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_helper.rb

Overview

Show table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = nil, ext = nil, &block) ⇒ ShowTable

Class constructor

Parameters:

  • conf (String) (defaults to: nil)

    Configuration file

  • ext (Helper) (defaults to: nil)

    Cli helper information



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_columnsObject (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

Parameters:

  • name (String)

    Column name

  • desc (String)

    Column description

  • conf (Array)

    Configutation 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

Parameters:

  • columns (Array)

    Array with columns information

Returns:

  • (Array)

    Array with columns objects



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

Parameters:

  • args (Array)

    Array with 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_columnsObject

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

#helperObject

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

Parameters:

  • index (Integer)

    Column index to search

Returns:

  • (Integer)

    Maximum length



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



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

Parameters:

  • data (Hash/Object)

    Data to show

  • options (Hash) (defaults to: {})

    Object with CLI user options

  • top (Boolean) (defaults to: false)

    True to not update columns again



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, options = {}, top = false)
    update_columns(options) unless top

    if options[:list]
        @cli_columns = options[: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, options) 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, options)
    else
        data ||= []

        print_table(data, options)
    end
end

#top(options = {}) ⇒ Object

Show resource continuosly

Parameters:

  • options (Hash) (defaults to: {})

    Object with CLI user options



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(options = {})
    delay = options[:delay] || 1
    top   = false

    begin
        loop do
            data = yield

            CLIHelper.scr_cls
            CLIHelper.scr_move(0, 0)

            show(data, options, top)

            sleep delay

            top = true
        end
    rescue SystemExit, Interrupt, StandardError => e
        CLIHelper.fail(e.message)
    end
end

#total_columns_size(columns) ⇒ Integer

Get total size of all columns

Parameters:

  • columns (Array)

    Array with columns name

Returns:

  • (Integer)

    Total size



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