Class: Infobar::Display

Inherits:
Object show all
Extended by:
Tins::Delegate
Includes:
Term::ANSIColor
Defined in:
lib/infobar/display.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDisplay

Returns a new instance of Display.



22
23
24
25
26
27
28
# File 'lib/infobar/display.rb', line 22

def initialize
  self.output    = $stdout
  self.input     = $stdin
  self.frequency = 0.05
  @show          = true
  self.style = self.class.default_style
end

Instance Attribute Details

#frequencyObject

Returns the value of attribute frequency.



34
35
36
# File 'lib/infobar/display.rb', line 34

def frequency
  @frequency
end

#inputObject



57
58
59
60
61
62
63
# File 'lib/infobar/display.rb', line 57

def input
  if show?
    @input
  else
    NULL
  end
end

#showObject

Returns the value of attribute show.



36
37
38
# File 'lib/infobar/display.rb', line 36

def show
  @show
end

Class Method Details

.default_styleObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/infobar/display.rb', line 11

def self.default_style
  {
    done_fill:     ?░,
    done_fg_color: 22,
    done_bg_color: 40,
    todo_fill:     ' ',
    todo_fg_color: 40,
    todo_bg_color: 22,
  }
end

Instance Method Details

#carriage_returnObject



112
113
114
115
# File 'lib/infobar/display.rb', line 112

def carriage_return
  output << ?\r
  self
end

#clearObject



104
105
106
107
108
109
110
# File 'lib/infobar/display.rb', line 104

def clear
  carriage_return
  output << ' ' * columns
  carriage_return
  @frequency.reset
  self
end

#newlineObject



117
118
119
120
# File 'lib/infobar/display.rb', line 117

def newline
  output << $/
  self
end

#outputObject



47
48
49
50
51
52
53
# File 'lib/infobar/display.rb', line 47

def output
  if show?
    @output
  else
    NULL
  end
end

#output=(io) ⇒ Object



42
43
44
45
# File 'lib/infobar/display.rb', line 42

def output=(io)
  @output = io
  @output.ask_and_send(:respond_to?, :sync=, true)
end

#resetObject



98
99
100
101
102
# File 'lib/infobar/display.rb', line 98

def reset
  clear
  self.style = self.class.default_style
  self
end

#show?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/infobar/display.rb', line 38

def show?
  @show
end

#styleObject



65
66
67
68
69
# File 'lib/infobar/display.rb', line 65

def style
  self.class.default_style.each_key.each_with_object({}) do |attribute, h|
    h[attribute] = instance_variable_get "@#{attribute}"
  end
end

#style=(new_style) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/infobar/display.rb', line 71

def style=(new_style)
  self.class.default_style.each_key do |attribute|
    value = new_style[attribute]
    value.nil? and next
    instance_variable_set "@#{attribute}", value
  end
  self
end

#update(message:, progressed:, force: false, **options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/infobar/display.rb', line 80

def update(message:, progressed:, force: false, **options)
  force and @frequency.reset
  @frequency.call do
    message = Infobar.convert_to_message(message)
    carriage_return
    self.style = options
    cols = columns
    todo = message.to_str.center cols, replace_character
    done = todo.slice!(0, (progressed * cols))
    done.gsub!(replace_character, @done_fill[0])
    todo.gsub!(replace_character, @todo_fill[0])
    output << color(@done_fg_color, on_color(@done_bg_color, done)) +
      color(@todo_fg_color, on_color(@todo_bg_color, todo))
  end
end