termplot 0.1.0 → 0.3.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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/README.md +134 -58
  4. data/Rakefile +28 -13
  5. data/doc/dash.png +0 -0
  6. data/doc/demo.png +0 -0
  7. data/doc/file.png +0 -0
  8. data/doc/memory.png +0 -0
  9. data/doc/ping.png +0 -0
  10. data/doc/sin.png +0 -0
  11. data/doc/tcp.png +0 -0
  12. data/examples/sample.rb +17 -0
  13. data/lib/termplot/character_map.rb +15 -4
  14. data/lib/termplot/cli.rb +14 -48
  15. data/lib/termplot/colors.rb +36 -29
  16. data/lib/termplot/commands.rb +27 -0
  17. data/lib/termplot/consumers/base_consumer.rb +132 -0
  18. data/lib/termplot/consumers/command_consumer.rb +14 -0
  19. data/lib/termplot/consumers/multi_source_consumer.rb +33 -0
  20. data/lib/termplot/consumers/single_source_consumer.rb +36 -0
  21. data/lib/termplot/consumers/stdin_consumer.rb +11 -0
  22. data/lib/termplot/consumers.rb +12 -0
  23. data/lib/termplot/{cursors/control_chars.rb → control_chars.rb} +0 -0
  24. data/lib/termplot/cursors/buffered_console_cursor.rb +51 -52
  25. data/lib/termplot/cursors/virtual_cursor.rb +64 -58
  26. data/lib/termplot/cursors.rb +7 -0
  27. data/lib/termplot/dsl/panels.rb +80 -0
  28. data/lib/termplot/dsl/widgets.rb +128 -0
  29. data/lib/termplot/file_config.rb +37 -0
  30. data/lib/termplot/message_broker.rb +111 -0
  31. data/lib/termplot/options.rb +211 -0
  32. data/lib/termplot/positioned_widget.rb +8 -0
  33. data/lib/termplot/producer_options.rb +3 -0
  34. data/lib/termplot/producers/base_producer.rb +32 -0
  35. data/lib/termplot/producers/command_producer.rb +42 -0
  36. data/lib/termplot/producers/stdin_producer.rb +11 -0
  37. data/lib/termplot/producers.rb +7 -0
  38. data/lib/termplot/renderable.rb +35 -0
  39. data/lib/termplot/renderer.rb +16 -257
  40. data/lib/termplot/renderers/border_renderer.rb +48 -0
  41. data/lib/termplot/renderers/text_renderer.rb +73 -0
  42. data/lib/termplot/renderers.rb +6 -0
  43. data/lib/termplot/shell.rb +13 -9
  44. data/lib/termplot/utils/ansi_safe_string.rb +68 -0
  45. data/lib/termplot/version.rb +1 -1
  46. data/lib/termplot/widgets/base_widget.rb +79 -0
  47. data/lib/termplot/widgets/border.rb +6 -0
  48. data/lib/termplot/widgets/dataset.rb +50 -0
  49. data/lib/termplot/widgets/histogram_widget.rb +196 -0
  50. data/lib/termplot/widgets/statistics.rb +21 -0
  51. data/lib/termplot/widgets/statistics_widget.rb +104 -0
  52. data/lib/termplot/widgets/time_series_widget.rb +248 -0
  53. data/lib/termplot/widgets.rb +8 -0
  54. data/lib/termplot/window.rb +29 -9
  55. data/termplot.gemspec +1 -6
  56. metadata +46 -30
  57. data/doc/cpu.png +0 -0
  58. data/doc/demo.cast +0 -638
  59. data/doc/demo.gif +0 -0
  60. data/lib/termplot/consumer.rb +0 -71
  61. data/lib/termplot/cursors/console_cursor.rb +0 -56
  62. data/lib/termplot/series.rb +0 -37
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1e3e2ddb262dda39fcc498fb8fed6bd84c1023c97a21d1a2aa2a7bb2ba22c59
4
- data.tar.gz: 87012dbfb94747ecc1d90530d7a199a74cd118a59e4ea8d2396b283519b1d628
3
+ metadata.gz: '068a1b08253b8e5993572546e7b73409b0ff5750a1aaa671d3d6c907b621128f'
4
+ data.tar.gz: a6ce340dac417d5999e7fd6018efd931731ecabcc2d894ff8430a5bd6c9d6bb3
5
5
  SHA512:
