termclock 0.1.3 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 746cefa7bb14ad6ea4c2886b3631047a1d28eaf65a23709f69dd53a1af139929
4
- data.tar.gz: 5f71dfabd6b0608b41f7707d89d09521b2d1121d8206f5279d67be242fd49850
3
+ metadata.gz: d4c5e863a8c650bfda4aaa58bc010d8526692f47853f2ddae298cec606f4b410
4
+ data.tar.gz: 3f3fef454b216911b4a7aaf860f5376e6c2b00b9aef1b72331afa9defba36b59
5
5
  SHA512:
6
- metadata.gz: ac18271d58739a9061f6c582d031f2bcd8e329259069681e8abb253a14af60307fb3253ff8468509afdc01057e2f802cbe1c32ba7d08a91c5df2ab944b65adc3
7
- data.tar.gz: 83e4ebc5a42e09cc880bd11468730266d8807b8aee72a873b04e8acf1867bcc9ae6500e13909ed421a37b6888c6bcaea9e8764ab5b0d6c7f6f0c0e02374af75c
6
+ metadata.gz: b23b1136a704adc42a90d9cdae7f8de046bc63a19688cfb7bfb7b1b230c2b6f4a59a0088e8817707a6b9ba02d95f72b7764519965c429068a3e9e9981710406b
7
+ data.tar.gz: 79c42cb8d83e1077073fcc94c29e19faec8ce67923095d32f6c0adcd8e8687a54c931911621b9ffb4d6a56f7eb7c28037af2ca879cee030ce5ed350c71ce207c
@@ -1,7 +1,58 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'termclock'
3
+ $-v = nil
3
4
 
