Class: Twterm::Image
- Inherits:
-
Object
show all
- Defined in:
- lib/twterm/image.rb,
lib/twterm/image/bold.rb,
lib/twterm/image/color.rb,
lib/twterm/image/empty.rb,
lib/twterm/image/parens.rb,
lib/twterm/image/between.rb,
lib/twterm/image/brackets.rb,
lib/twterm/image/blank_line.rb,
lib/twterm/image/underlined.rb,
lib/twterm/image/string_image.rb,
lib/twterm/image/vertical_sequential_image.rb,
lib/twterm/image/horizontal_sequential_image.rb
Defined Under Namespace
Classes: Attr, Between, BlankLine, Bold, Brackets, Color, Dim, Empty, HorizontalSequentialImage, Parens, StringImage, Underlined, VerticalSequentialImage
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(column: 0, line: 0) ⇒ Image
Returns a new instance of Image.
15
16
17
|
# File 'lib/twterm/image.rb', line 15
def initialize(column: 0, line: 0)
@column, @line = column, line
end
|
Class Method Details
.blank_line ⇒ Object
41
42
43
|
# File 'lib/twterm/image.rb', line 41
def self.blank_line
BlankLine.new
end
|
.checkbox(checked) ⇒ Object
57
58
59
|
# File 'lib/twterm/image.rb', line 57
def self.checkbox(checked)
string(checked ? '*' : ' ').brackets
end
|
.cursor(height, current) ⇒ Object
69
70
71
72
|
# File 'lib/twterm/image.rb', line 69
def self.cursor(height, current)
color = current ? [:black, :magenta] : [:black]
VerticalSequentialImage.new([whitespace] * height).color(*color)
end
|
.empty ⇒ Object
74
75
76
|
# File 'lib/twterm/image.rb', line 74
def self.empty
Empty.new
end
|
.number(n) ⇒ Object
98
99
100
|
# File 'lib/twterm/image.rb', line 98
def self.number(n)
string(n.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\1,'))
end
|
.plural(n, singular, plural = "#{singular}s") ⇒ Object
106
107
108
|
# File 'lib/twterm/image.rb', line 106
def self.plural(n, singular, plural = "#{singular}s")
string(n == 1 ? singular : plural)
end
|
.remaining_resource(remaining, total, length) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/twterm/image.rb', line 78
def self.remaining_resource(remaining, total, length)
ratio = remaining * 100 / total
color =
if ratio >= 40
:green
elsif ratio >= 20
:yellow
else
:red
end
bars = string(('|' * (remaining * length / total)).ljust(length)).color(color)
Between.new(bars, !string('['), !string(']'))
end
|
.string(str) ⇒ Object
110
111
112
|
# File 'lib/twterm/image.rb', line 110
def self.string(str)
StringImage.new(str)
end
|
.toggle_switch(items, selected) ⇒ Image
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/twterm/image.rb', line 118
def self.toggle_switch(items, selected)
items
.map do |item|
on = item == selected
string(item.to_s)
.bold(on)
.underlined(on)
end
.intersperse(string(' | '))
.reduce(empty) { |acc, x| acc - x }
.brackets
end
|
.whitespace ⇒ Object
135
136
137
|
# File 'lib/twterm/image.rb', line 135
def self.whitespace
string(' ')
end
|
Instance Method Details
#! ⇒ Object
23
24
25
|
# File 'lib/twterm/image.rb', line 23
def !
bold
end
|
#_ ⇒ Object
19
20
21
|
# File 'lib/twterm/image.rb', line 19
def _
underlined
end
|
#at(line, column) ⇒ Object
35
36
37
38
39
|
# File 'lib/twterm/image.rb', line 35
def at(line, column)
@line, @column = line, column
self
end
|
#bold(on = true) ⇒ Object
45
46
47
|
# File 'lib/twterm/image.rb', line 45
def bold(on = true)
on ? Bold.new(self) : self
end
|
#brackets ⇒ Object
49
50
51
|
# File 'lib/twterm/image.rb', line 49
def brackets
Brackets.new(self)
end
|
#color(fg, bg = :transparent) ⇒ Object
61
62
63
|
# File 'lib/twterm/image.rb', line 61
def color(fg, bg = :transparent)
Color.new(self, fg, bg)
end
|
#column ⇒ Object
65
66
67
|
# File 'lib/twterm/image.rb', line 65
def column
@column || 0
end
|
#dim(on = true) ⇒ Object
53
54
55
|
# File 'lib/twterm/image.rb', line 53
def dim(on = true)
on ? Dim.new(self) : self
end
|
#line ⇒ Object
94
95
96
|
# File 'lib/twterm/image.rb', line 94
def line
@line || 0
end
|
#parens ⇒ Object
102
103
104
|
# File 'lib/twterm/image.rb', line 102
def parens
Parens.new(self)
end
|
#underlined(on = true) ⇒ Object
131
132
133
|
# File 'lib/twterm/image.rb', line 131
def underlined(on = true)
on ? Underlined.new(self) : self
end
|