tplot 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *~
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tplot.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mitoma Ryo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Tplot
2
+
3
+ Tplot is text base graph plot tool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tplot'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tplot
18
+
19
+ ## Usage
20
+
21
+ List useful plugin list
22
+
23
+ $ tplot list
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/tplot ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # inital setting
4
+ trap(:INT) do
5
+ exit 0
6
+ end
7
+
8
+ BASE_DIR = File.expand_path(__FILE__.gsub(%r{/[^/]+\z},'') + "/../")
9
+ plugin_name = ARGV.first.nil? ? "list" : ARGV.first
10
+
11
+ # debug setting
12
+ require 'logger'
13
+ if ENV['DEBUG'] == 'true'
14
+ LOGGER = Logger.new("#{BASE_DIR}/log/debug.log", "daily")
15
+ LOGGER.level = Logger::DEBUG
16
+ else
17
+ LOGGER = Logger.new("/dev/null")
18
+ end
19
+
20
+ # plugin check
21
+ unless File.exists?("#{BASE_DIR}/plugins/#{plugin_name}.rb")
22
+ puts "#{plugin_name} plugin is not found."
23
+ puts
24
+ plugin_name = "list"
25
+ end
26
+
27
+ # load basic module
28
+ require 'curses'
29
+ require "#{BASE_DIR}/lib/tplot"
30
+ require "#{BASE_DIR}/lib/tplot/plugin"
31
+ require "#{BASE_DIR}/lib/tplot/line_chart"
32
+ require "#{BASE_DIR}/lib/tplot/bar_chart"
33
+
34
+ # execute plugin
35
+ require "#{BASE_DIR}/plugins/#{plugin_name}"
36
+ plugin_class = Tplot.const_get(plugin_name.capitalize)
37
+ plugin_class.new(ARGV.shift).execute
data/lib/tplot.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "tplot/version"
2
+
3
+ module Tplot
4
+ # noop
5
+ end
@@ -0,0 +1,138 @@
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
+
57
+ def draw_line
58
+ tmp_data = @datas.dup
59
+ (-@width..0).map(&:abs).each do |col|
60
+ data = tmp_data.pop
61
+ break unless data
62
+
63
+ data_with_idx = []
64
+ data.each_with_index do |value, idx|
65
+ data_with_idx.push [value, idx]
66
+ end
67
+ data_with_idx.sort{|l, r| r.first.abs <=> l.first.abs }.each do |array|
68
+ value, idx = array.first, array.last
69
+ min = calc_position(0)
70
+ max = calc_position(value)
71
+ min, max = max, min if min > max
72
+ (min..max).each do |line|
73
+ setpos(line, col)
74
+ attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
75
+ addstr(" ")
76
+ end
77
+ end
78
+ end
79
+ end
80
+ 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
+ ratio = (value - @min_value).abs.to_f / range.to_f
93
+ (@height * ratio * 2).to_i
94
+ end
95
+
96
+ def calc_position(value)
97
+ range = (@max_value - @min_value).abs
98
+ ratio = (value - @min_value).abs.to_f / range.to_f
99
+ (@height * (1 - ratio)).to_i
100
+ end
101
+
102
+ def draw_frame_pre
103
+ setpos(calc_position(@max_value), 0)
104
+ addstr(print_char(calc_mapping_value(@max_value)) * (@width + 1))
105
+
106
+ setpos(calc_position(0), 0)
107
+ addstr(print_char(calc_mapping_value(0)) * (@width + 1))
108
+
109
+ setpos(calc_position(@min_value), 0)
110
+ addstr(print_char(calc_mapping_value(@min_value)) * (@width + 1))
111
+ end
112
+
113
+ def draw_frame_post
114
+ setpos(calc_position(@max_value), 0)
115
+ addstr(@max_value.to_s)
116
+
117
+ setpos(calc_position(0), 0)
118
+ addstr(0.to_s)
119
+
120
+ setpos(calc_position(@min_value), 0)
121
+ addstr(@min_value.to_s)
122
+
123
+ labels.each_with_index do |label, idx|
124
+ setpos(idx + 2, 2)
125
+ attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
126
+ addstr(label)
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ def init_colors
133
+ COLORS.each_with_index do |col,idx|
134
+ init_pair(col, COLOR_BLACK, col)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,117 @@
1
+ module Tplot
2
+ class Chart
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
+ plot
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
+
57
+ def plot
58
+ # noop
59
+ end
60
+
61
+ def draw_frame_pre
62
+ setpos(calc_position(@max_value), 0)
63
+ addstr(print_char(calc_mapping_value(@max_value)) * (@width + 1))
64
+
65
+ setpos(calc_position(0), 0)
66
+ addstr(print_char(calc_mapping_value(0)) * (@width + 1))
67
+
68
+ setpos(calc_position(@min_value), 0)
69
+ addstr(print_char(calc_mapping_value(@min_value)) * (@width + 1))
70
+ end
71
+
72
+ def draw_frame_post
73
+ setpos(calc_position(@max_value), 0)
74
+ addstr(@max_value.to_s)
75
+
76
+ setpos(calc_position(0), 0)
77
+ addstr(0.to_s)
78
+
79
+ setpos(calc_position(@min_value), 0)
80
+ addstr(@min_value.to_s)
81
+
82
+ labels.each_with_index do |label, idx|
83
+ setpos(idx + 2, 2)
84
+ attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
85
+ addstr(label)
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ def print_char(v)
92
+ if v.even?
93
+ return "_"
94
+ else
95
+ return "-"
96
+ end
97
+ end
98
+
99
+ def calc_mapping_value(value)
100
+ range = (@max_value - @min_value).abs
101
+ ratio = (value - @min_value).abs.to_f / range.to_f
102
+ (@height * ratio * 2).to_i
103
+ end
104
+
105
+ def calc_position(value)
106
+ range = (@max_value - @min_value).abs
107
+ ratio = (value - @min_value).abs.to_f / range.to_f
108
+ (@height * (1 - ratio)).to_i
109
+ end
110
+
111
+ def init_colors
112
+ COLORS.each_with_index do |col,idx|
113
+ init_pair(col, col, COLOR_BLACK)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,128 @@
1
+ module Tplot
2
+ class LineChart
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
+
57
+ def draw_line
58
+ tmp_data = @datas.dup
59
+ (-@width..0).map(&:abs).each do |col|
60
+ data = tmp_data.pop
61
+ break unless data
62
+ data.each_with_index do |value, idx|
63
+ setpos(calc_position(value), col)
64
+ v = calc_mapping_value(value)
65
+ attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
66
+ addstr(print_char(v))
67
+ end
68
+ end
69
+ end
70
+ end
71
+
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
+ def calc_position(value)
87
+ range = (@max_value - @min_value).abs
88
+ ratio = (value - @min_value).abs.to_f / range.to_f
89
+ (@height * (1 - ratio)).to_i
90
+ end
91
+
92
+ def draw_frame_pre
93
+ setpos(calc_position(@max_value), 0)
94
+ addstr(print_char(calc_mapping_value(@max_value)) * (@width + 1))
95
+
96
+ setpos(calc_position(0), 0)
97
+ addstr(print_char(calc_mapping_value(0)) * (@width + 1))
98
+
99
+ setpos(calc_position(@min_value), 0)
100
+ addstr(print_char(calc_mapping_value(@min_value)) * (@width + 1))
101
+ end
102
+
103
+ def draw_frame_post
104
+ setpos(calc_position(@max_value), 0)
105
+ addstr(@max_value.to_s)
106
+
107
+ setpos(calc_position(0), 0)
108
+ addstr(0.to_s)
109
+
110
+ setpos(calc_position(@min_value), 0)
111
+ addstr(@min_value.to_s)
112
+
113
+ labels.each_with_index do |label, idx|
114
+ setpos(idx + 2, 2)
115
+ attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do
116
+ addstr(label)
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ def init_colors
123
+ COLORS.each_with_index do |col,idx|
124
+ init_pair(col, col, COLOR_BLACK)
125
+ end
126
+ end
127
+ end
128
+ end