termclock 0.1.6 → 0.5.0

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: '0916f4a4beb06eae575611343e1235e5fa9a0585116fd9694622d1a5e9867938'
4
- data.tar.gz: 53f9236790df604fe8eb5f0c1a4d0cb5ed0179c6aed3c04da4ae8b26398269ee
3
+ metadata.gz: 7bdab290cda29c688c44fb92516f6b5438d5d64dd9ad240cd2c33f648fbb6ea6
4
+ data.tar.gz: 0137ffaf7141499d18134b9b2d9334b4e37675f401e4b01d01ff0bfc55d8e509
5
5
  SHA512:
6
- metadata.gz: f85098f0082eca3776b0349b65a34323491c8372cf2b426c91bedde2d5031be210da5fe49d36f773a95afdc9fc6907bbcc9b7e1e3d5e863624106d8e4112ac8f
7
- data.tar.gz: a116f54e530fdfcecfc7f415812aa107bddd7e98409090acbd2f039aefccf0db937f47a8f441d5fa35b1fbe75974ff8795745d3a63da28187c85f275090fb24e
6
+ metadata.gz: fd583ba7e1a2896ed7dbf4be664f4cecf5dcc1c136ceca207da1e38978db16c0b884684d02d3577da7af73a7e20a3ee1ad5e6448af5668823c66571cf8fe85b9
7
+ data.tar.gz: 5ee71483b7c1d0e70c5c87e8f4913b7fc0d7786c2f5a3f2d5b8617b8eff79a53ef5d183db8210556835d76367836b2c9c94a50e571b0b29e2399604cef2357ca
@@ -1,28 +1,81 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'termclock'
3
3
  $-v = nil
4
+ STDOUT.sync = true
5
+
6
+ def version(logo_colour)
7
+ emoji = ?🕐.ord.-(1).chr('utf-8')
8
+ Time.now.strftime('%I').to_i.times { emoji.next! }
9
+
10
+ _v = "#{emoji} This is Termclock v#{Termclock::VERSION}"
11
+ vl_2 = _v.length / 2
12
+
13
+ _v[0...vl_2].gradient(logo_colour[0], logo_colour[1], underline: true, bold: true) <<
14
+ _v[vl_2..-1].gradient(logo_colour[1], logo_colour[2], underline: true, bold: true)
15
+ end
16
+
17
+ unless STDOUT.tty?
18
+ IO.undef_method(:winsize)
19
+ IO.define_method(:winsize) { [100, 100] }
20
+ end
21
+
22
+ LOGO_COLOUR = %w(ff0 f55 55f).map(&Termclock.method(:hex2rgb))
4
23
 
5
24
  if ARGV.any? { |x| x[/\A\-(\-help|h)\z/] }
6
25
  puts <<~EOF
7
26
  TermClock: A clock that runs on the LinuxTerminal!
8
27
 
