termline 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/termline.rb CHANGED
@@ -30,68 +30,167 @@ Building a better future, one line of code at a time.
30
30
  // ·
31
31
  =end
32
32
 
33
+
34
+ # ·
35
+ require_relative "termline/version"
36
+ require_relative "termline/terminal"
33
37
  require_relative "termline/style"
34
38
  require_relative "termline/table"
35
39
  require_relative "termline/space"
36
40
  require_relative "termline/list"
37
41
  require_relative "termline/msg"
42
+ require_relative "termline/text"
43
+ require_relative "termline/details"
38
44
 
39
45
 
40
46
  # ·
41
47
  module Termline
42
48
  class << self
49
+ def terminal
50
+ @terminal ||= Termline::Terminal.new
51
+ end
52
+
53
+ def output
54
+ terminal.output
55
+ end
56
+
57
+ def output=(io)
58
+ terminal.output = io
59
+ end
60
+
61
+ def ansi
62
+ terminal.ansi
63
+ end
64
+
65
+ def ansi=(mode)
66
+ terminal.ansi = mode
67
+ end
68
+
69
+ def width
70
+ terminal.width
71
+ end
72
+
73
+ def width=(value)
74
+ terminal.width = value
75
+ end
76
+
77
+ def timestamps
78
+ @timestamps.nil? ? true : @timestamps
79
+ end
80
+
81
+ alias timestamps? timestamps
82
+
83
+ def timestamps=(value)
84
+ unless value == true || value == false
85
+ raise ArgumentError, "Timestamps must be true or false"
86
+ end
87
+
88
+ @timestamps = value
89
+ end
90
+
91
+ def timestamp_format
92
+ @timestamp_format || "%H:%M:%S:%L"
93
+ end
94
+
95
+ def timestamp_format=(format)
96
+ unless format.is_a?(String) && !format.empty?
97
+ raise ArgumentError, "Timestamp format must be a non-empty string"
98
+ end
99
+
100
+ @timestamp_format = format
101
+ end
102
+
103
+ def configure
104
+ yield self
105
+ self
106
+ end
107
+
43
108
  def m(*messages)
44
- messages.each { |message| puts(message) }
109
+ write(messages.map(&:to_s).join("\n"))
45
110
  end
46
111
 
47
- def msg(*messages, **data)
48
- messages.each { |message| puts(Termline::Msg.builder(message, data:data)) }
112
+ def msg(*messages, time:nil, bgcolor: :default, **data)
113
+ write(messages.map { |message| Termline::Msg.builder(message, bgcolor:bgcolor, data:data, time:time) }.join("\n"))
49
114
  end
50
115
 
51
- def info(*messages, tag:'INFO:', icon: :info, **data)
52
- messages.each { |message| puts(Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :blue)) }
116
+ def info(*messages, tag:'INFO:', icon: :info, time:nil, bgcolor: :default, **data)
117
+ write(messages.map { |message| Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :blue, bgcolor:bgcolor, time:time) }.join("\n"))
53
118
  end
54
119
 
55
- def success(*messages, tag:'SUCCESS:', icon: :success, **data)
56
- messages.each { |message| puts(Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :green)) }
120
+ def success(*messages, tag:'SUCCESS:', icon: :success, time:nil, bgcolor: :default, **data)
121
+ write(messages.map { |message| Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :green, bgcolor:bgcolor, time:time) }.join("\n"))
57
122
  end
58
123
 
59
- def warning(*messages, tag:'WARNING:', icon: :warning, **data)
60
- messages.each { |message| puts(Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :yellow)) }
124
+ def warning(*messages, tag:'WARNING:', icon: :warning, time:nil, bgcolor: :default, **data)
125
+ write(messages.map { |message| Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :yellow, bgcolor:bgcolor, time:time) }.join("\n"))
61
126
  end
62
127
 
63
- def danger(*messages, tag:'DANGER:', icon: :error, **data)
64
- messages.each { |message| puts(Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :red)) }
128
+ def danger(*messages, tag:'DANGER:', icon: :error, time:nil, bgcolor: :default, **data)
129
+ write(messages.map { |message| Termline::Msg.builder(message, tag:tag, icon:icon, data:data, color: :red, bgcolor:bgcolor, time:time) }.join("\n"))
65
130
  end
66
131
 
67
132
  def alert(*messages)
68
- messages.each { |message| puts(Termline::Msg.alert(message)) }
133
+ write(messages.map { |message| Termline::Msg.alert(message) }.join("\n"))
69
134
  end
70
135
 
71
136
 
72
137
  def list(*items, color: :default, icon: :debug)
73
- Termline::List.builder(items, color:color, icon:icon)
138
+ write(Termline::List.builder(items, color:color, icon:icon))
74
139
  end
75
140
 
141
+ def ordered_list(*items, color: :default, start: 1)
142
+ write(Termline::List.builder(items, color:color, ordered:true, start:start))
143
+ end
76
144
 
77
- def table(data, header:true)
78
- Termline::Table.builder(data, header:header)
145
+ def table(data, header:true, align: :auto)
146
+ write(Termline::Table.builder(data, header:header, align:align))
79
147
  end
80
148
 
81
149
  def br(count=1)
82
- puts(Termline::Space.builder("\n", count))
150
+ write(Termline::Space.builder("\n", count))
83
151
  end
84
152
 
85
- def line(count=8)
86
- puts(Termline::Space.builder('-·- ', count))
153
+ def line(count=nil, character: "─", width: nil)
154
+ content = if count
155
+ Termline::Space.builder('-·- ', count)
156
+ else
157
+ Termline::Space.line(character, width || terminal.width)
158
+ end
159
+
160
+ write(content)
87
161
  end
88
162
 
89
163
  def color(color)
90
- Termline::Space.color(count)
164
+ write(Termline::Space.color(color))
91
165
  end
92
166
 
93
167
 
94
- def deprecation message
168
+ def title(text, color: :blue, character: "─", width: nil)
169
+ write(Termline::Text.title(text, color:color, character:character, width:width || terminal.width))
170
+ end
171
+
172
+ def details(data=nil, color: :blue, separator: ": ", icon: nil, icons: nil, **pairs)
173
+ write(Termline::Details.builder(
174
+ data,
175
+ color:color,
176
+ separator:separator,
177
+ icon:icon,
178
+ icons:icons,
179
+ **pairs
180
+ ))
181
+ end
182
+
183
+ def deprecation(*messages, time:nil, **data)
184
+ warning(*messages, tag: 'DEPRECATED:', time:time, **data)
185
+ end
186
+
187
+ private
188
+
189
+ def write(content)
190
+ return content if content.nil? || content.empty?
191
+
192
+ output.puts(content)
193
+ content
95
194
  end
96
195
  end
97
196
  end