4
- $-v = true
5
+ if ARGV.any? { |x| x[/\A\-(\-help|h)\z/] }
6
+ puts <<~EOF
7
+ TermClock: A clock that runs on the LinuxTerminal!
8
+
9
+ \e[1;4mArguments:\e[0m
10
+
11
+ 1. Help and Version:\e[0m
12
+ --help|-h\t\t\tShows this help section
13
+ --version|-v\t\t\tShows termclock version
14
+
15
+ 2. Style:\e[0m
16
+ --bold|-b\t\t\tMake texts bold
17
+ --italic|-i\t\t\tMake texts italic
18
+ --character=|char=\t\tDraws specified character
19
+ --clean\t\t\t\tJust run the clean bare clock
20
+ --colour=|-c=\t\t\tSpecify hex colour (4 colours)
21
+ \t\t\t\t[ with or without # ]
22
+
23
+ 3. Information:\e[0m
24
+ --no-date|-nd\t\t\tShows no date
25
+ --no-message|-nm\t\tShows no messages
26
+ --no-sysinfo|-ni\t\tShows no system info
27
+ --refresh|r\t\t\tSpecify delay or refresh time
28
+ --text-colour|-tc\t\tSpecify text colour (2 colours)
29
+
30
+ 4. Formats:\e[0m
31
+ --date-format=|-df=\t\tSpecify the date format
32
+ \t\t\t\t[ Run date --help for formats ]
33
+ --time-format=|-tf=\t\tSpecify the time format
34
+ \t\t\t\t[ Run date --help for formats ]
35
+
36
+ Supported characters are 0 - 9, a - z, /, \\, !, %, and |.
37
+
38
+ This is Termclock v#{Termclock::VERSION}
39
+ EOF
40
+
41
+ exit 0
42
+ end
43
+
44
+ if ARGV.any? { |x| x[/\A\-(-version|v)\z/] }
45
+ colour = [
46
+ [[255, 100, 100], [100, 100, 255]],
47
+ [[60, 230, 180], [100, 100, 255]],
48
+ [[255, 255, 0], [100, 100, 255]],
49
+ [[255, 80, 170], [255, 200, 0]],
50
+ ].sample
51
+
52
+ colour.reverse! if rand < 0.5
53
+ puts "This is Termclock v#{Termclock::VERSION}".gradient(*colour)
54
+ exit 0
55
+ end
5
56
 
6
57
  begin
7
58
  print "\e[?25l"
@@ -24,16 +75,45 @@ begin
24
75
  }
25
76
 
26
77
  chars = ARGV.find { |x| x[/\A\-(\-character|char)=.*\z/] } &.split(?=) &.at(1)
27
- Termclock::ParseCharacters.transform_characters!(chars[0]) if chars
78
+ Termclock::ParseCharacters.transform_characters!(*chars) if chars
28
79
 
29
80
  _refresh_time = ARGV.find { |x| x[/\A\-(\-refresh|r)=.*\z/] } &.split(?=) &.at(1)
30
81
  refresh_time = _refresh_time ? _refresh_time.to_f : 0.1
31
82
 
32
- text_colours = ARGV.find { |x| x[/\A\-(\-text\-colour|tc)=.*\z/] } &.split(?=) &.at(1) &.split(?,)
83
+ _time_format = ARGV.find { |x| x[/\A\-(\-time\-format|tf)=.*\z/] } &.split(?=) &.at(1)
84
+ time_format = _time_format ? _time_format : "%H %M %S %2N"
33
85
 
86
+ _date_format = ARGV.find { |x| x[/\A\-(\-date\-format|df)=.*\z/] } &.split(?=) &.at(1)
87
+ date_format = _date_format ? _date_format : "%a, %d %B %Y"
88
+
89
+ text_colours = ARGV.find { |x| x[/\A\-(\-text\-colour|tc)=.*\z/] } &.split(?=) &.at(1) &.split(?,)
34
90
  abort("Text colours need 2 colours. Example: -tc=55f,3ce3b5") if text_colours && text_colours.length != 2
35
91
 
36
- Termclock.start(*colours, *text_colours, sleep: refresh_time)
92
+ bold = ARGV.any? { |x| x[/\A\-(\-bold|b)\z/] }
93
+ italic = ARGV.any? { |x| x[/\A\-(\-italic|i)\z/] }
94
+
95
+ no_print_info = ARGV.any? { |x| x[/\A\-(\-no\-sysinfo|ni)\z/] }
96
+ no_print_message = ARGV.any? { |x| x[/\A\-(\-no\-message|nm)\z/] }
97
+ no_print_date = ARGV.any? { |x| x[/\A\-(\-no\-date|nd)\z/] }
98
+
99
+ clean = ARGV.any? { |x| x[/\A\-(\-clean)\z/] }
100
+
101
+ if clean
102
+ no_print_info = no_print_message = no_print_date = true
103
+ end
104
+
105
+ Termclock.start(
106
+ *colours,
107
+ *text_colours,
108
+ sleep: refresh_time,
109
+ bold: bold,
110
+ italic: italic,
111
+ print_info: !no_print_info,
112
+ print_message: !no_print_message,
113
+ print_date: !no_print_date,
114
+ time_format: time_format,
115
+ date_format: date_format
116
+ )
37
117
  rescue Interrupt, SignalException
38
118
  print "\e[H\e[2J\e[3J"
39
119
  ensure
@@ -1,7 +1,17 @@
1
1
  require 'linux_stat'
2
2
  require 'io/console'
3
+
4
+ module Termclock
5
+ COLOURTERM = ENV.key?('COLORTERM')
6
+ CLEAR = COLOURTERM ? "\e[H\e[2J\e[3J" : "\e[H"
7
+ NEWLINE = ?\n.freeze
8
+ SPACE = ?\s.freeze
9
+ TAB = ?\t.freeze
10
+ end
11
+
3
12
  require_relative "termclock/string"
4
13
  require_relative "termclock/parse_characters"
5
- require_relative "termclock/main"
14
+ require_relative "termclock/system_info"
15
+ require_relative "termclock/start"
6
16
  require_relative "termclock/hex2rgb"
7
17
  require_relative "termclock/version"
@@ -1,76 +1,75 @@
1
1
  module Termclock
2
2
  module ParseCharacters
3
- NEWLINE = ?\n.freeze
4
3
  CHARACTERS = <<~EOF.freeze
5
4
  # 0
6
- ```````
7
- `` ``
8
- `` ``
9
- `` ``
10
- ```````
5
+ ````````
6
+ `` ``
7
+ `` ``
8
+ `` ``
9
+ ````````
11
10
 
12
11
  # 1
13
12
  ``
14
- ````
13
+ `````
15
14
  ``
16
15
  ``
17
- ```````
16
+ ````````
18
17
 
19
18
  # 2
20
- ```````
21
- ``
22
- ```````
19
+ ````````
20
+ ``
21
+ ````````
23
22
  ``
24
- ```````
23
+ ````````
25
24
 
26
25
  # 3
27
- ```````
28
- ``
29
- ```````
30
- ``
31
- ```````
26
+ ````````
27
+ ``
28
+ ````````
29
+ ``
30
+ ````````
32
31
 
33
32
  # 4
34
- `` ``
35
- `` ``
36
- ```````
37
- ``
38
- ``
33
+ `` ``
34
+ `` ``
35
+ ````````
36
+ ``
37
+ ``
39
38
 
40
39
  # 5
41
- ```````
40
+ ````````
42
41
  ``
43
- ```````
44
- ``
45
- ```````
42
+ ````````
43
+ ``
44
+ ````````
46
45
 
47
46
  # 6
48
- ```````
47
+ ````````
49
48
  ``
50
- ```````
51
- `` ``
52
- ```````
49
+ ````````
50
+ `` ``
51
+ ````````
53
52
 
54
53
  # 7
55
- ```````
56
- ``
57
- ``
58
- ``
59
- ``
54
+ ````````
55
+ ``
56
+ ``
57
+ ``
58
+ ``
60
59
 
61
60
  # 8
62
- ```````
63
- `` ``
64
- ```````
65
- `` ``
66
- ```````
61
+ ````````
62
+ `` ``
63
+ ````````
64
+ `` ``
65
+ ````````
67
66
 
68
67
  # 9
69
- ```````
70
- `` ``
71
- ```````
72
- ``
73
- ```````
68
+ ````````
69
+ `` ``
70
+ ````````
71
+ ``
72
+ ````````
74
73
 
75
74
  # :
76
75
  \u2B29\u2B29
@@ -86,88 +85,88 @@ module Termclock
86
85
  \s\s
87
86
  \s\s
88
87
  # A
89
- ```````
90
- `` ``
91
- ```````
92
- `` ``
93
- `` ``
88
+ ````````
89
+ `` ``
90
+ ````````
91
+ `` ``
92
+ `` ``
94
93
 
95
94
  # B
96
- ``````
97
- `` ``
98
- ``````
99
- `` ``
100
- ``````
95
+ ````````
96
+ `` ``
97
+ ````````
98
+ `` ``
99
+ ````````
101
100
 
102
101
  # C
103
- ```````
102
+ ````````
104
103
  ``
105
104
  ``
106
105
  ``
107
- ```````
106
+ ````````
108
107
 
109
108
  # D
110
- `````
111
- `` ``
112
- `` ``
113
- `` ``
114
- `````
109
+ ````````
110
+ `` ``
111
+ `` ``
112
+ `` ``
113
+ ````````
115
114
 
116
115
  # E
117
- ```````
116
+ ````````
118
117
  ``
119
- ```````
118
+ ````````
120
119
  ``
121
- ```````
120
+ ````````
122
121
 
123
122
  # F
124
- ```````
123
+ ````````
125
124
  ``
126
- ```````
125
+ ````````
127
126
  ``
128
127
  ``
129
128
 
130
129
  # G
131
- ```````
130
+ ````````
132
131
  ``
133
132
  ``
134
- `` ```
133
+ `` ````
135
134
  ````````
136
135
 
137
136
  # H
138
- `` ``
139
- `` ``
140
- ```````
141
- `` ``
142
- `` ``
137
+ `` ``
138
+ `` ``
139
+ ````````
140
+ `` ``
141
+ `` ``
143
142
 
144
143
  # I
145
- ```````
146
- `
147
- `
148
- `
149
- ```````
144
+ ````````
145
+ \s\s\s``
146
+ \s\s\s``
147
+ \s\s\s``
148
+ ````````
150
149
 
151
150
  # J
152
- ```````
153
- ``
154
- ``
155
- `` ``
156
- ``````
151
+ ````````
152
+ ``
153
+ ``
154
+ `` ``
155
+ ```````
157
156
 
158
157
  # K
159
- `` ``
160
- `` ``
161
- ````
162
- `` ``
163
- `` ``
158
+ `` ``
159
+ `` ``
160
+ `````
161
+ `` ``
162
+ `` ``
164
163
 
165
164
  # L
166
165
  ``
167
166
  ``
168
167
  ``
169
168
  ``
170
- ``````
169
+ ````````
171
170
 
172
171
  # M
173
172
  ``` ```
@@ -177,11 +176,11 @@ module Termclock
177
176
  `` ``
178
177
 
179
178
  # N
180
- ```` ``
181
- `` ` ``
182
- `` ` ``
183
- `` ` ``
184
- `` ```
179
+ ```` ``
180
+ `` `` ``
181
+ `` `` ``
182
+ `` `` ``
183
+ `` ````
185
184
 
186
185
  # O
187
186
  ````````
@@ -198,25 +197,25 @@ module Termclock
198
197
  ``
199
198
 
200
199
  # Q
201
- ```````
202
- `` ``
203
- ```````
204
- ``
205
- ``
200
+ ````````
201
+ `` ``
202
+ ````````
203
+ ``
204
+ ``
206
205
 
207
206
  # R
208
- ```````
207
+ ````````
208
+ `` ``
209
+ ````````
210
+ `````
209
211
  `` ``
210
- ```````
211
- ````
212
- `` ``
213
212
 
214
213
  # S
215
- ```````
214
+ ````````
216
215
  ``
217
- ```````
218
- ``
219
- ```````
216
+ ````````
217
+ ``
218
+ ````````
220
219
 
221
220
  # T
222
221
  ````````
@@ -226,32 +225,32 @@ module Termclock
226
225
  ``
227
226
 
228
227
  # U
229
- `` ``
230
- `` ``
231
- `` ``
232
- `` ``
233
- ```````
228
+ `` ``
229
+ `` ``
230
+ `` ``
231
+ `` ``
232
+ ````````
234
233
 
235
234
  # V
236
- `` ``
237
- `` ``
238
- `` ``
239
- `` ``
240
- `
235
+ `` ``
236
+ `` ``
237
+ `` ``
238
+ \s`` ``\s
239
+ \s\s\s``
241
240
 
242
241
  # W
243
- `` ``
244
- `` ``
245
- `` ``
246
- `` ` ``
247
- `````````
242
+ `` ``
243
+ `` ``
244
+ `` ``
245
+ `` ` ``
246
+ ````````
248
247
 
249
248
  # X
250
- `` ``
251
- `` ``
252
- ``
253
- `` ``
254
- `` ``
249
+ `` ``
250
+ \s`` ``
251
+ \s\s\s``
252
+ \s`` ``
253
+ `` ``
255
254
 
256
255
  # Y
257
256
  `` ``
@@ -266,6 +265,41 @@ module Termclock
266
265
  ``
267
266
  ``
268
267
  ````````
268
+
269
+ # /
270
+ \s\s\s\s\s``\s
271
+ \s\s\s\s``\s\s
272
+ \s\s\s``\s\s\s
273
+ \s\s``\s\s\s\s
274
+ \s``\s\s\s\s\s
275
+
276
+ # \\
277
+ \s``\s\s\s\s\s
278
+ \s\s``\s\s\s\s
279
+ \s\s\s``\s\s\s
280
+ \s\s\s\s``\s\s
281
+ \s\s\s\s\s``\s
282
+
283
+ # %
284
+ \s\s\s\s\s``\s
285
+ ``\s\s``\s\s
286
+ \s\s\s``\s\s\s
287
+ \s\s``\s\s``
288
+ \s``\s\s\s\s\s
289
+
290
+ # |
291
+ \s\s\s``\s\s\s
292
+ \s\s\s``\s\s\s
293
+ \s\s\s``\s\s\s
294
+ \s\s\s``\s\s\s
295
+ \s\s\s``\s\s\s
296
+
297
+ # !
298
+ \s\s\s``\s\s\s
299
+ \s\s\s``\s\s\s
300
+ \s\s\s``\s\s\s
301
+ \s\s\s\s\s\s\s\s
302
+ \s\s\s``\s\s\s
269
303
  EOF
270
304
 
271
305
  class << self
@@ -283,9 +317,23 @@ module Termclock
283
317
  end.freeze
284
318
 
285
319
  def transform_characters!(arg)
320
+ @@transformed ||= nil
321
+ fail RuntimeError, 'Characters already transformed!' if @@transformed
322
+ @@transformed ||= true
323
+
286
324
  @@characters.values.each { |x|
287
325
  stripped = x.strip[0]
288
- x.gsub!(stripped, arg) if stripped
326
+ chars = arg.chars.rotate(-1)
327
+
328
+ if stripped
329
+ replace_with = x.chars.map { |y|
330
+ chars = arg.chars.rotate(-1) if y == NEWLINE
331
+ next(y) if y != stripped
332
+ chars.rotate!(1)[0]
333
+ }.join
334
+
335
+ x.replace(replace_with)
336
+ end
289
337
  }
290
338
  end
291
339
 
@@ -0,0 +1,152 @@
1
+ module Termclock
2
+ def self.start(colour1, colour2, colour3, colour4,
3
+ textcolour1 = nil, textcolour2 = nil,
4
+ sleep: 0.1,
5
+ bold: false,
6
+ italic: false,
7
+ print_info: true, print_message: true,
8
+ print_date: true,
9
+ time_format: "%H %M %S %2N",
10
+ date_format: '%a, %d %B %Y'
11
+ )
12
+
13
+ generate = proc do |start, stop, n = 5|
14
+ r_op = r_val = nil
15
+ ary = []
16
+
17
+ if start > stop
18
+ r_op, r_val = :-, start.-(stop).fdiv(n - 1)
19
+ elsif start < stop
20
+ r_op, r_val = :+, stop.-(start).fdiv(n - 1)
21
+ end
22
+
23
+ _r = r_op ? start.send(r_op, r_val * -1) : start
24
+ n.times {
25
+ _r = _r.send(r_op, r_val) if r_op
26
+ ary << _r.clamp(0, 255).to_i
27
+ }
28
+
29
+ ary
30
+ end
31
+
32
+ gc_compact, gc_compacted = GC.respond_to?(:compact), Time.now.to_i + 7200
33
+ print "\e[H\e[2J\e[3J" unless COLOURTERM
34
+
35
+ r1, g1, b1 = *colour1
36
+ r2, g2, b2 = *colour2
37
+ r3, g3, b3 = *colour3
38
+ r4, g4, b4 = *colour4
39
+
40
+ # colour1 -> colour3
41
+ rs1 = generate.(r1, r3)
42
+ gs1 = generate.(g1, g3)
43
+ bs1 = generate.(b1, b3)
44
+
45
+ # colour2 -> colour4
46
+ rs2 = generate.(r2, r4)
47
+ gs2 = generate.(g2, g4)
48
+ bs2 = generate.(b2, b4)
49
+
50
+ # All Gradient Colours
51
+ colours = [rs1, gs1, bs1].transpose.zip([rs2, gs2, bs2].transpose)
52
+ colours.unshift(colours[0])
53
+ colours.push(colours[-1])
54
+
55
+ # Text colours
56
+ tc1 = textcolour1 ? hex2rgb(textcolour1) : hex2rgb('5555ff')
57
+ tc2 = textcolour2 ? hex2rgb(textcolour2) : hex2rgb('3ce3b5')
58
+
59
+ cpu_usage = 0
60
+ cpu_usage_t = Thread.new { }
61
+
62
+ current_net_usage = ''
63
+ current_net_usage_t = Thread.new { }
64
+
65
+ message_time = Time.now.to_i - 5
66
+ message_counter = -1
67
+ message = ''
68
+ message_final = ''
69
+ message_temp = ''
70
+
71
+ date, info = '', ''
72
+
73
+ get_message = proc {
74
+ case Time.now.hour
75
+ when 5...12
76
+ "\u{1F304} Good Morning..."
77
+ when 12...16
78
+ "\u26C5 Good Afternoon..."
79
+ when 16...18
80
+ "\u{1F307} Good Evening..."
81
+ when 18...20
82
+ "\u{1F31F} Good Evening..."
83
+ when 20...24
84
+ "\u{1F303} Good Night..."
85
+ else
86
+ "\u{2728} Good Night..."
87
+ end
88
+ }
89
+
90
+ while true
91
+ time_now = Time.now
92
+ height, width = *STDOUT.winsize
93
+
94
+ if time_now.to_f./(0.5).to_i.even?
95
+ splitter = ?:.freeze
96
+ else
97
+ splitter = ?$.freeze
98
+ end
99
+
100
+ if print_message
101
+ message_counter += 1
102
+ message_align = width - message_counter % width + message.length / 2 - 4
103
+ if (width - message_counter % width <= message.length)
104
+ message.replace(message[1..-1])
105
+ message_align = width - message_counter % width + 4
106
+ else
107
+ message.clear if width - message_counter % width == width
108
+ message_temp = get_message.call
109
+
110
+ if message_temp != message
111
+ message << message_temp[message.length..message.length + 1]
112
+ end
113
+ end
114
+
115
+ message_final = message.rjust(message_align).gradient(
116
+ tc1, tc2, exclude_spaces: true, bold: bold, italic: italic
117
+ )
118
+ end
119
+
120
+ if print_info
121
+ info = system_info(width).gradient(
122
+ tc1, tc2, bold: bold, italic: italic
123
+ )
124
+ end
125
+
126
+ if print_date
127
+ date = time_now.strftime(date_format).center(width)
128
+ .gradient(tc1, tc2, bold: bold, italic: italic)
129
+ end
130
+
131
+ time = time_now.strftime(time_format).split.join(splitter)
132
+ art = Termclock::ParseCharacters.display(time).lines
133
+
134
+ art_aligned = art.each_with_index do |x, i|
135
+ chomped = x.chomp(''.freeze).+(NEWLINE)
136
+ gr = chomped.gradient(*colours[i], bold: bold, italic: italic)
137
+ x.replace(SPACE.*(width./(2.0).-(chomped.length / 2.0).abs.to_i + 1) + gr)
138
+ end.join
139
+
140
+ vertical_gap = "\e[#{height./(2.0).-(art.length / 2.0).to_i + 1}H"
141
+
142
+ print "#{CLEAR}#{info}#{vertical_gap}#{art_aligned}\n#{date}\n\n#{message_final}"
143
+
144
+ if gc_compact && time_now.to_i > gc_compacted
145
+ GC.compact
146
+ gc_compacted = time_now.to_i + 7200
147
+ end
148
+
149
+ sleep(sleep)
150
+ end
151
+ end
152
+ end
@@ -1,5 +1,14 @@
1
1
  class String
2
- def gradient(*all_rgbs, bg: false, exclude_spaces: false)
2
+ NEWLINE = ?\n.freeze
3
+ SPACE = ?\s.freeze
4
+ TAB = ?\t.freeze
5
+
6
+ def gradient(*all_rgbs,
7
+ bg: false,
8
+ exclude_spaces: false,
9
+ bold: false, italic: false, blink: false
10
+ )
11
+
3
12
  temp = ''
4
13
 
5
14
  r, g, b = all_rgbs[0]
@@ -8,7 +17,18 @@ class String
8
17
 
9
18
  init = bg ? 48 : 38
10
19
 
20
+ style = nil
21
+ if bold || italic
22
+ style = "\e["
23
+ style << '1;'.freeze if bold
24
+ style << '3;'.freeze if italic
25
+ style.chop!
26
+ style << 'm'.freeze
27
+ end
28
+
11
29
  each_line do |c|
30
+ temp << style if style
31
+
12
32
  _r, _g, _b = r, g, b
13
33
  chomped = !!c.chomp!(''.freeze)
14
34
 
@@ -49,7 +69,7 @@ class String
49
69
  while (i += 1) < len
50
70
  _c = c[i]
51
71
 
52
- if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
72
+ if !exclude_spaces || (exclude_spaces && !(_c == SPACE || _c == TAB))
53
73
  _r = _r.send(r_op, r_val) if r_op
54
74
  _g = _g.send(g_op, g_val) if g_op
55
75
  _b = _b.send(b_op, b_val) if b_op
@@ -70,7 +90,7 @@ class String
70
90
  ret = if !c.empty?
71
91
  chomped ? "\e[0m\n".freeze : "\e[0m".freeze
72
92
  elsif chomped
73
- ?\n.freeze
93
+ NEWLINE
74
94
  end
75
95
 
76
96
  temp << ret
@@ -0,0 +1,81 @@
1
+ module Termclock
2
+ @@cpu_usage = 0
3
+ @@cpu_usage_t = Thread.new { }.join
4
+
5
+ @@current_net_usage = "\u{1F4CA} Curr. DL/UL:"
6
+ @@current_net_usage_t = Thread.new { }.join
7
+
8
+ class << self
9
+ def system_info(width)
10
+ unless @@cpu_usage_t.alive?
11
+ @@cpu_usage_t = Thread.new { @@cpu_usage = LS::CPU.usage(0.25) }
12
+ end
13
+
14
+ unless @@current_net_usage_t.alive?
15
+ @@current_net_usage_t = Thread.new do
16
+ _m = LS::Net.current_usage(0.25)
17
+
18
+ _dl = LS::PrettifyBytes.convert_short_binary(_m[:received], precision: 0)
19
+ _ul = LS::PrettifyBytes.convert_short_binary(_m[:transmitted], precision: 0)
20
+
21
+ @@current_net_usage = "\u{1F4CA} Curr. DL/UL: #{sprintf "%-7s", _dl} | #{sprintf "%7s", _ul}"
22
+ end
23
+ end
24
+
25
+ cpu = "\u{1F9E0} CPU: #{sprintf "%5s", @@cpu_usage}% (#{LS::CPU.count_online} / #{LS::CPU.count})"
26
+
27
+ battery = if LS::Battery.present?
28
+ stat = LS::Battery.stat
29
+ emoji = LS::Battery.charging? ? "\u{1F4A1}" : "\u{1F50B}"
30
+ plug = LS::Battery.charging? ? "\u{1F50C} " : ''.freeze
31
+ "#{emoji} Battery: #{stat[:charge].to_i}% (#{plug}#{stat[:status]})"
32
+ else
33
+ ''.freeze
34
+ end
35
+
36
+ user = "\u{1F481} User: #{LS::User.get_current_user.capitalize}"
37
+ hostname = "\u{1F4BB} Hostname: #{LS::OS.hostname}"
38
+
39
+ _m = LS::Net.total_bytes
40
+ ip = "\u{1F30F} IP Addr: #{LS::Net.ipv4_private}"
41
+
42
+ net_usage = "\u{1F4C8} Totl. DL/UL: #{sprintf "%-7s", LS::PrettifyBytes.convert_short_binary(_m[:received], precision: 0)}"\
43
+ " | #{sprintf "%7s", LS::PrettifyBytes.convert_short_binary(_m[:transmitted], precision: 0)}"
44
+
45
+ _m = LS::Memory.stat
46
+ memory = "\u{1F3B0} Mem: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i * 1024)}"\
47
+ " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i * 1024)}"\
48
+ " (#{_m[:percent_used].to_i}%)"
49
+
50
+ _m = LS::Swap.stat
51
+ swap = "\u{1F300} Swap: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i * 1024)}"\
52
+ " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i * 1024)}"\
53
+ " (#{_m[:percent_used].to_i}%)"
54
+
55
+ _m = LS::Filesystem.stat
56
+ fs = "\u{1F4BD} FS: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i)}"\
57
+ " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i)}"\
58
+ " (#{_m[:used].to_i*(100).fdiv(_m[:total].to_i).round(2)}%)"
59
+
60
+ pt = LS::Process.types.values
61
+
62
+ process = "\u{1F9EE} Process: T:#{sprintf "%4s", LS::Process.count}|"\
63
+ "R:#{sprintf "%3s", pt.count(:running)}|"\
64
+ "S:#{sprintf "%3s", pt.count(:sleeping)}|"\
65
+ "I:#{sprintf "%3s", pt.count(:idle)}"
66
+
67
+ os = "\u{1F427} Distrib: #{LS::OS.distribution} #{LS::OS.machine} (#{LS::OS.version})"
68
+
69
+ max_l = [hostname, process, ip, battery, @@current_net_usage, net_usage].map(&:length).max + 4
70
+
71
+ <<~STR
72
+ \s#{user}#{SPACE.*(width.-(user.length + max_l).abs)}#{hostname}
73
+ \s#{os}#{SPACE.*(width.-(os.length + max_l).abs)}#{battery}
74
+ \s#{cpu}#{SPACE.*(width.-(cpu.length + max_l).abs)}#{ip}
75
+ \s#{memory}#{SPACE.*(width.-(memory.length + max_l).abs)}#{@@current_net_usage}
76
+ \s#{swap}#{SPACE.*(width.-(swap.length + max_l).abs)}#{net_usage}
77
+ \s#{fs}#{SPACE.*(width.-(fs.length + max_l).abs)}#{process}
78
+ STR
79
+ end
80
+ end
81
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Termclock
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: termclock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-14 00:00:00.000000000 Z
11
+ date: 2021-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: linux_stat
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.3.1
19
+ version: 1.4.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.3.1
26
+ version: 1.4.0
27
27
  description: A clock for Linux VTE
28
28
  email:
29
29
  - souravgoswami@protonmail.com
@@ -35,9 +35,10 @@ files:
35
35
  - exe/termclock
36
36
  - lib/termclock.rb
37
37
  - lib/termclock/hex2rgb.rb
38
- - lib/termclock/main.rb
39
38
  - lib/termclock/parse_characters.rb
39
+ - lib/termclock/start.rb
40
40
  - lib/termclock/string.rb
41
+ - lib/termclock/system_info.rb
41
42
  - lib/termclock/version.rb
42
43
  homepage: https://github.com/souravgoswami/termclock
43
44
  licenses:
@@ -1,196 +0,0 @@
1
- module Termclock
2
- COLOURTERM = ENV.key?('COLORTERM')
3
- CLEAR = COLOURTERM ? "\e[H\e[2J\e[3J" : "\e[H"
4
-
5
- def self.start(colour1, colour2, colour3, colour4, textcolour1 = nil, textcolour2 = nil, sleep: 0.1)
6
- newline = ?\n.freeze
7
- space = ?\s.freeze
8
-
9
- generate = proc do |start, stop, n = 5|
10
- r_op = r_val = nil
11
- ary = []
12
-
13
- if start > stop
14
- r_op, r_val = :-, start.-(stop).fdiv(n - 1)
15
- elsif start < stop
16
- r_op, r_val = :+, stop.-(start).fdiv(n - 1)
17
- end
18
-
19
- _r = r_op ? start.send(r_op, r_val * -1) : start
20
- n.times {
21
- _r = _r.send(r_op, r_val) if r_op
22
- ary << _r.clamp(0, 255).to_i
23
- }
24
-
25
- ary
26
- end
27
-
28
- gc_compact, gc_compacted = GC.respond_to?(:compact), Time.now.to_i + 7200
29
- print "\e[H\e[2J\e[3J" unless COLOURTERM
30
-
31
- r1, g1, b1 = *colour1
32
- r2, g2, b2 = *colour2
33
- r3, g3, b3 = *colour3
34
- r4, g4, b4 = *colour4
35
-
36
- # colour1 -> colour3
37
- rs1 = generate.(r1, r3)
38
- gs1 = generate.(g1, g3)
39
- bs1 = generate.(b1, b3)
40
-
41
- # colour2 -> colour4
42
- rs2 = generate.(r2, r4)
43
- gs2 = generate.(g2, g4)
44
- bs2 = generate.(b2, b4)
45
-
46
- # All Gradient Colours
47
- colours = [rs1, gs1, bs1].transpose.zip([rs2, gs2, bs2].transpose)
48
- colours.unshift(colours[0])
49
- colours.push(colours[-1])
50
-
51
- # Text colours
52
- tc1 = textcolour1 ? hex2rgb(textcolour1) : hex2rgb('5555ff')
53
- tc2 = textcolour2 ? hex2rgb(textcolour2) : hex2rgb('3ce3b5')
54
-
55
- cpu_usage = 0
56
- cpu_usage_t = Thread.new { }
57
-
58
- current_net_usage = ''
59
- current_net_usage_t = Thread.new { }
60
-
61
- message_time = Time.now.to_i - 5
62
- message = ''
63
- message_temp = ''
64
- caret = "\u2588"
65
-
66
- get_message = proc {
67
- case Time.now.hour
68
- when 5...12
69
- "\u{1F304} Good Morning..."
70
- when 12...16
71
- "\u26C5 Good Afternoon..."
72
- when 16...18
73
- "\u{1F307} Good Evening..."
74
- when 18...20
75
- "\u{1F31F} Good Evening..."
76
- when 20...24
77
- "\u{1F303} Good Night..."
78
- else
79
- "\u{2728} Good Night..."
80
- end
81
- }
82
-
83
- i = -1
84
- while true
85
- i += 1
86
- time_now = Time.now
87
- height, width = *STDOUT.winsize
88
-
89
- message_align = width - i % width + message.length / 2 - 4
90
-
91
- if (width - i % width <= message.length)
92
- message.replace(message[1..-1])
93
- message_align = width - i % width + 4
94
- else
95
- message.clear if width - i % width == width
96
- message_temp = get_message.call
97
-
98
- if message_temp != message
99
- message << message_temp[message.length..message.length + 1]
100
-
101
- end
102
- end
103
-
104
- if time_now.to_f./(0.5).to_i.even?
105
- splitter = ?:.freeze
106
- _caret = caret
107
- else
108
- splitter = ?$.freeze
109
- _caret = ''
110
- end
111
-
112
- time = time_now.strftime("%H %M %S %2N").split.join(splitter)
113
- art = Termclock::ParseCharacters.display(time).lines
114
-
115
- art_aligned = art.each_with_index do |x, i|
116
- chomped = x.chomp(''.freeze).+(newline)
117
- gr = chomped.gradient(*colours[i])
118
- x.replace(space.*(width./(2.0).-(chomped.length / 2.0).abs.to_i + 1) + gr)
119
- end.join
120
-
121
- vertical_gap = "\e[#{height./(2.0).-(art.length / 2.0).to_i + 1}H"
122
-
123
- unless cpu_usage_t.alive?
124
- cpu_usage_t = Thread.new { cpu_usage = LS::CPU.usage(0.25) }
125
- end
126
-
127
- unless current_net_usage_t.alive?
128
- current_net_usage_t = Thread.new do
129
- _m = LS::Net.current_usage(0.25)
130
-
131
- _dl = LS::PrettifyBytes.convert_short_binary(_m[:received], precision: 0)
132
- _ul = LS::PrettifyBytes.convert_short_binary(_m[:transmitted], precision: 0)
133
-
134
- current_net_usage = "\u{1F4CA} Curr. DL/UL: #{sprintf "%-7s", _dl} | #{sprintf "%7s", _ul}"
135
- end
136
- end
137
-
138
- cpu = "\u{1F9E0} CPU: #{sprintf "%5s", cpu_usage}% (#{LS::CPU.count_online} / #{LS::CPU.count})"
139
-
140
- battery = if LS::Battery.present?
141
- stat = LS::Battery.stat
142
- emoji = LS::Battery.charging? ? "\u{1F4A1}" : "\u{1F50B}"
143
- plug = LS::Battery.charging? ? "\u{1F50C} " : ''.freeze
144
- "#{emoji} Battery: #{stat[:charge].to_i}% (#{plug}#{stat[:status]})"
145
- else
146
- ''.freeze
147
- end
148
-
149
- user = "\u{1F481} User: #{LS::User.get_current_user.capitalize}"
150
- hostname = "\u{1F4BB} Hostname: #{LS::OS.hostname}"
151
-
152
- _m = LS::Net.total_bytes
153
- ip = "\u{1F30F} IP Addr: #{LS::Net.ipv4_private}"
154
-
155
- net_usage = "\u{1F4C8} Totl. DL/UL: #{sprintf "%-7s", LS::PrettifyBytes.convert_short_binary(_m[:received], precision: 0)}"\
156
- " | #{sprintf "%7s", LS::PrettifyBytes.convert_short_binary(_m[:transmitted], precision: 0)}"
157
-
158
- _m = LS::Memory.stat
159
- memory = "\u{1F3B0} Mem: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i * 1024)}"\
160
- " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i * 1024)}"\
161
- " (#{_m[:percent_used].to_i}%)"
162
-
163
- _m = LS::Swap.stat
164
- swap = "\u{1F300} Swap: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i * 1024)}"\
165
- " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i * 1024)}"\
166
- " (#{_m[:percent_used].to_i}%)"
167
-
168
- _m = LS::Filesystem.stat
169
- fs = "\u{1F4BD} FS: #{LS::PrettifyBytes.convert_short_binary(_m[:used].to_i)}"\
170
- " / #{LS::PrettifyBytes.convert_short_binary(_m[:total].to_i)}"\
171
- " (#{_m[:used].to_i*(100).fdiv(_m[:total].to_i).round(2)}%)"
172
-
173
- process = "\u{1F9EE} Process: #{LS::Process.count}"
174
- message_final = message.rjust(message_align).+(_caret).gradient(tc1, tc2, exclude_spaces: true)
175
-
176
- max_l = [hostname, process, ip, battery, current_net_usage, net_usage].map(&:length).max + 4
177
-
178
- info = <<~EOF.gradient(tc1, tc2, exclude_spaces: true)
179
- \s#{user}#{?\s.*(width.-(user.length + max_l).abs)}#{hostname}
180
- \s#{cpu}#{?\s.*(width.-(cpu.length + max_l).abs)}#{battery}
181
- \s#{memory}#{?\s.*(width.-(memory.length + max_l).abs)}#{ip}
182
- \s#{swap}#{?\s.*(width.-(swap.length + max_l).abs)}#{current_net_usage}
183
- \s#{fs}#{?\s.*(width.-(fs.length + max_l).abs)}#{net_usage}
184
- EOF
185
-
186
- print "#{CLEAR}#{info}#{vertical_gap}#{art_aligned}\n#{message_final}"
187
-
188
- if gc_compact && time_now.to_i > gc_compacted
189
- GC.compact
190
- gc_compacted = time_now.to_i + 7200
191
- end
192
-
193
- sleep(sleep)
194
- end
195
- end
196
- end