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



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

Parameters:

  • name (String)

    Column name

  • desc (String)

    Column description

  • conf (Array)

    Configutation 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

Parameters:

  • columns (Array)

    Array with columns information

Returns:

  • (Array)

    Array with columns objects



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

Parameters:

  • args (Array)

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

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

#helperObject

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

Parameters:

  • index (Integer)

    Column index to search

Returns:

  • (Integer)

    Maximum length



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



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

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



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, 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



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



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