tplot 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/tplot/bar_chart.rb +2 -115
- data/lib/tplot/line_chart.rb +5 -21
- data/lib/tplot/version.rb +1 -1
- data/plugins/csv.rb +42 -0
- data/plugins/free.rb +1 -0
- data/plugins/help.rb +7 -2
- data/plugins/list.rb +6 -2
- data/plugins/network.rb +1 -0
- data/plugins/uptime.rb +1 -0
- metadata +3 -2
data/lib/tplot/bar_chart.rb
CHANGED
@@ -1,59 +1,5 @@
|
|
1
1
|
module Tplot
|
2
|
-
class BarChart
|
3
|
-
include Curses
|
4
|
-
|
5
|
-
COLORS = [COLOR_BLUE,
|
6
|
-
COLOR_GREEN,
|
7
|
-
COLOR_RED,
|
8
|
-
COLOR_CYAN,
|
9
|
-
COLOR_MAGENTA,
|
10
|
-
COLOR_YELLOW,
|
11
|
-
COLOR_WHITE]
|
12
|
-
|
13
|
-
attr_accessor :labels
|
14
|
-
attr_accessor :limit
|
15
|
-
|
16
|
-
def initialize
|
17
|
-
@labels = []
|
18
|
-
@datas ||= []
|
19
|
-
@limit = 800
|
20
|
-
end
|
21
|
-
|
22
|
-
def add(values)
|
23
|
-
@datas.push(values)
|
24
|
-
@datas.shift if @datas.size > limit
|
25
|
-
draw
|
26
|
-
end
|
27
|
-
|
28
|
-
def update(values)
|
29
|
-
@datas.pop
|
30
|
-
@datas.push(values)
|
31
|
-
draw
|
32
|
-
end
|
33
|
-
|
34
|
-
def draw
|
35
|
-
pre_draw
|
36
|
-
|
37
|
-
draw_frame_pre
|
38
|
-
draw_line
|
39
|
-
draw_frame_post
|
40
|
-
|
41
|
-
setpos(0, 0)
|
42
|
-
refresh
|
43
|
-
end
|
44
|
-
|
45
|
-
def pre_draw
|
46
|
-
clear
|
47
|
-
start_color
|
48
|
-
init_colors
|
49
|
-
|
50
|
-
@min_value = (@datas.flatten.min < 0) ? @datas.flatten.min : 0
|
51
|
-
@max_value = (@datas.flatten.max < 0) ? 1 : @datas.flatten.max
|
52
|
-
|
53
|
-
@height = lines - 1
|
54
|
-
@width = cols - 1
|
55
|
-
end
|
56
|
-
|
2
|
+
class BarChart < LineChart
|
57
3
|
def draw_line
|
58
4
|
tmp_data = @datas.dup
|
59
5
|
(-@width..0).map(&:abs).each do |col|
|
@@ -72,70 +18,11 @@ module Tplot
|
|
72
18
|
(min..max).each do |line|
|
73
19
|
setpos(line, col)
|
74
20
|
attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
|
75
|
-
addstr("
|
21
|
+
addstr("*")
|
76
22
|
end
|
77
23
|
end
|
78
24
|
end
|
79
25
|
end
|
80
26
|
end
|
81
|
-
|
82
|
-
def print_char(v)
|
83
|
-
if v.even?
|
84
|
-
return "_"
|
85
|
-
else
|
86
|
-
return "-"
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def calc_mapping_value(value)
|
91
|
-
range = (@max_value - @min_value).abs
|
92
|
-
return 0 if range == 0
|
93
|
-
ratio = (value - @min_value).abs.to_f / range.to_f
|
94
|
-
(@height * ratio * 2).to_i
|
95
|
-
end
|
96
|
-
|
97
|
-
def calc_position(value)
|
98
|
-
range = (@max_value - @min_value).abs
|
99
|
-
return 0 if range == 0
|
100
|
-
ratio = (value - @min_value).abs.to_f / range.to_f
|
101
|
-
LOGGER.debug "#{range}:#{ratio}"
|
102
|
-
(@height * (1 - ratio)).to_i
|
103
|
-
end
|
104
|
-
|
105
|
-
def draw_frame_pre
|
106
|
-
setpos(calc_position(@max_value), 0)
|
107
|
-
addstr(print_char(calc_mapping_value(@max_value)) * (@width + 1))
|
108
|
-
|
109
|
-
setpos(calc_position(0), 0)
|
110
|
-
addstr(print_char(calc_mapping_value(0)) * (@width + 1))
|
111
|
-
|
112
|
-
setpos(calc_position(@min_value), 0)
|
113
|
-
addstr(print_char(calc_mapping_value(@min_value)) * (@width + 1))
|
114
|
-
end
|
115
|
-
|
116
|
-
def draw_frame_post
|
117
|
-
setpos(calc_position(@max_value), 0)
|
118
|
-
addstr(@max_value.to_s)
|
119
|
-
|
120
|
-
setpos(calc_position(0), 0)
|
121
|
-
addstr(0.to_s)
|
122
|
-
|
123
|
-
setpos(calc_position(@min_value), 0)
|
124
|
-
addstr(@min_value.to_s)
|
125
|
-
|
126
|
-
labels.each_with_index do |label, idx|
|
127
|
-
setpos(idx + 2, 2)
|
128
|
-
attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
|
129
|
-
addstr(label)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
def init_colors
|
136
|
-
COLORS.each_with_index do |col,idx|
|
137
|
-
init_pair(col, COLOR_BLACK, col)
|
138
|
-
end
|
139
|
-
end
|
140
27
|
end
|
141
28
|
end
|
data/lib/tplot/line_chart.rb
CHANGED
@@ -22,13 +22,11 @@ module Tplot
|
|
22
22
|
def add(values)
|
23
23
|
@datas.push(values)
|
24
24
|
@datas.shift if @datas.size > limit
|
25
|
-
draw
|
26
25
|
end
|
27
26
|
|
28
27
|
def update(values)
|
29
28
|
@datas.pop
|
30
29
|
@datas.push(values)
|
31
|
-
draw
|
32
30
|
end
|
33
31
|
|
34
32
|
def draw
|
@@ -61,43 +59,29 @@ module Tplot
|
|
61
59
|
break unless data
|
62
60
|
data.each_with_index do |value, idx|
|
63
61
|
setpos(calc_position(value), col)
|
64
|
-
v = calc_mapping_value(value)
|
65
62
|
attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
|
66
|
-
addstr(
|
63
|
+
addstr("*")
|
67
64
|
end
|
68
65
|
end
|
69
66
|
end
|
70
67
|
end
|
71
68
|
|
72
|
-
def print_char(v)
|
73
|
-
if v.even?
|
74
|
-
return "_"
|
75
|
-
else
|
76
|
-
return "-"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def calc_mapping_value(value)
|
81
|
-
range = (@max_value - @min_value).abs
|
82
|
-
ratio = (value - @min_value).abs.to_f / range.to_f
|
83
|
-
(@height * ratio * 2).to_i
|
84
|
-
end
|
85
|
-
|
86
69
|
def calc_position(value)
|
87
70
|
range = (@max_value - @min_value).abs
|
71
|
+
return 0 if range == 0
|
88
72
|
ratio = (value - @min_value).abs.to_f / range.to_f
|
89
73
|
(@height * (1 - ratio)).to_i
|
90
74
|
end
|
91
75
|
|
92
76
|
def draw_frame_pre
|
93
77
|
setpos(calc_position(@max_value), 0)
|
94
|
-
addstr(
|
78
|
+
addstr("-" * (@width + 1))
|
95
79
|
|
96
80
|
setpos(calc_position(0), 0)
|
97
|
-
addstr(
|
81
|
+
addstr("-" * (@width + 1))
|
98
82
|
|
99
83
|
setpos(calc_position(@min_value), 0)
|
100
|
-
addstr(
|
84
|
+
addstr("-" * (@width + 1))
|
101
85
|
end
|
102
86
|
|
103
87
|
def draw_frame_post
|
data/lib/tplot/version.rb
CHANGED
data/plugins/csv.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Tplot::Csv < Tplot::Plugin
|
2
|
+
|
3
|
+
def initialize
|
4
|
+
require 'csv'
|
5
|
+
@options = {
|
6
|
+
chart_type: Tplot::BarChart,
|
7
|
+
delay: 1,
|
8
|
+
use_header: false
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def parser
|
13
|
+
OptionParser.new do |opt|
|
14
|
+
opt.banner = "Usage: tplot csv csv_file [options]\n"
|
15
|
+
opt.on('-l', '--line','line chart.') do
|
16
|
+
@options[:chart_type] = Tplot::LineChart
|
17
|
+
end
|
18
|
+
opt.on('-h', '--header','set header first row') do
|
19
|
+
@options[:use_header] = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def execute
|
25
|
+
parser.parse!(ARGV)
|
26
|
+
file_name = ARGV.shift
|
27
|
+
|
28
|
+
chart = @options[:chart_type].new
|
29
|
+
CSV.foreach(file_name) do |csv|
|
30
|
+
if @options[:use_header]
|
31
|
+
chart.labels = ["hoge","moge"]
|
32
|
+
@options[:use_header] = false
|
33
|
+
next
|
34
|
+
end
|
35
|
+
chart.add(csv.map(&:to_f))
|
36
|
+
end
|
37
|
+
chart.draw
|
38
|
+
end
|
39
|
+
|
40
|
+
description "plot csv file"
|
41
|
+
help self.new.parser.to_s
|
42
|
+
end
|
data/plugins/free.rb
CHANGED
data/plugins/help.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
class Tplot::Help < Tplot::Plugin
|
2
2
|
|
3
|
-
description "print plugins help."
|
4
|
-
|
5
3
|
def execute
|
6
4
|
plugin_name = ARGV.shift
|
7
5
|
if File.exists?("#{PLUGIN_DIR}/#{plugin_name}.rb")
|
@@ -12,6 +10,7 @@ class Tplot::Help < Tplot::Plugin
|
|
12
10
|
print_help("#{PLUGIN_DIR}/#{plugin_name}.rb", USER_PLUGIN_DIR)
|
13
11
|
exit 0
|
14
12
|
end
|
13
|
+
puts "#{plugin_name} is not installed."
|
15
14
|
end
|
16
15
|
|
17
16
|
def print_help(plugin_path, dir)
|
@@ -21,4 +20,10 @@ class Tplot::Help < Tplot::Plugin
|
|
21
20
|
return puts plugin_class.help.call if plugin_class.help.is_a? Proc
|
22
21
|
return puts plugin_class.help
|
23
22
|
end
|
23
|
+
|
24
|
+
description "print plugins help."
|
25
|
+
help <<-END
|
26
|
+
Usage: tplot help [plugin_name]
|
27
|
+
print plugin's help.
|
28
|
+
END
|
24
29
|
end
|
data/plugins/list.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
class Tplot::List < Tplot::Plugin
|
2
2
|
|
3
|
-
description "list installed plugins."
|
4
|
-
|
5
3
|
def execute
|
6
4
|
# builtin plugin.
|
7
5
|
puts "-- builtin plugins --"
|
@@ -22,4 +20,10 @@ class Tplot::List < Tplot::Plugin
|
|
22
20
|
plugin_class = Tplot.const_get(name.capitalize)
|
23
21
|
puts "tplot #{name}\t: #{plugin_class.description}"
|
24
22
|
end
|
23
|
+
|
24
|
+
description "list installed plugins."
|
25
|
+
help <<-END
|
26
|
+
Usage: tplot list
|
27
|
+
list installed plugins.
|
28
|
+
END
|
25
29
|
end
|
data/plugins/network.rb
CHANGED
data/plugins/uptime.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tplot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-22 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Tplot is text base graph plot tool.
|
15
15
|
email:
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib/tplot/line_chart.rb
|
32
32
|
- lib/tplot/plugin.rb
|
33
33
|
- lib/tplot/version.rb
|
34
|
+
- plugins/csv.rb
|
34
35
|
- plugins/free.rb
|
35
36
|
- plugins/help.rb
|
36
37
|
- plugins/list.rb
|