6
- metadata.gz: fd4bdb6d311d03c7987fc3efcb0c6773d2abd40af2a13a5e7d9743ad4b6af0fce70ccd10414dded74bad0451f742ad50582cbc3e95813f8dfe1cfec115f322d5
7
- data.tar.gz: bcc75b035ea812d163c5fb4bdbd7d35f387cce9d9d0b38938ce86acdd7005d2effb4f2b865344c7a6ce03c5de835a1a7a41535c444e4017eae9954105f755be2
6
+ metadata.gz: fe102cf42e2e1017ca2f3327c65847dbece92aeeea5de3c05b72e384bf3cfc6eb0dd602c7fa03585282ea747e88c167e8259e724ecbd30b64618c4dbce9ddc53
7
+ data.tar.gz: 13967f3be92508cc0fcc8dcc65eb9f0625ac359e30853ceaeed30e778863a7298eef38b55efe64d615ca61e9e37bcd781247c98b5e64967a46af9208deacf80c
data/.gitignore CHANGED
@@ -8,3 +8,6 @@
8
8
 
9
9
  squiggles.txt
10
10
  Gemfile.lock
11
+ README.md.html
12
+
13
+ .ruby-version
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
1
  # Termplot
2
2
 
3
- Termplot is a simple terminal plotting tool for visualising streaming data.
3
+ [![Gem Version](https://badge.fury.io/rb/termplot.svg)](https://badge.fury.io/rb/termplot)
4
4
 
5
- See the demo on Asciinema:
5
+ Termplot is a simple terminal plotting tool for visualising streaming data.
6
6
 
7
- [![Termplot Demo Asciicast](https://asciinema.org/a/3rzeUSXp2fRjnErX0p3SptP5e.png)](https://asciinema.org/a/3rzeUSXp2fRjnErX0p3SptP5e)
7
+ ![Termplot Demo Image](doc/demo.png)
8
8
 
9
9
  ## Overview
10
10
 
11
- Termplot draws trendlines of data piped into the command from standard input.
12
- It's handy for quickly visualising streaming data in your terminal and works
13
- with any numeric data you can pipe into stdin.
11
+ Termplot draws live plots of data in your terminal. It's handy for quickly
12
+ visualising streaming data and works with any numeric data you can pipe into
13
+ stdin or otherwise obtain by running a shell command.
14
14
 
15
15
  ## Installation
16
16
 
@@ -20,83 +20,158 @@ The tool is a ruby gem and can be installed with:
20
20
 
21
21
  ## Usage
22
22
 
23
- The basic usage is simple:
23
+ There are 3 ways to provide input to termplot:
24
+ - **Stdin:** Pipe data into standard input
25
+ - **Command:** Specify a command to be ran at an interval
26
+ - **Configuration file:** Read a multi-chart configuration from a file
27
+
28
+ ### Pipe data into standard input
29
+ ```
30
+ COMMAND | termplot [OPTIONS]
31
+ ```
32
+
33
+ `COMMAND` is any command that will periodically output numbers to standard out
34
+ delimited by a newline. These will be consumed by termplot and drawn to a chart.
24
35
 
36
+ You can specify the following options (all are optional):
25
37
  ```
26
- {command} | termplot [OPTIONS]
38
+ -r, --rows ROWS Number of rows in the chart window (default: 19)
39
+ -c, --cols COLS Number of cols in the chart window (default: 100)
40
+ --full-screen Render to the full available terminal size
41
+
42
+ --type TYPE The type of chart to render.
43
+ Options are: timeseries (default), stats, hist
44
+
45
+ --timeseries Shorthand for --type timeseries
46
+ --stats Shorthand for --type stats
47
+ --hist Shorthand for --type hist
48
+
49
+ -t, --title TITLE Title of the series (default: 'Series')
50
+ --color COLOR Series color, specified as ansi 16-bit color name:
51
+ (i.e. black, light_black, red, light_red,
52
+ green (default), light_green, yellow,
53
+ light_yellow, blue, light_blue, magenta,
54
+ light_magenta, cyan, light_cyan, white,
55
+ light_white, default)
56
+
57
+ --line-style STYLE Line style.
58
+ Options are: line, heavy-line (default), dot, star, x, bar
27
59
  ```
28
60
 
29
- `{command}` is any command that will periodically output numbers to standard out
30
- delimited by a newline. Options and examples are given below. All command line
31
- options are optional.
61
+ #### Examples
32
62
 
33
- Options:
63
+ ##### Plot a sine wave from standard in:
34
64
 
35
65
  ```
36
- Usage: termplot [OPTIONS]
37
- -r, --rows ROWS Number of rows in the chart window (default: 19)
38
- -c, --cols COLS Number of cols in the chart window (default: 80)
39
- -t, --title TITLE Title of the series (default: Series)
66
+ for i in $(seq 500); do \
67
+ echo $i | awk '{ print sin($0/10) }'; \
68
+ sleep 0.5; \
69
+ done | termplot -t "Sin(x)" --color red
70
+ ```
71
+ ![Sine Wave Plot](doc/sin.png)
40
72
 
41
- --line-style STYLE Line style. Options are:
42
- line [default], heavy-line, dot, star, x
73
+ ##### Plot histogram of network times as reported by `ping`:
74
+ ```
75
+ ping example.com |
76
+ awk '{ split($8,arr,"="); print arr[2]; fflush("/dev/stdout") }' |
77
+ termplot -t "Ping Response Times (ms)" --hist
78
+ ```
79
+ ![Ping Response Times Plot](doc/ping.png)
80
+
81
+ ##### Plot histogram of data from column 33 of a CSV file:
82
+ ```
83
+ cat file.csv | cut -d, -f33 | tail -n +2 |
84
+ termplot -- --hist -r30 -t "Data" --color light_black
85
+ ```
86
+ ![File Data Plot](doc/file.png)
43
87
 
44
- --color COLOR Series color, specified as ansi 16-bit color name:
45
- (i.e. black, red [default], green, yellow, blue,
46
- magenta, cyan, white). Light versions are specified
47
- as light_{color}
48
88
 
49
- -h, --help Display this help message
89
+ ### Run command at an interval
90
+ To run a command at an interval, specify `--command`, and optionally
91
+ `--interval` to termplot:
92
+ ```
93
+ termplot --command 'COMMAND' --interval INTERVAL [OPTIONS]
50
94
  ```
51
95
 
52
- ## Examples
96
+ `COMMAND` will be invoked at the specified interval in milliseconds (default:
97
+ 1000) and its output will be used as the data for the chart. You can specify all
98
+ the same additional options as in stdin mode above.
53
99
 
54
- Sine wave:
100
+ #### Examples
101
+ ##### Plot number of TCP connections over time:
55
102
 
56
103
  ```
57
- for i in $(seq 500); do \
58
- echo $i | awk '{ print sin($0/10) }'; \
59
- sleep 0.5; \
60
- done | termplot -t "Sin(x)"
104
+ termplot --command 'ss -s | head -n1 | cut -d" " -f2' \
105
+ --interval 500 -t "TCP Connections"
61
106
  ```
62
- ![Sine Wave Plot](http://localhost:8080/doc/sin.png)
107
+ ![TCP Connections Plot](doc/tcp.png)
108
+
109
+ ##### Plot memory usage of process with PID 4396:
63
110
 
64
- Total % memory usage:
65
111
  ```
66
- while true; do \
67
- free | awk 'NR==2 { print ($3/$2) * 100 }'; \
68
- sleep 0.5; \
69
- done | termplot -t "Memory (% used)" --color light_magenta --line-style heavy-line
112
+ termplot --command "ps -q 4396 -o rss= | awk '{ print (\$0/1024.0) }'" \
113
+ -t "Process 4396 Memory (MB)" --color light_magenta
114
+ ```
115
+ ![Process Memory Plot](doc/memory.png)
116
+
117
+ ### Specify a multi-chart configuration
118
+ You can configure termplot to set up a multi-chart dashboard using a ruby
119
+ configuration file:
120
+
121
+ ```
122
+ termplot [--file/-f] FILE
70
123
  ```
71
- ![Free Memory % Chart](http://localhost:8080/doc/memory.png)
72
124
 
73
- % CPU usage of a "puma" process:
125
+ The configuration file uses a simple ruby DSL to arrange charts into rows and
126
+ columns. Each chart will take a command and optionally an interval as well as
127
+ formatting options. Termplot will then run the command at the specified interval
128
+ and render the resulting data into the charts.
129
+
130
+ #### Example
131
+
132
+ File (sample.rb):
133
+ ```ruby
134
+ col do
135
+ row do
136
+ cpu_command =
137
+ "top -b -n 1 | awk -F',' 'NR==3{ split($4, arr, \" \"); print 100.0 - arr[1] }'"
138
+
139
+ histogram title: "CPU (%)", command: cpu_command, color: "light_cyan"
140
+ timeseries title: "CPU (%)", command: cpu_command, color: "light_cyan"
141
+ statistics title: "CPU (%)", command: cpu_command
142
+ end
143
+
144
+ row do
145
+ memory_command = "free | awk 'NR==2 { print ($3/$2) * 100 }'"
146
+
147
+ histogram title: "Memory (%)", command: memory_command, color: "light_magenta"
148
+ timeseries title: "Memory (%)", command: memory_command, color: "light_magenta"
149
+ statistics title: "Memory (%)", command: memory_command
150
+ end
151
+ end
74
152
  ```
75
- while true; do \
76
- ps au | grep puma | awk 'NR==1{ print $3 }'; \
77
- sleep 0.5; \
78
- done | termplot -t "Ruby CPU(%)" --color yellow --line-style dot -r10 -c 120
153
+ Run it with:
79
154
  ```
80
- ![CPU % Chart](http://localhost:8080/doc/cpu.png)
155
+ termplot -f sample.rb --cols 150 --rows 20
156
+ ```
157
+
158
+ Result:
159
+ ![CPU_Ram_dashboard](doc/dash.png)
81
160
 
82
161
 
83
162
  ## Notes
84
163
 
85
- - The command should work just fine if you have a monospaced unicode font.
164
+ - Termplot should work just fine if you have a monospaced unicode font.
86
165
  Tested on linux, and should work on MacOS too. Not too sure about windows.
87
- - The samples received are plotted in sequence order, and there is no notion of
88
- temporal spacing. So even if the time between samples is inconsistent, they
89
- will be plotted with the same amount of space between them.
90
- - The `while true; do {...}; sleep INTERVAL` is pretty typical, I would like to
91
- sometime soon implement a `--command "{...}" --interval INTERVAL` which would
92
- reduce some of the boilerplate of calling some command at an interval and make
93
- it easy to watch and plot.
166
+ - On timeseries plots, samples received are plotted in sequence order, and there
167
+ is no notion of temporal spacing. So even if the time between samples is
168
+ inconsistent, they will be plotted with the same amount of space between them.
94
169
 
95
- ## Background
170
+ ## Background
96
171
 
97
- I recently needed to be able to monitor the number of open TCP connections on my
98
- linux machine. I could get the data I needed from `ss` with some text processing
99
- but I didn't have a quick and easy way to eyeball the overall trend.
172
+ A while back I needed to be able to monitor the number of open TCP connections
173
+ on my linux machine. I could get the data I needed from `ss` with some text
174
+ processing but I didn't have a quick and easy way to eyeball the overall trend.
100
175
 
101
176
  I mainly work with ruby, so taking
102
177
  inspiration from fantastic libraries like [Unicode Plots
@@ -107,12 +182,13 @@ ruby.
107
182
  Now with termplot, it's as easy as:
108
183
 
109
184
  ```
110
- while true; do \
111
- ss -s | head -n1 | cut -d ' ' -f2; sleep 1; \
112
- done | termplot -t "TCP Connections"
185
+ termplot --command 'ss -s | head -n1 | cut -d" " -f2' --interval 500 -t "TCP Connections"
113
186
  ```
114
187
 
115
- ![TCP Connections](http://localhost:8080/doc/tcp.png)
188
+ ![TCP Connections](doc/tcp.png)
189
+
190
+ In the end termplot turned out to be useful for all sorts of command line
191
+ visualisation tasks.
116
192
 
117
193
  ## Development
118
194
 
data/Rakefile CHANGED
@@ -1,18 +1,33 @@
1
+ require_relative "./lib/termplot/commands"
1
2
  require "bundler/gem_tasks"
2
3
 
3
- task :bin do
4
- Bundler.require(:default)
5
- exec "bin/termplot", *ARGV[2..-1]
6
- end
4
+ extend Termplot::Commands
5
+ extend Termplot::StdinCommands
7
6
 
8
- task :sin_test do
9
- cmd = <<-CMD
10
- for i in $(seq 5000);
11
- do
12
- echo $i | awk '{ print sin($0/10)* 10; fflush("/dev/stdout") }';
13
- sleep 0.1;
14
- done | ruby -Ilib bin/termplot -- -t 'sin(x)'
15
- CMD
16
- exec cmd
7
+ SAMPLE_FILES_PATH = "examples"
8
+ def termplot_binary
9
+ "ruby -Ilib bin/termplot"
17
10
  end
18
11
 
12
+ namespace :test do
13
+ task :bin do
14
+ exec "ruby", "-Ilib", "bin/termplot", *ARGV[2..-1]
15
+ end
16
+
17
+ task :file do
18
+ cmd = %( #{termplot_binary} -f #{File.join(SAMPLE_FILES_PATH, ARGV[2])} #{ARGV[3..-1].join(" ")})
19
+ exec cmd
20
+ end
21
+
22
+ namespace :timeseries do
23
+ task :sin do
24
+ cmd = "#{sin(500)} | #{termplot_binary} -t 'sin(x)'"
25
+ exec cmd
26
+ end
27
+
28
+ task :random do
29
+ cmd = %( #{termplot_binary} --command '#{random}' --interval 900% )
30
+ exec cmd
31
+ end
32
+ end
33
+ end
data/doc/dash.png ADDED
Binary file
data/doc/demo.png ADDED
Binary file
data/doc/file.png ADDED
Binary file
data/doc/memory.png CHANGED
Binary file
data/doc/ping.png ADDED
Binary file
data/doc/sin.png CHANGED
Binary file
data/doc/tcp.png CHANGED
Binary file
@@ -0,0 +1,17 @@
1
+ col do
2
+ row do
3
+ cpu_command = "top -b -n 1 | awk -F',' 'NR==3{ split($4, arr, \" \"); print 100.0 - arr[1] }'"
4
+
5
+ histogram title: "CPU (%)", command: cpu_command, color: "light_cyan"
6
+ timeseries title: "CPU (%)", command: cpu_command, color: "light_cyan"
7
+ statistics title: "CPU (%)", command: cpu_command
8
+ end
9
+
10
+ row do
11
+ memory_command = "free | awk 'NR==2 { print ($3/$2) * 100 }'"
12
+
13
+ histogram title: "Memory (%)", command: memory_command, color: "light_magenta"
14
+ timeseries title: "Memory (%)", command: memory_command, color: "light_magenta"
15
+ statistics title: "Memory (%)", command: memory_command
16
+ end
17
+ end
@@ -12,7 +12,9 @@ module Termplot
12
12
  top_left: "┌",
13
13
  bot_right: "┘",
14
14
  tick_right: "┤",
15
- extended: true
15
+ tick_left: "├",
16
+ extended: true,
17
+ filled: false
16
18
  }
17
19
  DEFAULT = LINE
18
20
 
@@ -26,13 +28,15 @@ module Termplot
26
28
  top_right: "┓",
27
29
  top_left: "┏",
28
30
  bot_right: "┛",
29
- tick_right: "┫"
31
+ tick_right: "┫",
32
+ tick_left: "┣",
30
33
  )
31
34
 
32
35
  BASIC = {
33
36
  empty: " ",
34
37
  point: "•",
35
- extended: false
38
+ extended: false,
39
+ filled: false
36
40
  }
37
41
  DOTS = BASIC
38
42
 
@@ -44,12 +48,19 @@ module Termplot
44
48
  point: "*"
45
49
  )
46
50
 
51
+ BAR = LINE.merge(
52
+ point: "▄",
53
+ extended: false,
54
+ filled: true
55
+ )
56
+
47
57
  LINE_STYLES = {
48
58
  "line" => LINE,
49
59
  "heavy-line" => HEAVY_LINE,
50
60
  "dot" => DOTS,
51
61
  "star" => STAR,
52
- "x" => X
62
+ "x" => X,
63
+ "bar" => BAR,
53
64
  }
54
65
  end
55
66
  end
data/lib/termplot/cli.rb CHANGED
@@ -1,59 +1,25 @@
1
- require "optparse"
2
- require "termplot/consumer"
1
+ # frozen_string_literal: true
2
+ require "termplot/options"
3
+ require "termplot/consumers"
3
4
 
4
5
  module Termplot
5
6
  class CLI
6
7
  def self.run
7
- opts = self.parse_options
8
- Consumer.new(**opts).run
8
+ options = Termplot::Options.new
9
+ options.parse_options!
10
+ run_consumer(options)
9
11
  end
10
12
 
11
13
  private
12
- def self.parse_options
13
- options = {
14
- rows: 19,
15
- cols: 80,
16
- title: "Series",
17
- line_style: "line",
18
- color: "red",
19
- debug: false
20
- }
21
- OptionParser.new do |opts|
22
- opts.banner = "Usage: termplot [OPTIONS]"
23
14
 
24
- opts.on("-rROWS", "--rows ROWS", "Number of rows in the chart window (default: 19)") do |v|
25
- options[:rows] = v.to_i
26
- end
27
-
28
- opts.on("-cCOLS", "--cols COLS", "Number of cols in the chart window (default: 80)") do |v|
29
- options[:cols] = v.to_i
30
- end
31
-
32
- opts.on("-tTITLE", "--title TITLE", "Title of the series (default: Series)") do |v|
33
- options[:title] = v
34
- end
35
-
36
- opts.on("--line-style STYLE", "Line style. Options are: line [default], heavy-line, dot, star, x") do |v|
37
- options[:line_style] = v.downcase
38
- end
39
-
40
- opts.on("--color COLOR", "Series color, specified as ansi 16-bit color name",
41
- "(i.e. black, red [default], green, yellow, blue, magenta, cyan, white)",
42
- "with light versions specified as light_{color}") do |v|
43
- options[:color] = v.downcase
44
- end
45
-
46
- opts.on("-d", "--debug", "Enable debug mode, Logs window data to stdout instead of rendering") do |v|
47
- options[:debug] = v
48
- end
49
-
50
- opts.on("-h", "--help", "Display this help message") do
51
- puts opts
52
- exit(0)
53
- end
54
-
55
- end.parse!
56
- options
15
+ CONSUMERS = {
16
+ file: "Termplot::Consumers::MultiSourceConsumer",
17
+ command: "Termplot::Consumers::CommandConsumer",
18
+ stdin: "Termplot::Consumers::StdinConsumer",
19
+ }
20
+ def self.run_consumer(options)
21
+ consumer = Object.const_get(CONSUMERS[options.input_mode])
22
+ consumer.new(options).run
57
23
  end
58
24
  end
59
25
  end
@@ -1,40 +1,43 @@
1
1
  module Termplot
2
2
  class Colors
3
- class << self
4
- COLORS = {
5
- black: 0,
6
- light_black: 60,
7
- red: 1,
8
- light_red: 61,
9
- green: 2,
10
- light_green: 62,
11
- yellow: 3,
12
- light_yellow: 63,
13
- blue: 4,
14
- light_blue: 64,
15
- magenta: 5,
16
- light_magenta: 65,
17
- cyan: 6,
18
- light_cyan: 66,
19
- white: 7,
20
- light_white: 67,
21
- default: 9
22
- }
3
+ COLORS = {
4
+ black: 0,
5
+ light_black: 60,
6
+ red: 1,
7
+ light_red: 61,
8
+ green: 2,
9
+ light_green: 62,
10
+ yellow: 3,
11
+ light_yellow: 63,
12
+ blue: 4,
13
+ light_blue: 64,
14
+ magenta: 5,
15
+ light_magenta: 65,
16
+ cyan: 6,
17
+ light_cyan: 66,
18
+ white: 7,
19
+ light_white: 67,
20
+ default: 9
21
+ }
23
22
 
24
- MODES = {
25
- default: 0,
26
- bold: 1,
27
- italic: 3,
28
- underline: 4,
29
- blink: 5,
30
- swap: 7,
31
- hide: 8
32
- }
23
+ MODES = {
24
+ default: 0,
25
+ bold: 1,
26
+ italic: 3,
27
+ underline: 4,
28
+ blink: 5,
29
+ swap: 7,
30
+ hide: 8
31
+ }
33
32
 
33
+ class << self
34
34
  COLORS.each do |(color, code)|
35
35
  define_method(color) do |str|
36
36
  escape_color(color) + str + escape_mode(:default)
37
37
  end
38
+ define_method("#{color}_bg") do |str|
39
+ escape_bg_color(color) + str + escape_mode(:default)
40
+ end
38
41
  end
39
42
 
40
43
  def fetch(color, default)
@@ -47,6 +50,10 @@ module Termplot
47
50
  "\e[#{COLORS[color] + 30}m"
48
51
  end
49
52
 
53
+ def escape_bg_color(color)
54
+ "\e[#{COLORS[color] + 40}m"
55
+ end
56
+
50
57
  def escape_mode(mode)
51
58
  "\e[#{MODES[mode]}m"
52
59
  end
@@ -0,0 +1,27 @@
1
+ module Termplot
2
+ module Commands
3
+ def random
4
+ "echo $RANDOM"
5
+ end
6
+
7
+ def memory
8
+ "free | awk 'NR==2 { print ($3/$2) * 100 }'"
9
+ end
10
+
11
+ def cpu
12
+ "top -b -n 1 | awk -F',' 'NR==3{ split($4, arr, \" \"); print 100.0 - arr[1] }'"
13
+ end
14
+ end
15
+
16
+ module StdinCommands
17
+ def sin(n)
18
+ <<-CMD.chomp
19
+ for i in $(seq #{n});
20
+ do
21
+ echo $i | awk '{ print sin($0/10)* 10; fflush("/dev/stdout") }';
22
+ sleep 0.1;
23
+ done
24
+ CMD
25
+ end
26
+ end
27
+ end