libconsole 0.1.0 → 0.3.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: 4fda5f5e1aec52364fd0bc9d9f98ce7543a15a7ec23be96cf6174d9949c5fe19
4
- data.tar.gz: 873e341f3fc2c40026315c348203a37488059ac2b5ad09e59adb2255bfe1360d
3
+ metadata.gz: bf63cde4bc1e976d28f254c98725fba0806407e9edffdbb3412f765050794de4
4
+ data.tar.gz: 35ede5f6aedf9ef6bca5eeab18f80590599b2a21ca8242ef39282ab26cbb8fc5
5
5
  SHA512:
6
- metadata.gz: 925f93707fc3ca887ab5d856dfb42fa74bf40787b5ccd7fcae4e2a5458f771e23cabf86da41ea904a897b2faac7db66fd233db983d55747eafba8db16b60d6c7
7
- data.tar.gz: 87d6c0ea54b1ae8b202d3779aed912991d51cafa60a0299303dbf18b8f2b69071cdb2a7aee206cc366d967c356c672b0a2d4069ea0771b7f02abb2a3c0bd6dd8
6
+ metadata.gz: c3810e38bed70d7de401d265207a68ac852fd4c7730af6fafe5d635c531d76514e18e0e67527e6d022a1d86535a50a0efeccc07e7e52ba1d96d1f05045daf473
7
+ data.tar.gz: 00ef216807f2429735cbbdac5e24cec81d7986a28e48ab809367e12ac14c294b002a3a5019778dbe5077070ccaae031ec206f8cecb3c0dda8ae7bda292bb78c0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- libconsole (0.1.0)
4
+ libconsole (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -19,15 +19,15 @@ If bundler is not being used to manage dependencies, install the gem by executin
19
19
  ```ruby
20
20
  require "libconsole"
21
21
 
22
- class MyClass
22
+ class MyClass < Libconsole::CI::Github::Attr
23
23
  extend Libconsole::CI::Github
24
- include Libconsole::CI::Github::Attr
25
24
  end
26
25
 
27
26
  MyClass.info("info")
28
27
 
29
28
  m = MyClass.new
30
29
  m.add_path("/usr/bin")
30
+ m.export_variable("CC", "gcc")
31
31
  ```
32
32
 
33
33
  ## Development
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libconsole
4
+ module Color
5
+ # https://dev.to/truemark/11-most-asked-questions-about-rubocop-38al
6
+
7
+ # rubocop:disable Layout/HashAlignment
8
+
9
+ # https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
10
+ Escape = {
11
+ uni: "\u001b",
12
+ oct: "\033",
13
+ hex: "\x1B",
14
+ lang: "\e"
15
+ }
16
+
17
+ # https://gist.github.com/iamnewton/8754917
18
+ Format = {
19
+ reset: [0, "E"], bold: [1, "B"],
20
+ dim: [2, "D"], italic: [3, "I"],
21
+ underline: [4, "U"], blink: [5, "N"],
22
+ fastblink: [6, "F"], reverse: [7, "V"],
23
+ hide: [8, "H"], strike: [9, "S"]
24
+ }
25
+
26
+ # https://ss64.com/nt/syntax-ansi.html
27
+ Color = {
28
+ black: [0, "K"], red: [1, "R"],
29
+ green: [2, "G"], yellow: [3, "Y"],
30
+ blue: [4, "L"], magenta: [5, "M"],
31
+ cyan: [6, "C"], white: [7, "W"]
32
+ }
33
+
34
+ Range = {
35
+ "" => 30,
36
+ "bg" => 40,
37
+ "light" => 90,
38
+ "bglight" => 100
39
+ }
40
+
41
+ def _no_empty(argv)
42
+ argv.any? ? argv : nil
43
+ end
44
+
45
+ Escape.each do |std, pre|
46
+ Format.each do |name, pair|
47
+ code = pair[0]
48
+ alias_name = pair[1]
49
+
50
+ fn_name = "#{std}_#{name}"
51
+ define_method(fn_name) do |any = nil|
52
+ if any
53
+ "#{pre}[#{code}m#{any}#{pre}[0m"
54
+ else
55
+ "#{pre}[#{code}m"
56
+ end
57
+ end
58
+
59
+ alias_method alias_name, fn_name
60
+ alias_method name, fn_name if std == :oct
61
+ end
62
+
63
+ Range.each do |n_pre, c_base|
64
+ Color.each do |n_base, pair|
65
+ c_add = pair[0]
66
+ alias_name = pair[1]
67
+
68
+ name = "#{n_pre}#{n_base}"
69
+ code = c_base + c_add
70
+ fn_name = "#{std}_#{name}"
71
+ define_method(fn_name) do |any = nil|
72
+ if any
73
+ "#{pre}[#{code}m#{any}#{pre}[0m"
74
+ else
75
+ "#{pre}[#{code}m"
76
+ end
77
+ end
78
+
79
+ alias_method alias_name, fn_name if n_pre == ""
80
+ alias_method "G#{alias_name}", fn_name if n_pre == "bg"
81
+ alias_method "L#{alias_name}", fn_name if n_pre == "light"
82
+ alias_method "GL#{alias_name}", fn_name if n_pre == "bglight"
83
+
84
+ alias_method name, fn_name if std == :oct
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,9 +1,195 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../color"
4
+
3
5
  module Libconsole
4
6
  module Lang
5
7
  # https://developer.mozilla.org/en-US/docs/Web/API/console
6
8
  module JS
9
+ extend Libconsole::Color
10
+ include Libconsole::Color
11
+
12
+ def default
13
+ @debug = false
14
+
15
+ @count_state = {}
16
+ @time_start_state = {}
17
+
18
+ @indet = ""
19
+ @indet_symbol = " "
20
+ @indet_size = 4
21
+ @indet_count = 0
22
+
23
+ @group_tier = 0
24
+ @group_symbol = blue("|")
25
+ end
26
+
27
+ def group_inner
28
+ @group_tier.positive?
29
+ end
30
+ private :group_inner
31
+
32
+ def pre_puts(argv)
33
+ pre = if group_inner
34
+ @indet + @group_symbol
35
+ else
36
+ @indet
37
+ end
38
+ puts _prefix(pre, argv)
39
+ end
40
+ private :pre_puts
41
+
42
+ # logging
43
+ def log(*argv)
44
+ pre_puts _color_space(argv)
45
+ end
46
+
47
+ def info(*argv)
48
+ argv.unshift("~")
49
+ pre_puts blue(_color_space(argv))
50
+ end
51
+
52
+ def warn(*argv)
53
+ argv.unshift("?")
54
+ pre_puts yellow(_color_space(argv))
55
+ end
56
+
57
+ def error(*argv)
58
+ argv.unshift("!")
59
+ pre_puts red(_color_space(argv))
60
+ end
61
+
62
+ def debug(*argv)
63
+ pre_puts bgred(_color_space(argv)) if @debug
64
+ end
65
+
66
+ def assert(assertion = false, *args)
67
+ args.unshift("!= Assertion failed:")
68
+ pre_puts red(_color_space(args)) unless assertion
69
+ end
70
+
71
+ def clear
72
+ !Gem.win_platform? ? (system "clear") : (system "cls")
73
+ pre_puts italic("Console was cleared")
74
+ end
75
+
76
+ def count(label = "default")
77
+ @count_state[label.to_sym] = 0 unless @count_state[label.to_sym]
78
+ @count_state[label.to_sym] += 1
79
+ pre_puts "#{label}: #{@count_state[label.to_sym]}"
80
+ end
81
+
82
+ def count_reset(label = "default")
83
+ unless @count_state[label.to_sym]
84
+ pre_puts "Count for '#{label}' does not exist"
85
+ return
86
+ end
87
+ @count_state[label.to_sym] = 0
88
+ pre_puts "#{label}: #{@count_state[label.to_sym]}"
89
+ end
90
+
91
+ def dir(*argv)
92
+ pp(argv)
93
+ end
94
+
95
+ def dirxml(*argv)
96
+ pp(argv)
97
+ end
98
+
99
+ def group(label = "console.group")
100
+ @group_tier += 1
101
+ pre_puts bold(label)
102
+ @indet_count += @indet_size
103
+ @indet = @indet_symbol * @indet_count
104
+ end
105
+
106
+ def group_collapsed(label = "console.group")
107
+ @group_tier += 1
108
+ pre_puts bold(label)
109
+ @indet_count += @indet_size
110
+ @indet = @indet_symbol * @indet_count
111
+ end
112
+
113
+ def group_end
114
+ @indet_count -= @indet_size
115
+ @indet_count = 0 if @indet_count.negative?
116
+ @indet = @indet_symbol * @indet_count
117
+ @group_tier -= 1
118
+ @group_tier = 0 if @group_tier.negative?
119
+ end
120
+
121
+ def table(*msg)
122
+ pre_puts msg.join(" ")
123
+ end
124
+
125
+ def time(label = "default")
126
+ if @time_start_state[label.to_sym]
127
+ pre_puts yellow("Timer '#{label}' already exists")
128
+ else
129
+ # https://docs.ruby-lang.org/en/master/Time.html#method-i-tv_usec
130
+ @time_start_state[label.to_sym] = Time.now
131
+ end
132
+ end
133
+
134
+ def time_end(label = "default")
135
+ unless @time_start_state[label.to_sym]
136
+ pre_puts "Timer '#{label}' does not exist"
137
+ return
138
+ end
139
+ now = Time.now
140
+ range = now - @time_start_state[label.to_sym]
141
+ range_ms = format("%.6f", range.to_f * 1000)
142
+ pre_puts "#{label}: #{range_ms} ms - timer ended"
143
+ @time_start_state[label.to_sym] = nil
144
+ end
145
+
146
+ def time_log(label = "default")
147
+ unless @time_start_state[label.to_sym]
148
+ pre_puts "Timer '#{label}' does not exist"
149
+ return
150
+ end
151
+ now = Time.now
152
+ range = now - @time_start_state[label.to_sym]
153
+ range_ms = format("%.6f", range.to_f * 1000)
154
+ pre_puts "#{label}: #{range_ms} ms"
155
+ end
156
+
157
+ def trace(*argv)
158
+ if argv.empty?
159
+ pre_puts "console.trace"
160
+ else
161
+ pre_puts argv.join(" ")
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def _color_space(argv)
168
+ arr = []
169
+ argv.each do |arg|
170
+ if arg.instance_of?(String)
171
+ arr.push arg
172
+ elsif arg.instance_of?(TrueClass)
173
+ arr.push bgblue(arg)
174
+ elsif arg.instance_of?(FalseClass)
175
+ arr.push bgblue("false")
176
+ elsif arg.instance_of?(Integer)
177
+ arr.push bgcyan(arg)
178
+ elsif arg.instance_of?(Float)
179
+ arr.push bgmagenta(arg)
180
+ elsif arg.nil?
181
+ arr.push bgwhite("nil")
182
+ else
183
+ arr.push cyan(arg)
184
+ end
185
+ end
186
+ arr.join(" ")
187
+ end
188
+
189
+ def _prefix(pre, args)
190
+ args unless pre
191
+ %(#{pre}#{args})
192
+ end
7
193
  end
8
194
  end
9
195
  end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "js"
4
+
5
+ class MyClass
6
+ include Libconsole::Lang::JS
7
+ end
8
+
9
+ console = MyClass.new
10
+ console.default
11
+
12
+ url = {
13
+ host: "localhost",
14
+ path: "/"
15
+ }
16
+
17
+ arr = %w[apples oranges bananas]
18
+
19
+ href = {
20
+ url: url,
21
+ query: "l=ruby"
22
+ }
23
+
24
+ comp = {
25
+ query: "l=ruby",
26
+ url: url,
27
+ arr: arr
28
+ }
29
+
30
+ xml = `
31
+ <table id="producttable">
32
+ <thead>
33
+ <tr>
34
+ <td>UPC_Code</td>
35
+ <td>Product_Name</td>
36
+ </tr>
37
+ </thead>
38
+ <tbody>
39
+ <!-- existing data could optionally be included here -->
40
+ </tbody>
41
+ </table>
42
+ `
43
+
44
+ console.log("log")
45
+ console.log("log")
46
+ console.clear
47
+
48
+ console.log("log")
49
+
50
+ console.log
51
+ console.log(true)
52
+ console.log(false)
53
+ console.log(1)
54
+ console.log(1.1)
55
+ console.log(nil)
56
+ console.log("hello", "world")
57
+ console.log("hello", ["world"], "!")
58
+
59
+ console.info("info")
60
+ console.warn("warn")
61
+ console.error("error")
62
+ console.debug("debug")
63
+
64
+ console.assert(false, "assert")
65
+ console.assert(true, "assert")
66
+
67
+ console.count_reset("count")
68
+
69
+ 3.times do
70
+ console.count("count")
71
+ end
72
+ console.count_reset("count")
73
+ console.count("count")
74
+
75
+ console.dir(href)
76
+ console.log(url)
77
+
78
+ console.dirxml(comp)
79
+
80
+ console.group
81
+ console.group("label")
82
+ console.log("a")
83
+ console.group_collapsed("label")
84
+ console.log("a")
85
+ console.log("a")
86
+ console.group_end
87
+ console.group_end
88
+ # console.group_end
89
+
90
+ puts console.red
91
+
92
+ puts "is red?"
93
+
94
+ console.table(arr)
95
+ console.info(arr)
96
+
97
+ console.table(url)
98
+ console.table(href)
99
+ console.table(comp)
100
+
101
+ console.time_end("time")
102
+ console.time("time")
103
+ console.time
104
+ 30.times do
105
+ sleep 0.001
106
+ console.time_log("time")
107
+ end
108
+ console.time_log
109
+ console.time_end("time")
110
+ console.time_log
111
+ console.time_log("time")
112
+ console.time_log("time")
113
+
114
+ def foo(c)
115
+ c.trace("foo")
116
+ def bar(c)
117
+ c.trace
118
+ end
119
+ bar(c)
120
+ end
121
+
122
+ foo(console)
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libconsole
4
+ module Linux
5
+ module Raw
6
+ def default
7
+ @debug = false
8
+ @count_state = {}
9
+ @time_start_state = {}
10
+ end
11
+
12
+ def pre_puts(argv, level = nil)
13
+ pre = if level.instance_of?(String)
14
+ "[#{level.center(6)}] "
15
+ elsif level.instance_of?(0.1.class)
16
+ # https://docs.ruby-lang.org/en/master/format_specifications_rdoc.html
17
+ time = format("%.6f", level)
18
+ t_size = time.size
19
+ size = t_size > 12 ? t_size : 12
20
+ "[#{time.rjust(size)}] "
21
+ end
22
+ str = argv.instance_of?(Array) ? argv.join(": ") : argv
23
+ puts _prefix(pre, str)
24
+ end
25
+ private :pre_puts
26
+
27
+ # dmesg -h
28
+ #
29
+ # Supported log levels (priorities):
30
+ # emerg - system is unusable
31
+ # alert - action must be taken immediately
32
+ # crit - critical conditions
33
+ # err - error conditions
34
+ # warn - warning conditions
35
+ # notice - normal but significant condition
36
+ # info - informational
37
+ # debug - debug-level messages
38
+ def info(*argv)
39
+ pre_puts argv, "info"
40
+ end
41
+
42
+ def notice(*argv)
43
+ pre_puts argv, "noti"
44
+ end
45
+
46
+ def warn(*argv)
47
+ pre_puts argv, "warn"
48
+ end
49
+
50
+ def err(*argv)
51
+ pre_puts argv, "erro"
52
+ end
53
+
54
+ def crit(*argv)
55
+ pre_puts argv, "crit"
56
+ end
57
+
58
+ def alert(*argv)
59
+ pre_puts argv, "aler"
60
+ end
61
+
62
+ def emerg(*argv)
63
+ pre_puts argv, "emer"
64
+ end
65
+
66
+ def debug(*argv)
67
+ pre_puts argv, "debu" if @debug
68
+ end
69
+
70
+ def assert(assertion = false, *args)
71
+ pre_puts args, "asse" unless assertion
72
+ end
73
+
74
+ def clear
75
+ !Gem.win_platform? ? (system "clear") : (system "cls")
76
+ end
77
+
78
+ def count(*argv)
79
+ label = argv.empty? ? "default" : argv.shift
80
+ @count_state[label.to_sym] = 0 unless @count_state[label.to_sym]
81
+ @count_state[label.to_sym] += 1
82
+ pre_puts [label, @count_state[label.to_sym], argv], "coun"
83
+ end
84
+
85
+ def count_reset(*argv)
86
+ label = argv.empty? ? "default" : argv.shift
87
+ unless @count_state[label.to_sym]
88
+ pre_puts "Count for '#{label}' does not exist", "warn"
89
+ return
90
+ end
91
+ @count_state[label.to_sym] = 0
92
+ pre_puts [label, @count_state[label.to_sym], argv], "coun"
93
+ end
94
+
95
+ def time(*argv)
96
+ label = argv.empty? ? "default" : argv.shift
97
+ if @time_start_state[label.to_sym]
98
+ pre_puts "Timer '#{label}' already exists", "warn"
99
+ else
100
+ @time_start_state[label.to_sym] = Time.now
101
+ pre_puts [label, argv], 0.0
102
+ end
103
+ end
104
+
105
+ def time_end(*argv)
106
+ label = argv.empty? ? "default" : argv.shift
107
+ unless @time_start_state[label.to_sym]
108
+ pre_puts "Timer '#{label}' does not exist", "warn"
109
+ return
110
+ end
111
+ now = Time.now
112
+ range = now - @time_start_state[label.to_sym]
113
+ pre_puts [label, "timer ended", argv], range.to_f
114
+ @time_start_state[label.to_sym] = nil
115
+ end
116
+
117
+ def time_log(*argv)
118
+ label = argv.empty? ? "default" : argv.shift
119
+ unless @time_start_state[label.to_sym]
120
+ pre_puts "Timer '#{label}' does not exist", "warn"
121
+ return
122
+ end
123
+ now = Time.now
124
+ range = now - @time_start_state[label.to_sym]
125
+ pre_puts [label, argv], range.to_f
126
+ end
127
+
128
+ private
129
+
130
+ def _prefix(pre, args)
131
+ args unless pre
132
+ %(#{pre}#{args})
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "raw"
4
+
5
+ class MyClass
6
+ include Libconsole::Linux::Raw
7
+ end
8
+
9
+ linux = MyClass.new
10
+ linux.default
11
+
12
+ url = {
13
+ host: "localhost",
14
+ path: "/"
15
+ }
16
+
17
+ arr = %w[apples oranges bananas]
18
+
19
+ href = {
20
+ url: url,
21
+ query: "l=ruby"
22
+ }
23
+
24
+ comp = {
25
+ query: "l=ruby",
26
+ url: url,
27
+ arr: arr
28
+ }
29
+
30
+ xml = `
31
+ <table id="producttable">
32
+ <thead>
33
+ <tr>
34
+ <td>UPC_Code</td>
35
+ <td>Product_Name</td>
36
+ </tr>
37
+ </thead>
38
+ <tbody>
39
+ <!-- existing data could optionally be included here -->
40
+ </tbody>
41
+ </table>
42
+ `
43
+
44
+ linux.info("log")
45
+ linux.info("log")
46
+ linux.clear
47
+
48
+ linux.info("log")
49
+
50
+ linux.info
51
+ linux.info(true)
52
+ linux.info(false)
53
+ linux.info(1)
54
+ linux.info(1.1)
55
+ linux.info(nil)
56
+ linux.info("hello", "world")
57
+ linux.info("hello", ["world"], "!")
58
+
59
+ linux.info("info")
60
+ linux.warn("warn")
61
+ linux.err("err")
62
+ linux.notice("notice")
63
+ linux.crit("crit")
64
+ linux.alert("alert")
65
+ linux.emerg("emerg")
66
+
67
+ linux.assert(false, "assert")
68
+ linux.assert(true, "assert")
69
+
70
+ linux.count_reset("count")
71
+ linux.count
72
+
73
+ 3.times do
74
+ linux.count("count")
75
+ end
76
+ linux.count_reset("count")
77
+ linux.count("count", "bb", "cc")
78
+
79
+ linux.time_end("time")
80
+ linux.time("time")
81
+
82
+ linux.count_reset
83
+ linux.time
84
+ linux.time_log
85
+
86
+ arr = []
87
+
88
+ 20.times do
89
+ linux.time_log("time", arr)
90
+ # fork { linux.count }
91
+ sleep 0.01
92
+ arr.push("a")
93
+ # Process.wait
94
+ end
95
+
96
+ linux.time_end("time")
97
+ linux.time_log
98
+ linux.time_log("time")
99
+ linux.time_log
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libconsole
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/libconsole.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  require_relative "libconsole/version"
4
4
  require_relative "libconsole/ci/github"
5
+ require_relative "libconsole/lang/js"
6
+ require_relative "libconsole/linux/raw"
5
7
 
6
8
  module Libconsole
7
9
  class Error < StandardError; end
@@ -10,4 +12,12 @@ module Libconsole
10
12
  class Github
11
13
  extend Libconsole::CI::Github
12
14
  end
15
+
16
+ class JS
17
+ extend Libconsole::Lang::JS
18
+ end
19
+
20
+ class Raw
21
+ extend Libconsole::Linux::Raw
22
+ end
13
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libconsole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - initdc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-26 00:00:00.000000000 Z
11
+ date: 2022-12-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Console inspired from JavaScript, Github Actions, Linux...
14
14
  email:
@@ -27,7 +27,11 @@ files:
27
27
  - Rakefile
28
28
  - lib/libconsole.rb
29
29
  - lib/libconsole/ci/github.rb
30
+ - lib/libconsole/color.rb
30
31
  - lib/libconsole/lang/js.rb
32
+ - lib/libconsole/lang/js_test.rb
33
+ - lib/libconsole/linux/raw.rb
34
+ - lib/libconsole/linux/raw_test.rb
31
35
  - lib/libconsole/version.rb
32
36
  - libconsole.gemspec
33
37
  - sig/libconsole.rbs