Class: Pocolog::CLI::TTYReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/pocolog/cli/tty_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, colors: true, progress: true, **options) ⇒ TTYReporter

Returns a new instance of TTYReporter



21
22
23
24
25
26
27
28
29
30
# File 'lib/pocolog/cli/tty_reporter.rb', line 21

def initialize(format, colors: true, progress: true, **options)
    @base = 0
    @progress_enabled = progress
    reset_progressbar(format, **options)
    pastel = Pastel.new(enabled: colors)
    @c_warn = pastel.yellow.detach
    @c_info = pastel.yellow.detach
    @c_error = pastel.bright_red.detach
    @c_title = pastel.bold.detach
end

Instance Attribute Details

#baseObject

Base value for #current

It works as an offset between the reporter's current value and the underlying progress handler



19
20
21
# File 'lib/pocolog/cli/tty_reporter.rb', line 19

def base
  @base
end

#c_errorObject (readonly)

Returns the value of attribute c_error



11
12
13
# File 'lib/pocolog/cli/tty_reporter.rb', line 11

def c_error
  @c_error
end

#c_infoObject (readonly)

Returns the value of attribute c_info



13
14
15
# File 'lib/pocolog/cli/tty_reporter.rb', line 13

def c_info
  @c_info
end

#c_titleObject (readonly)

Returns the value of attribute c_title



12
13
14
# File 'lib/pocolog/cli/tty_reporter.rb', line 12

def c_title
  @c_title
end

#c_warnObject (readonly)

Returns the value of attribute c_warn



10
11
12
# File 'lib/pocolog/cli/tty_reporter.rb', line 10

def c_warn
  @c_warn
end

#progress_barObject (readonly)

Returns the value of attribute progress_bar



9
10
11
# File 'lib/pocolog/cli/tty_reporter.rb', line 9

def progress_bar
  @progress_bar
end

Instance Method Details

#advance(step = 1) ⇒ Object



60
61
62
63
64
# File 'lib/pocolog/cli/tty_reporter.rb', line 60

def advance(step = 1)
    return unless @progress_enabled

    progress_bar.advance(step)
end

#currentObject



48
49
50
51
52
# File 'lib/pocolog/cli/tty_reporter.rb', line 48

def current
    return unless @progress_enabled

    progress_bar.current - base
end

#current=(value) ⇒ Object



54
55
56
57
58
# File 'lib/pocolog/cli/tty_reporter.rb', line 54

def current=(value)
    return unless @progress_enabled

    progress_bar.current = value + base
end

#error(msg) ⇒ Object



78
79
80
# File 'lib/pocolog/cli/tty_reporter.rb', line 78

def error(msg)
    log(c_error.call(msg))
end

#finishObject



82
83
84
85
86
# File 'lib/pocolog/cli/tty_reporter.rb', line 82

def finish
    return unless @progress_enabled

    progress_bar.finish
end

#info(msg) ⇒ Object



70
71
72
# File 'lib/pocolog/cli/tty_reporter.rb', line 70

def info(msg)
    log(c_info.call(msg))
end

#log(msg) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/pocolog/cli/tty_reporter.rb', line 40

def log(msg)
    if progress_bar&.send(:tty?)
        progress_bar.log(msg)
    else
        $stdout.puts(msg)
    end
end

#reset_progressbar(format, **options) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/pocolog/cli/tty_reporter.rb', line 32

def reset_progressbar(format, **options)
    return unless @progress_enabled

    progress_bar.reset if @progress_bar
    @progress_bar = TTY::ProgressBar.new(format, **options)
    progress_bar.resize(60)
end

#title(msg) ⇒ Object



66
67
68
# File 'lib/pocolog/cli/tty_reporter.rb', line 66

def title(msg)
    log(c_title.call(msg))
end

#warn(msg) ⇒ Object



74
75
76
# File 'lib/pocolog/cli/tty_reporter.rb', line 74

def warn(msg)
    log(c_warn.call(msg))
end