9
- Arguments:
28
+ \e[1;4mArguments:\e[0m
29
+
30
+ \e[1m1. Help and Version:\e[0m
10
31
  --help|-h\t\t\tShows this help section
11
- --bold\t\t\t\tMake texts bold
32
+ --version|-v\t\t\tShows termclock version
33
+
34
+ \e[1m2. Style:\e[0m
35
+ --bold|-b\t\t\tMake texts bold
36
+ --italic|-i\t\t\tMake texts italic
12
37
  --character=|char=\t\tDraws specified character
13
38
  --clean\t\t\t\tJust run the clean bare clock
14
39
  --colour=|-c=\t\t\tSpecify hex colour (4 colours)
15
- \t\t\t\t[ with or without # ]
40
+ --text-colour=|-tc=\t\tSpecify text colour (2 colours)
41
+
42
+ \u2B22 \e[3mColours has to be hex, 3 or 6 characters with or
43
+ without the leading # in front\e[0m
44
+
45
+ \e[1m3. Information:\e[0m
16
46
  --no-date|-nd\t\t\tShows no date
17
47
  --no-message|-nm\t\tShows no messages
18
48
  --no-sysinfo|-ni\t\tShows no system info
19
- --refresh|r\t\t\tSpecify delay or refresh time
20
- --text-colour|-tc\t\tSpecify text colour (2 colours)
49
+ --refresh=|r=\t\t\tSpecify delay or refresh time
50
+ --no-logo|-nl\t\t\tDon't show the logo at the bottom
51
+
52
+ \e[1m4. Antiflicker:\e[0m
53
+ --antiflicker|-af\t\tDon't flicker terminal
54
+
55
+ \u2B22 \e[3mIf refresh time is low or terminal is slow, use this option\e[0m
56
+
57
+ \e[1m5. Formats:\e[0m
58
+ --date-format=|-df=\t\tSpecify the date format
59
+ --time-format=|-tf=\t\tSpecify the time format
60
+
61
+ Usually the format looks like this:
62
+ %H:%M:%S:%2N or %d %B, %Y
63
+
64
+ \u2B22 \e[3mRun `date --help` to know more formats\e[0m
65
+
66
+ Supported characters are 0 - 9, a - z, /, \\, !, %, and |.
67
+
68
+ #{version(LOGO_COLOUR)}
21
69
  EOF
22
70
 
23
71
  exit 0
24
72
  end
25
73
 
74
+ if ARGV.any? { |x| x[/\A\-(-version|v)\z/] }
75
+ puts version(LOGO_COLOUR)
76
+ exit 0
77
+ end
78
+
26
79
  begin
27
80
  print "\e[?25l"
28
81
 
@@ -49,31 +102,56 @@ begin
49
102
  _refresh_time = ARGV.find { |x| x[/\A\-(\-refresh|r)=.*\z/] } &.split(?=) &.at(1)
50
103
  refresh_time = _refresh_time ? _refresh_time.to_f : 0.1
51
104
 
105
+ _time_format = ARGV.find { |x| x[/\A\-(\-time\-format|tf)=.*\z/] } &.split(?=) &.at(1)
106
+ time_format = _time_format ? _time_format : "%H %M %S %2N"
107
+
108
+ _date_format = ARGV.find { |x| x[/\A\-(\-date\-format|df)=.*\z/] } &.split(?=) &.at(1)
109
+ date_format = _date_format ? _date_format : "%a, %d %B %Y"
110
+
52
111
  text_colours = ARGV.find { |x| x[/\A\-(\-text\-colour|tc)=.*\z/] } &.split(?=) &.at(1) &.split(?,)
53
112
  abort("Text colours need 2 colours. Example: -tc=55f,3ce3b5") if text_colours && text_colours.length != 2
54
113
 
55
- bold = ARGV.any? { |x| x[/\A\-\-bold\z/] }
114
+ bold = ARGV.any? { |x| x[/\A\-(\-bold|b)\z/] }
115
+ italic = ARGV.any? { |x| x[/\A\-(\-italic|i)\z/] }
116
+ anti_flicker = ARGV.any? { |x| x[/\A\-(\-antiflicker|af)\z/] }
117
+
56
118
  no_print_info = ARGV.any? { |x| x[/\A\-(\-no\-sysinfo|ni)\z/] }
119
+ no_logo = ARGV.any? { |x| x[/\A\-(\-no\-logo|nl)\z/] }
57
120
  no_print_message = ARGV.any? { |x| x[/\A\-(\-no\-message|nm)\z/] }
58
121
  no_print_date = ARGV.any? { |x| x[/\A\-(\-no\-date|nd)\z/] }
59
122
 
60
123
  clean = ARGV.any? { |x| x[/\A\-(\-clean)\z/] }
61
124
 
62
125
  if clean
63
- no_print_info = no_print_message = no_print_date = true
126
+ no_print_info = no_print_message = no_print_date = no_logo = true
127
+ end
128
+
129
+ begin
130
+ Time.now.strftime(time_format)
131
+ Time.now.strftime(date_format)
132
+ rescue
133
+ abort "Date / Time format is invalid\n#{$!.to_s}"
64
134
  end
65
135
 
66
136
  Termclock.start(
67
137
  *colours,
68
138
  *text_colours,
69
- sleep: refresh_time,
139
+ refresh: refresh_time,
70
140
  bold: bold,
141
+ italic: italic,
71
142
  print_info: !no_print_info,
72
143
  print_message: !no_print_message,
73
- print_date: !no_print_date
144
+ print_date: !no_print_date,
145
+ no_logo: no_logo,
146
+ time_format: time_format,
147
+ date_format: date_format,
148
+ logo_colour: LOGO_COLOUR,
149
+ anti_flicker: anti_flicker
74
150
  )
75
151
  rescue Interrupt, SignalException
76
152
  print "\e[H\e[2J\e[3J"
153
+ # rescue Errno::ENOTTY
154
+ # abort "This should be run on a TTY! Also please don't pipe or redirect the output!"
77
155
  ensure
78
156
  print "\e[?25h"
79
157
  end
@@ -1,17 +1,20 @@
1
1
  require 'linux_stat'
2
2
  require 'io/console'
3
+ require_relative "termclock/version"
3
4
 
4
5
  module Termclock
5
- COLOURTERM = ENV.key?('COLORTERM')
6
- CLEAR = COLOURTERM ? "\e[H\e[2J\e[3J" : "\e[H"
6
+ GC_COMPACT_TIME = 7200
7
+ CLEAR = "\e[H\e[2J\e[3J".freeze
8
+ ANTIFLICKER = "\e[J\e[1;1H".freeze
7
9
  NEWLINE = ?\n.freeze
8
10
  SPACE = ?\s.freeze
9
11
  TAB = ?\t.freeze
12
+ EMPTY = ''.freeze
13
+ EPSILON = 5.0e-07
10
14
  end
11
15
 
12
16
  require_relative "termclock/string"
13
17
  require_relative "termclock/parse_characters"
14
18
  require_relative "termclock/system_info"
15
- require_relative "termclock/main"
19
+ require_relative "termclock/start"
16
20
  require_relative "termclock/hex2rgb"
17
- require_relative "termclock/version"
@@ -3,7 +3,7 @@ module Termclock
3
3
  colour = hex.dup.to_s
4
4
  colour.strip!
5
5
  colour.downcase!
6
- colour[0] = ''.freeze if colour[0] == ?#.freeze
6
+ colour[0] = EMPTY if colour[0] == ?#.freeze
7
7
 
8
8
  # out of range
9
9
  oor = colour.scan(/[^a-f0-9]/)
@@ -1,270 +1,305 @@
1
1
  module Termclock
2
2
  module ParseCharacters
3
3
  CHARACTERS = <<~EOF.freeze
4
- # 0
4
+ #\s0
5
5
  ````````
6
- `` ``
7
- `` ``
8
- `` ``
6
+ ``\s\s\s\s``
7
+ ``\s\s\s\s``
8
+ ``\s\s\s\s``
9
9
  ````````
10
10
 
11
- # 1
12
- ``
11
+ #\s1
12
+ \s\s\s``
13
13
  `````
14
- ``
15
- ``
14
+ \s\s\s``
15
+ \s\s\s``
16
16
  ````````
17
17
 
18
- # 2
18
+ #\s2
19
19
  ````````
20
- ``
20
+ \s\s\s\s\s\s``
21
21
  ````````
22
22
  ``
23
23
  ````````
24
24
 
25
- # 3
25
+ #\s3
26
26
  ````````
27
- ``
27
+ \s\s\s\s\s\s``
28
28
  ````````
29
- ``
29
+ \s\s\s\s\s\s``
30
30
  ````````
31
31
 
32
- # 4
33
- `` ``
34
- `` ``
32
+ #\s4
33
+ ``\s\s\s\s``
34
+ ``\s\s\s\s``
35
35
  ````````
36
- ``
37
- ``
36
+ \s\s\s\s\s\s``
37
+ \s\s\s\s\s\s``
38
38
 
39
- # 5
39
+ #\s5
40
40
  ````````
41
41
  ``
42
42
  ````````
43
- ``
43
+ \s\s\s\s\s\s``
44
44
  ````````
45
45
 
46
- # 6
46
+ #\s6
47
47
  ````````
48
48
  ``
49
49
  ````````
50
- `` ``
50
+ ``\s\s\s\s``
51
51
  ````````
52
52
 
53
- # 7
53
+ #\s7
54
54
  ````````
55
- ``
56
- ``
57
- ``
58
- ``
55
+ \s\s\s\s\s\s``
56
+ \s\s\s\s\s\s``
57
+ \s\s\s\s\s\s``
58
+ \s\s\s\s\s\s``
59
59
 
60
- # 8
60
+ #\s8
61
61
  ````````
62
- `` ``
62
+ ``\s\s\s\s``
63
63
  ````````
64
- `` ``
64
+ ``\s\s\s\s``
65
65
  ````````
66
66
 
67
- # 9
67
+ #\s9
68
68
  ````````
69
- `` ``
69
+ ``\s\s\s\s``
70
70
  ````````
71
- ``
71
+ \s\s\s\s\s\s``
72
72
  ````````
73
73
 
74
- # :
74
+ #\s:
75
75
  \u2B29\u2B29
76
76
  \u2B29\u2B29
77
77
 
78
78
  \u2B29\u2B29
79
79
  \u2B29\u2B29
80
80
 
81
- # $
81
+ #\s$
82
82
  \s\s
83
83
  \s\s
84
84
 
85
85
  \s\s
86
86
  \s\s
87
- # A
87
+ #\sA
88
88
  ````````
89
- `` ``
89
+ ``\s\s\s\s``
90
90
  ````````
91
- `` ``
92
- `` ``
91
+ ``\s\s\s\s``
92
+ ``\s\s\s\s``
93
93
 
94
- # B
94
+ #\sB
95
95
  ````````
96
- `` ``
96
+ ``\s\s\s\s\s``
97
97
  ````````
98
- `` ``
98
+ ``\s\s\s\s\s``
99
99
  ````````
100
100
 
101
- # C
101
+ #\sC
102
102
  ````````
103
103
  ``
104
104
  ``
105
105
  ``
106
106
  ````````
107
107
 
108
- # D
108
+ #\sD
109
109
  ````````
110
- `` ``
111
- `` ``
112
- `` ``
110
+ ``\s\s\s\s\s``
111
+ ``\s\s\s\s\s\s``
112
+ ``\s\s\s\s\s``
113
113
  ````````
114
114
 
115
- # E
115
+ #\sE
116
116
  ````````
117
117
  ``
118
118
  ````````
119
119
  ``
120
120
  ````````
121
121
 
122
- # F
122
+ #\sF
123
123
  ````````
124
124
  ``
125
125
  ````````
126
126
  ``
127
127
  ``
128
128
 
129
- # G
129
+ #\sG
130
130
  ````````
131
131
  ``
132
132
  ``
133
- `` ````
133
+ ``\s\s````
134
134
  ````````
135
135
 
136
- # H
137
- `` ``
138
- `` ``
136
+ #\sH
137
+ ``\s\s\s\s``
138
+ ``\s\s\s\s``
139
139
  ````````
140
- `` ``
141
- `` ``
140
+ ``\s\s\s\s``
141
+ ``\s\s\s\s``
142
142
 
143
- # I
143
+ #\sI
144
144
  ````````
145
- `
146
- `
147
- `
145
+ \s\s\s``
146
+ \s\s\s``
147
+ \s\s\s``
148
148
  ````````
149
149
 
150
- # J
150
+ #\sJ
151
151
  ````````
152
- ``
153
- ``
154
- `` ``
155
- ```````
152
+ \s\s\s\s\s\s``
153
+ \s\s\s\s\s\s``
154
+ \s``\s\s\s``
155
+ \s```````
156
156
 
157
- # K
158
- `` ``
159
- `` ``
157
+ #\sK
158
+ ``\s\s\s\s``
159
+ ``\s\s``
160
160
  `````
161
- `` ``
162
- `` ``
161
+ ``\s\s``
162
+ ``\s\s\s\s``
163
163
 
164
- # L
164
+ #\sL
165
165
  ``
166
166
  ``
167
167
  ``
168
168
  ``
169
169
  ````````
170
170
 
171
- # M
172
- ``` ```
173
- `` `` ``
174
- `` ``
175
- `` ``
176
- `` ``
171
+ #\sM
172
+ ```\s\s```
173
+ ``\s``\s``
174
+ ``\s\s\s\s``
175
+ ``\s\s\s\s``
176
+ ``\s\s\s\s``
177
177
 
178
- # N
179
- ```` ``
180
- `` `` ``
181
- `` `` ``
182
- `` `` ``
183
- `` ````
178
+ #\sN
179
+ ````\s\s``
180
+ ``\s``\s``
181
+ ``\s``\s``
182
+ ``\s``\s``
183
+ ``\s\s````
184
184
 
185
- # O
185
+ #\sO
186
186
  ````````
187
- `` ``
188
- `` ``
189
- `` ``
187
+ ``\s\s\s\s``
188
+ ``\s\s\s\s``
189
+ ``\s\s\s\s``
190
190
  ````````
191
191
 
192
- # P
192
+ #\sP
193
193
  ````````
194
- `` ``
194
+ ``\s\s\s\s``
195
195
  ````````
196
196
  ``
197
197
  ``
198
198
 
199
- # Q
199
+ #\sQ
200
200
  ````````
201
- `` ``
201
+ ``\s\s\s\s``
202
202
  ````````
203
- ``
204
- ``
203
+ \s\s\s\s\s\s``
204
+ \s\s\s\s\s\s``
205
205
 
206
- # R
206
+ #\sR
207
207
  ````````
208
- `` ``
208
+ ``\s\s\s\s``
209
209
  ````````
210
210
  `````
211
- `` ``
211
+ ``\s\s\s``
212
212
 
213
- # S
213
+ #\sS
214
214
  ````````
215
215
  ``
216
216
  ````````
217
- ``
218
- ````````
219
-
220
- # T
221
- ````````
222
- ``
223
- ``
224
- ``
225
- ``
226
-
227
- # U
228
- `` ``
229
- `` ``
230
- `` ``
231
- `` ``
232
- ````````
233
-
234
- # V
235
- `` ``
236
- `` ``
237
- `` ``
238
- `` ``
239
- `
240
-
241
- # W
242
- `` ``
243
- `` ``
244
- `` ``
245
- `` ` ``
246
- ````````
247
-
248
- # X
249
- ` `
250
- ` `
251
- `
252
- ` `
253
- ` `
254
-
255
- # Y
256
- `` ``
257
- `` ``
258
- ````````
259
- ``
260
- ``
261
-
262
- # Z
263
- ````````
264
- ``
265
- ``
266
- ``
267
- ````````
217
+ \s\s\s\s\s\s``
218
+ ````````
219
+
220
+ #\sT
221
+ ````````
222
+ \s\s\s``
223
+ \s\s\s``
224
+ \s\s\s``
225
+ \s\s\s``
226
+
227
+ #\sU
228
+ ``\s\s\s\s``
229
+ ``\s\s\s\s``
230
+ ``\s\s\s\s``
231
+ ``\s\s\s\s``
232
+ ````````
233
+
234
+ #\sV
235
+ ``\s\s\s\s``
236
+ ``\s\s\s\s``
237
+ ``\s\s\s\s``
238
+ \s``\s\s``\s
239
+ \s\s\s``
240
+
241
+ #\sW
242
+ ``\s\s\s\s``
243
+ ``\s\s\s\s``
244
+ ``\s\s\s\s``
245
+ ``\s`\s\s``
246
+ ````````
247
+
248
+ #\sX
249
+ ``\s\s\s\s``
250
+ \s``\s\s``
251
+ \s\s\s``
252
+ \s``\s\s``
253
+ ``\s\s\s\s``
254
+
255
+ #\sY
256
+ ``\s\s\s\s``
257
+ ``\s\s\s\s``
258
+ ````````
259
+ \s\s\s``
260
+ \s\s\s``
261
+
262
+ #\sZ
263
+ ````````
264
+ \s\s\s\s\s``
265
+ \s\s\s``
266
+ \s``
267
+ ````````
268
+
269
+ #\s/
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
+ #\s\\
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
+ #\s%
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
+ #\s|
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
+ #\s!
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
268
303
  EOF
269
304
 
270
305
  class << self
@@ -0,0 +1,211 @@
1
+ module Termclock
2
+ def self.start(colour1, colour2, colour3, colour4,
3
+ textcolour1 = nil, textcolour2 = nil,
4
+ refresh: 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
+ no_logo: false,
12
+ anti_flicker: false,
13
+ logo_colour: [Termclock.hex2rgb('ff0'), Termclock.hex2rgb('f55'), Termclock.hex2rgb('55f')]
14
+ )
15
+
16
+ clear_character = anti_flicker ? ANTIFLICKER : CLEAR
17
+
18
+ generate = proc do |start, stop, n = 5|
19
+ r_op = r_val = nil
20
+ ary = []
21
+
22
+ if start > stop
23
+ r_op, r_val = :-, start.-(stop).fdiv(n - 1)
24
+ elsif start < stop
25
+ r_op, r_val = :+, stop.-(start).fdiv(n - 1)
26
+ end
27
+
28
+ _r = r_op ? start.send(r_op, r_val * -1) : start
29
+ n.times {
30
+ _r = _r.send(r_op, r_val) if r_op
31
+ ary << _r.clamp(0, 255).to_i
32
+ }
33
+
34
+ ary
35
+ end
36
+
37
+ gc_compact, gc_compacted = GC.respond_to?(:compact), Time.now.to_i + GC_COMPACT_TIME
38
+
39
+ r1, g1, b1 = *colour1
40
+ r2, g2, b2 = *colour2
41
+ r3, g3, b3 = *colour3
42
+ r4, g4, b4 = *colour4
43
+
44
+ # colour1 -> colour3
45
+ rs1 = generate.(r1, r3)
46
+ gs1 = generate.(g1, g3)
47
+ bs1 = generate.(b1, b3)
48
+
49
+ # colour2 -> colour4
50
+ rs2 = generate.(r2, r4)
51
+ gs2 = generate.(g2, g4)
52
+ bs2 = generate.(b2, b4)
53
+
54
+ # All Gradient Colours
55
+ colours = [rs1, gs1, bs1].transpose.zip([rs2, gs2, bs2].transpose)
56
+ colours.unshift(colours[0])
57
+ colours.push(colours[-1])
58
+
59
+ # Text colours
60
+ tc1 = textcolour1 ? hex2rgb(textcolour1) : hex2rgb('5555ff')
61
+ tc2 = textcolour2 ? hex2rgb(textcolour2) : hex2rgb('3ce3b5')
62
+
63
+ message_counter = -1
64
+ message = ''
65
+ message_final = ''
66
+ message_align = 0
67
+ message_temp = ''
68
+
69
+ date, info = '', ''
70
+
71
+ clock_emoji = ?\u{1F550}.ord.-(1).chr('utf-8')
72
+ clock_emoji = 12.times.map { clock_emoji.next!.dup }
73
+
74
+ braille = %W(\u2821 \u2811 \u2812 \u280A \u280C)
75
+
76
+ get_message = proc {
77
+ braille.rotate!
78
+ braille_0 = braille[0]
79
+
80
+ case Time.now.hour
81
+ when 5...12
82
+ "\u{1F304} #{braille_0} Good Morning #{braille_0} \u{1F304}"
83
+ when 12...16
84
+ "\u26C5 #{braille_0} Good Afternoon #{braille_0} \u26C5"
85
+ when 16...18
86
+ "\u{1F307} #{braille_0} Good Evening #{braille_0} \u{1F307}"
87
+ when 18...20
88
+ "\u{1F31F} #{braille_0} Good Evening #{braille_0} \u{1F31F}"
89
+ when 20...24
90
+ "\u{1F303} #{braille_0} Good Night #{braille_0} \u{1F303}"
91
+ else
92
+ "\u{2728} #{braille_0} Good Night #{braille_0} \u{2728}"
93
+ end
94
+ }
95
+
96
+ version = "Termclock v#{Termclock::VERSION}"
97
+
98
+ v_col1 = logo_colour[0]
99
+ v_col2 = logo_colour[1]
100
+ v_col3 = logo_colour[2]
101
+
102
+ vl_2 = version.length / 2
103
+ _term_clock_v = version[0...vl_2].gradient(v_col1, v_col2, underline: true, bold: bold, italic: italic) <<
104
+ version[vl_2..-1].gradient(v_col2, v_col3, underline: true, bold: bold, italic: italic)
105
+
106
+ term_clock_v = ''
107
+
108
+ chop_message = 0
109
+ deviation = 0
110
+
111
+ time_seperators = [?:, ?$]
112
+ time_seperator = time_seperators[0]
113
+ point_five_tick = -0.5
114
+
115
+ height, width = *STDOUT.winsize
116
+ height2, width2 = *STDOUT.winsize
117
+
118
+ print CLEAR
119
+
120
+ while true
121
+ monotonic_time_1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
122
+ time_now = Time.now
123
+ height, width = *STDOUT.winsize
124
+
125
+ if time_now.to_f > point_five_tick
126
+ point_five_tick = time_now.to_f + 0.5
127
+ time_seperators.rotate!
128
+ clock_emoji.rotate!
129
+ time_seperator = time_seperators[0]
130
+ end
131
+
132
+ unless no_logo
133
+ term_clock_v = "\e[#{height}H #{clock_emoji[0]} #{_term_clock_v} \e[0m"
134
+ end
135
+
136
+ if print_message
137
+ message_temp = get_message.call
138
+ message_counter += 1
139
+ message_length = message.length
140
+
141
+ if (width - message_counter % width < 8)
142
+ unless chop_message == message_temp.length
143
+ chop_message += 1
144
+ message_counter -= 1
145
+ message_align -= 1
146
+ message.replace(message_temp[chop_message..-1])
147
+ end
148
+ else
149
+ chop_message = 0 unless chop_message == 0
150
+ message.clear if width - message_counter % width == width
151
+ message_align = width - message_counter % width + message_length - 4
152
+
153
+ if message_temp != message
154
+ if message_length < message_temp.length
155
+ message.replace(message_temp[0..message_length])
156
+ else
157
+ message.replace(message_temp)
158
+ end
159
+ end
160
+ end
161
+
162
+ message_final = (message).rjust(message_align).gradient(
163
+ tc1, tc2, exclude_spaces: true, bold: bold, italic: italic
164
+ )
165
+ end
166
+
167
+ info = system_info(width, tc1, tc2, bold, italic) if print_info
168
+
169
+ if print_date
170
+ date = time_now.strftime(date_format).center(width)
171
+ .gradient(tc1, tc2, bold: bold, italic: italic, exclude_spaces: true)
172
+ end
173
+
174
+ time = time_now.strftime(time_format).split.join(time_seperator)
175
+ art = Termclock::ParseCharacters.display(time).lines
176
+
177
+ art_aligned = art.each_with_index do |x, i|
178
+ chomped = x.chomp(EMPTY).+(NEWLINE)
179
+ gr = chomped.gradient(*colours[i], bold: bold, italic: italic)
180
+
181
+ w = width./(2.0).-(chomped.length / 2.0) + 2
182
+ w = 0 if w < 0
183
+
184
+ x.replace(SPACE.*(w) + gr)
185
+ end.join
186
+
187
+ vertical_gap = "\e[#{height./(2.0).-(art.length / 2.0).to_i + 1}H"
188
+ final_output = "#{info}#{vertical_gap}#{art_aligned}\n#{date}\n\n\e[K#{message_final}#{term_clock_v}"
189
+
190
+ if anti_flicker && (height != height2 || width != width2)
191
+ height2, width2 = *STDOUT.winsize
192
+ print CLEAR
193
+ end
194
+
195
+ print "#{clear_character}#{final_output}"
196
+
197
+ if gc_compact && time_now.to_i > gc_compacted
198
+ GC.compact
199
+ gc_compacted = time_now.to_i + GC_COMPACT_TIME
200
+ end
201
+
202
+ monotonic_time_2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
203
+ time_diff = monotonic_time_2 - monotonic_time_1
204
+ sleep_time = refresh.-(time_diff + deviation)
205
+ sleep_time = 0 if sleep_time < 0
206
+
207
+ sleep(sleep_time)
208
+ deviation = Process.clock_gettime(Process::CLOCK_MONOTONIC).-(monotonic_time_2 + sleep_time + EPSILON)
209
+ end
210
+ end
211
+ end
@@ -2,8 +2,14 @@ class String
2
2
  NEWLINE = ?\n.freeze
3
3
  SPACE = ?\s.freeze
4
4
  TAB = ?\t.freeze
5
+ EMPTY = ''.freeze
6
+
7
+ def gradient(*all_rgbs,
8
+ bg: false,
9
+ exclude_spaces: false,
10
+ bold: false, italic: false, underline: false
11
+ )
5
12
 
6
- def gradient(*all_rgbs, bg: false, exclude_spaces: false, bold: false)
7
13
  temp = ''
8
14
 
9
15
  r, g, b = all_rgbs[0]
@@ -12,9 +18,21 @@ class String
12
18
 
13
19
  init = bg ? 48 : 38
14
20
 
21
+ style = nil
22
+ if bold || italic || underline
23
+ style = "\e["
24
+ style << '1;'.freeze if bold
25
+ style << '3;'.freeze if italic
26
+ style << '4;'.freeze if underline
27
+ style.chop!
28
+ style << 'm'.freeze
29
+ end
30
+
15
31
  each_line do |c|
32
+ temp << style if style
33
+
16
34
  _r, _g, _b = r, g, b
17
- chomped = !!c.chomp!(''.freeze)
35
+ chomped = !!c.chomp!(EMPTY)
18
36
 
19
37
  len = c.length
20
38
  n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
@@ -49,8 +67,6 @@ class String
49
67
  _g = _g.send(g_op, g_val * -1) if g_op
50
68
  _b = _b.send(b_op, b_val * -1) if b_op
51
69
 
52
- temp << "\e[1m" if bold
53
-
54
70
  i = -1
55
71
  while (i += 1) < len
56
72
  _c = c[i]
@@ -2,35 +2,44 @@ module Termclock
2
2
  @@cpu_usage = 0
3
3
  @@cpu_usage_t = Thread.new { }.join
4
4
 
5
- @@current_net_usage = ''
5
+ @@current_net_usage = "\u{1F4CA} Curr. DL/UL:"
6
6
  @@current_net_usage_t = Thread.new { }.join
7
7
 
8
8
  class << self
9
- def system_info(width, tc1, tc2, bold)
9
+ def system_info(width, tc1, tc2, bold, italic)
10
10
  unless @@cpu_usage_t.alive?
11
- @@cpu_usage_t = Thread.new { @@cpu_usage = LS::CPU.usage(0.25) }
11
+ @@cpu_usage_t = Thread.new {
12
+ _cpu_usage = LS::CPU.usage(0.25)
13
+ _cpu_usage_i = _cpu_usage.to_i
14
+
15
+ @@cpu_usage = "%0.2f" % _cpu_usage
16
+ }
12
17
  end
13
18
 
14
19
  unless @@current_net_usage_t.alive?
15
20
  @@current_net_usage_t = Thread.new do
16
21
  _m = LS::Net.current_usage(0.25)
17
22
 
18
- _dl = LS::PrettifyBytes.convert_short_binary(_m[:received], precision: 0)
19
- _ul = LS::PrettifyBytes.convert_short_binary(_m[:transmitted], precision: 0)
23
+ _dl = LS::PrettifyBytes.convert_short_decimal(_m[:received])
24
+ _ul = LS::PrettifyBytes.convert_short_decimal(_m[:transmitted])
20
25
 
21
- @@current_net_usage = "\u{1F4CA} Curr. DL/UL: #{sprintf "%-7s", _dl} | #{sprintf "%7s", _ul}"
26
+ @@current_net_usage = "\u{1F4CA} Curr. DL/UL: #{"%-9s" % _dl} | #{ "%9s" % _ul}"
22
27
  end
23
28
  end
24
29
 
25
- cpu = "\u{1F9E0} CPU: #{sprintf "%5s", @@cpu_usage}% (#{LS::CPU.count_online} / #{LS::CPU.count})"
30
+ cpu = "\u{1F9E0} CPU: #{"%6s" % @@cpu_usage}% (#{LS::CPU.count_online}/#{LS::CPU.count})"
26
31
 
27
32
  battery = if LS::Battery.present?
28
33
  stat = LS::Battery.stat
29
- emoji = LS::Battery.charging? ? "\u{1F4A1}" : "\u{1F50B}"
30
- plug = LS::Battery.charging? ? "\u{1F50C} " : ''.freeze
34
+ emoji, plug = "\u{1F50B}".freeze, EMPTY
35
+
36
+ if LS::Battery.charging?
37
+ emoji, plug = "\u{1F4A1}".freeze, "\u{1F50C} ".freeze
38
+ end
39
+
31
40
  "#{emoji} Battery: #{stat[:charge].to_i}% (#{plug}#{stat[:status]})"
32
41
  else
33
- ''.freeze
42
+ EMPTY
34
43
  end
35
44
 
36
45
  user = "\u{1F481} User: #{LS::User.get_current_user.capitalize}"
@@ -39,35 +48,106 @@ module Termclock
39
48
  _m = LS::Net.total_bytes
40
49
  ip = "\u{1F30F} IP Addr: #{LS::Net.ipv4_private}"
41
50
 
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)}"
51
+ net_usage = "\u{1F4C8} Totl. DL/UL: #{"%-9s" % LS::PrettifyBytes.convert_short_decimal(_m[:received])}"\
52
+ " | #{"%9s" % LS::PrettifyBytes.convert_short_decimal(_m[:transmitted])}"
44
53
 
45
54
  _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}%)"
55
+ _m.default = 0
56
+
57
+ memory = "\u{1F3B0} Mem: #{LS::PrettifyBytes.convert_short_decimal(_m[:used] * 1000)}"\
58
+ " / #{LS::PrettifyBytes.convert_short_decimal(_m[:total] * 1000)}"\
59
+ " (#{"%.2f" % _m[:percent_used]}%)"
49
60
 
50
61
  _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
- process = "\u{1F9EE} Process: #{LS::Process.count}"
61
-
62
- max_l = [hostname, process, ip, battery, @@current_net_usage, net_usage].map(&:length).max + 4
63
-
64
- <<~EOF.gradient(tc1, tc2, exclude_spaces: true, bold: bold)
65
- \s#{user}#{SPACE.*(width.-(user.length + max_l).abs)}#{hostname}
66
- \s#{cpu}#{SPACE.*(width.-(cpu.length + max_l).abs)}#{battery}
67
- \s#{memory}#{SPACE.*(width.-(memory.length + max_l).abs)}#{ip}
68
- \s#{swap}#{SPACE.*(width.-(swap.length + max_l).abs)}#{@@current_net_usage}
69
- \s#{fs}#{SPACE.*(width.-(fs.length + max_l).abs)}#{net_usage}
70
- EOF
62
+ _m.default = 0
63
+
64
+ swap = "\u{1F300} Swap: #{LS::PrettifyBytes.convert_short_decimal(_m[:used] * 1000)}"\
65
+ " / #{LS::PrettifyBytes.convert_short_decimal(_m[:total] * 1000)}"\
66
+ " (#{"%.2f" % _m[:percent_used]}%)"
67
+
68
+ _m = LS::Filesystem.stat('/')
69
+ _m.default = 0
70
+
71
+ fs = "\u{1F4BD} FS: #{LS::PrettifyBytes.convert_short_decimal(_m[:used])}"\
72
+ " / #{LS::PrettifyBytes.convert_short_decimal(_m[:total])}"\
73
+ " (#{"%.2f" % _m[:used].*(100).fdiv(_m[:total]).round(2)}%)"
74
+
75
+ pt = LS::Process.types.values
76
+
77
+ process = "\u{1F9EE} Process: T:#{"%4s" % pt.length}|"\
78
+ "R:#{"%3s" % pt.count(:running)}|"\
79
+ "S:#{"%3s" % pt.count(:sleeping)}|"\
80
+ "I:#{"%3s" % pt.count(:idle)}"
81
+
82
+ @@os_v ||= unless LS::OS.version.empty?
83
+ " (#{LS::OS.version})"
84
+ else
85
+ EMPTY
86
+ end
87
+
88
+ @@os ||= "\u{1F427} Distrib: #{LS::OS.distribution} #{LS::OS.machine}#{@@os_v}"
89
+
90
+ _uptime = LS::OS.uptime
91
+ _second = _uptime[:second]
92
+ _second_i = _second.to_i
93
+
94
+ hour = "%02d" % _uptime[:hour]
95
+ minute = "%02d" % _uptime[:minute]
96
+ second = "%02d" % _second_i
97
+ ms = "%02d" % _second.-(_second_i).*(100)
98
+
99
+ uptime = "\u{1F3A1} Uptime: #{hour}:#{minute}:#{second}:#{ms} (#{LS::OS.uptime_i}s)"
100
+
101
+ _loadavg = LS::Sysinfo.loads.map! { |x| "%.2f" % x }
102
+ loadavg = "\u{1F9FA} LoadAvg: 1m #{_loadavg[0]}|5m #{_loadavg[1]}|15m #{_loadavg[2]}"
103
+
104
+ all_info = []
105
+ max_l = 0
106
+ i = -1
107
+
108
+ [
109
+ user, hostname,
110
+ @@os, battery,
111
+ cpu, ip,
112
+ memory, @@current_net_usage,
113
+ swap, net_usage,
114
+ fs, process,
115
+ uptime, loadavg
116
+ ].each { |x|
117
+ unless x.empty?
118
+ all_info << x
119
+ i += 1
120
+
121
+ if i.odd?
122
+ _x_len = x.length
123
+ max_l = _x_len if max_l < _x_len
124
+ end
125
+ end
126
+ }
127
+
128
+ max_l += 4
129
+
130
+ all_info.each_slice(2).map { |x, y|
131
+ _diff = width.-(x.length + max_l)
132
+ _diff = 0 if _diff < 1
133
+ y_to_s = y.to_s
134
+
135
+ padding = "#{SPACE * _diff}"
136
+ str = SPACE + x + padding + y_to_s
137
+
138
+ grads = SPACE + x.gradient(tc1, tc2, bold: bold, italic: italic) +
139
+ padding +
140
+ y_to_s.gradient(tc1, tc2, bold: bold, italic: italic)
141
+
142
+ len = str.chars.map { |x|
143
+ _x = x.bytesize./(2)
144
+ _x == 0 ? 1 : _x
145
+ }.sum
146
+
147
+ w = width - 2
148
+
149
+ len < w ? grads.+(SPACE.*(w - len)) : grads
150
+ }.join(NEWLINE)
71
151
  end
72
152
  end
73
153
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Termclock
4
- VERSION = "0.1.6"
4
+ VERSION = "0.5.0"
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.6
4
+ version: 0.5.0
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-24 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.5.1
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.5.1
27
27
  description: A clock for Linux VTE
28
28
  email:
29
29
  - souravgoswami@protonmail.com
@@ -35,8 +35,8 @@ 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
41
  - lib/termclock/system_info.rb
42
42
  - lib/termclock/version.rb
@@ -1,148 +0,0 @@
1
- module Termclock
2
- def self.start(colour1, colour2, colour3, colour4,
3
- textcolour1 = nil, textcolour2 = nil,
4
- sleep: 0.1,
5
- bold: false,
6
- print_info: true, print_message: true,
7
- print_date: true
8
- )
9
-
10
- generate = proc do |start, stop, n = 5|
11
- r_op = r_val = nil
12
- ary = []
13
-
14
- if start > stop
15
- r_op, r_val = :-, start.-(stop).fdiv(n - 1)
16
- elsif start < stop
17
- r_op, r_val = :+, stop.-(start).fdiv(n - 1)
18
- end
19
-
20
- _r = r_op ? start.send(r_op, r_val * -1) : start
21
- n.times {
22
- _r = _r.send(r_op, r_val) if r_op
23
- ary << _r.clamp(0, 255).to_i
24
- }
25
-
26
- ary
27
- end
28
-
29
- gc_compact, gc_compacted = GC.respond_to?(:compact), Time.now.to_i + 7200
30
- print "\e[H\e[2J\e[3J" unless COLOURTERM
31
-
32
- r1, g1, b1 = *colour1
33
- r2, g2, b2 = *colour2
34
- r3, g3, b3 = *colour3
35
- r4, g4, b4 = *colour4
36
-
37
- # colour1 -> colour3
38
- rs1 = generate.(r1, r3)
39
- gs1 = generate.(g1, g3)
40
- bs1 = generate.(b1, b3)
41
-
42
- # colour2 -> colour4
43
- rs2 = generate.(r2, r4)
44
- gs2 = generate.(g2, g4)
45
- bs2 = generate.(b2, b4)
46
-
47
- # All Gradient Colours
48
- colours = [rs1, gs1, bs1].transpose.zip([rs2, gs2, bs2].transpose)
49
- colours.unshift(colours[0])
50
- colours.push(colours[-1])
51
-
52
- # Text colours
53
- tc1 = textcolour1 ? hex2rgb(textcolour1) : hex2rgb('5555ff')
54
- tc2 = textcolour2 ? hex2rgb(textcolour2) : hex2rgb('3ce3b5')
55
-
56
- cpu_usage = 0
57
- cpu_usage_t = Thread.new { }
58
-
59
- current_net_usage = ''
60
- current_net_usage_t = Thread.new { }
61
-
62
- message_time = Time.now.to_i - 5
63
- message_counter = -1
64
- message = ''
65
- message_final = ''
66
- message_temp = ''
67
- caret = "\u2588"
68
-
69
- date, info = '', ''
70
-
71
- get_message = proc {
72
- case Time.now.hour
73
- when 5...12
74
- "\u{1F304} Good Morning..."
75
- when 12...16
76
- "\u26C5 Good Afternoon..."
77
- when 16...18
78
- "\u{1F307} Good Evening..."
79
- when 18...20
80
- "\u{1F31F} Good Evening..."
81
- when 20...24
82
- "\u{1F303} Good Night..."
83
- else
84
- "\u{2728} Good Night..."
85
- end
86
- }
87
-
88
- while true
89
- time_now = Time.now
90
- height, width = *STDOUT.winsize
91
-
92
- if time_now.to_f./(0.5).to_i.even?
93
- splitter = ?:.freeze
94
- _caret = caret
95
- else
96
- splitter = ?$.freeze
97
- _caret = ''
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).+(_caret).gradient(tc1, tc2, exclude_spaces: true, bold: bold)
116
- end
117
-
118
- if print_info
119
- info = system_info(width, tc1, tc2, bold)
120
- end
121
-
122
- if print_date
123
- date = time_now.strftime('%a, %d %B %Y').center(width)
124
- .gradient(tc1, tc2, bold: bold)
125
- end
126
-
127
- time = time_now.strftime('%H %M %S %2N').split.join(splitter)
128
- art = Termclock::ParseCharacters.display(time).lines
129
-
130
- art_aligned = art.each_with_index do |x, i|
131
- chomped = x.chomp(''.freeze).+(NEWLINE)
132
- gr = chomped.gradient(*colours[i], bold: bold)
133
- x.replace(SPACE.*(width./(2.0).-(chomped.length / 2.0).abs.to_i + 1) + gr)
134
- end.join
135
-
136
- vertical_gap = "\e[#{height./(2.0).-(art.length / 2.0).to_i + 1}H"
137
-
138
- print "#{CLEAR}#{info}#{vertical_gap}#{art_aligned}\n#{date}\n\n#{message_final}"
139
-
140
- if gc_compact && time_now.to_i > gc_compacted
141
- GC.compact
142
- gc_compacted = time_now.to_i + 7200
143
- end
144
-
145
- sleep(sleep)
146
- end
147
- end
148
- end