bash-visual 1.0.5 → 1.0.6

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 CHANGED
@@ -1,3 +1,5 @@
1
1
  *~
2
2
  .gitignore
3
3
  .idea
4
+ Gemfile.lock
5
+ .coverage
data/Gemfile CHANGED
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in bash-visual.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'test-unit'
8
+ gem 'mocha', :group => :test, :require=>false
9
+ gem 'minitest-reporters'
10
+
11
+ gem 'simplecov', :require => false, :group => :test
12
+ end
data/README.md CHANGED
@@ -1,6 +1,17 @@
1
- # Bash::Visual
1
+ # Bash-Visual
2
2
 
3
- Bash visualisation tools
3
+ Bash visualisation library
4
+
5
+ ## Description
6
+ **BashVisual** simplifies Unix-console operation (bash, csh etc). It uses _tput_ and standard Unix-terminal facilities. It is also thread-safe.
7
+
8
+ BashVisual allows:
9
+ - control position, colors and text's form
10
+ - draw graphical primitives (rectangle, window)
11
+ - create dynamic scrolling objects
12
+ - get window parameters (weight, height)
13
+
14
+ [See the wiki!](https://github.com/AlmazKo/BashVisual/wiki)
4
15
 
5
16
  ## Installation
6
17
 
@@ -17,14 +28,20 @@ Or install it yourself as:
17
28
  $ gem install bash-visual
18
29
 
19
30
  ## Usage
31
+ ```ruby
32
+ require 'bash-visual'
33
+ include Bash_Visual
34
+
35
+ border_font = Font.new(:std, :blue)
36
+ main_color = Font.new(:bold, :green)
37
+
38
+ console = Console.new(main_color)
39
+ console.clear
20
40
 
21
- include Bash_Visual;
22
- font = Font.new(Font::STD, Font::LIGHT_BLUE)
23
- console = Console.new(font)
24
- console.clear()
25
- console.draw_window(2, 2, 20, 5, 'Example', font, Console::BORDER_UTF_DOUBLE)
26
- console.write_to_position(5, 3, 'Hello World!', Font.new(Font::BOLD, Font::LIGHT_GREEN))
27
- console.position = [0, 8]
41
+ console.draw_window(2, 2, 20, 5, 'Example', border_font, Console::BORDER_UTF_DOUBLE)
42
+ console.write_to_position(5, 3, 'Hello World!')
43
+ console.position = [0, 8]
44
+ ```
28
45
 
29
46
  ## Contributing
30
47
 
data/bash-visual.gemspec CHANGED
@@ -1,16 +1,18 @@
1
- # -*- encoding: utf-8 -*-
1
+ # coding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'bash-visual/version'
2
4
 
3
- Dir[File.dirname(__FILE__) + '/lib/bash-visual/*.rb'].each {|file| require file }
4
5
 
5
6
  Gem::Specification.new do |gem|
6
7
  gem.name = 'bash-visual'
7
8
  gem.author = 'Alexander Suslov'
8
9
  gem.email = ['a.s.suslov@gmail.com']
9
10
  gem.description = %q{Bash visualisation tools}
10
- gem.summary = %q{Bash visualisation tools}
11
- gem.homepage = 'https://github.com/AlmazKo/BashConsole'
11
+ gem.summary = gem.description
12
+ gem.homepage = 'https://github.com/AlmazKo/BashVisual'
12
13
 
13
14
  gem.files = `git ls-files`.split($\)
15
+ gem.test_files = `git ls-files -- {test}/*`.split($\)
14
16
  gem.require_paths = ['lib']
15
17
  gem.version = Bash_Visual::VERSION
16
18
  gem.license = 'MIT'
@@ -0,0 +1,14 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bash-visual'
4
+ include Bash_Visual
5
+
6
+ border_font = Font.new(:std, :blue)
7
+ main_color = Font.new(:bold, :green)
8
+
9
+ console = Console.new(main_color)
10
+ console.clear
11
+
12
+ console.draw_window(2, 2, 20, 5, 'Example', border_font, Console::BORDER_UTF_DOUBLE)
13
+ console.write_to_position(5, 3, 'Hello World!')
14
+ console.position = [0, 8]
data/example/random.rb ADDED
@@ -0,0 +1,9 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+ require 'bash-visual'
3
+ include Bash_Visual
4
+
5
+ console = Console.new
6
+
7
+ 'Font random demonstration'.chars do |char|
8
+ console.write char, Font.new(:std, Font::rand_color(:black))
9
+ end
@@ -0,0 +1,30 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bash-visual'
4
+ include Bash_Visual
5
+
6
+ colors = Font::COLORS
7
+ types = [:std, :bold, :underline, :blink,
8
+ [:bold, :underline], [:bold, :blink], [:underline, :blink],
9
+ [:bold, :underline, :blink]
10
+ ]
11
+
12
+ console = Console.new
13
+ console.clear
14
+
15
+ console.write_ln 'Example "Spectrum":'
16
+
17
+ types.each do |type|
18
+
19
+ colors.each do |color|
20
+ console.write('X', Font.new(type, color))
21
+ end
22
+
23
+ console.write ' '
24
+
25
+ colors.each do |color|
26
+ console.write('X', Font.new(type, :white, color))
27
+ end
28
+
29
+ console.write_ln ' ' + type.inspect
30
+ end
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module Bash_Visual
2
4
  class Builder
3
5
 
@@ -21,7 +23,7 @@ module Bash_Visual
21
23
  "\e[s"
22
24
  end
23
25
 
24
- def set_position (x, y)
26
+ def set_position(x, y)
25
27
  "\e[#{y.to_i};#{x.to_i}H"
26
28
  end
27
29
 
@@ -40,7 +42,7 @@ module Bash_Visual
40
42
  bash
41
43
  end
42
44
 
43
- def write_to_position (x, y, text, font = @default_font)
45
+ def write_to_position(x, y, text, font = @default_font)
44
46
  bash = ''
45
47
  bash << save_position()
46
48
  bash << move_position(x, y)
@@ -49,12 +51,12 @@ module Bash_Visual
49
51
  bash
50
52
  end
51
53
 
52
- def write (text, font = @default_font)
54
+ def write(text, font = @default_font)
53
55
  font.to_bash + text + Font::RESET
54
56
  end
55
57
 
56
- def write_ln (text, font = @default_font)
57
- font.to_bash + text << "\n" << Font::RESET
58
+ def write_ln(text, font = @default_font)
59
+ font.to_bash + text.to_s + $/ + Font::RESET
58
60
  end
59
61
  end
60
62
  end
@@ -23,7 +23,7 @@ module Bash_Visual
23
23
  def initialize (font = Font.new, way_outnput = OUTPUT_WITHOUT_BLOCK, builder = Builder.new)
24
24
  @current_x = 0
25
25
  @current_y = 0
26
- @font = font
26
+ @type = font
27
27
  @way_output = way_outnput
28
28
  @builder = builder
29
29
  end
@@ -42,15 +42,20 @@ module Bash_Visual
42
42
  # Записать что-то в определенной позиции, а потом вернуться на текущую
43
43
  # Если необходимо сохранить позицию после записи - используйте связку
44
44
  # move_position/position= и write
45
- def write_to_position (x, y, text, font = @font)
45
+ def write_to_position (x, y, text, font = @type)
46
46
  print @builder.write_to_position(x, y, text, font)
47
47
  end
48
48
 
49
- def write (text, font = @font)
49
+ def write (text, font = @type)
50
50
  print @builder.write(text, font)
51
51
  end
52
52
 
53
- def write_ln (text, font = @font)
53
+ def write_ln (text = nil, font = @type)
54
+ if text.nil?
55
+ print $/
56
+ return
57
+ end
58
+
54
59
  print @builder.write_ln(text, font)
55
60
  end
56
61
 
@@ -121,7 +126,15 @@ module Bash_Visual
121
126
  end
122
127
 
123
128
  def font= font
124
- @font = font
129
+ @type = font
130
+ end
131
+
132
+ def lines
133
+ `tput lines`
134
+ end
135
+
136
+ def cols
137
+ `tput cols`
125
138
  end
126
139
 
127
140
  private
@@ -1,92 +1,122 @@
1
1
  module Bash_Visual
2
2
  class Font
3
- BLACK = 0
4
- RED = 1
5
- GREEN = 2
6
- BROWN = 3
7
- BLUE = 4
8
- MAGENTA = 5
9
- CYAN = 6
10
- LIGHT_GRAY = 7
11
-
12
- GRAY = 10
13
- LIGHT_RED = 11
14
- LIGHT_GREEN = 12
15
- YELLOW = 13
16
- LIGHT_BLUE = 14
17
- LIGHT_MAGENTA = 15
18
- LIGHT_CYAN = 16
19
- WHITE = 17
20
-
21
- RESET = "\e[0m"
22
-
23
- #types font
24
- STD = 0
25
- BOLD = 1
26
- UNDERLINE = 2
27
- BLINK = 4
28
-
29
- attr_reader :font, :foreground, :background
30
-
31
- def initialize (font = STD, foreground = WHITE, background = nil)
32
- raise "not found color #{foreground}" unless (0..7).cover? foreground or (10..17).cover? foreground
33
- @font, @foreground, @background = font, foreground, background
34
-
35
- @bash_command = ''
36
-
37
- @string = "<type=%s, foreground=%s, background=%s>" % [@font, @foreground, @background]
38
- if font == STD
39
- if foreground and foreground < 10
40
- @bash_command << "\e[3#{foreground}m"
41
- else
42
- @bash_command << "\e[9#{foreground-10}m"
43
- end
3
+
4
+ COLORS = [:black, :dark_red, :dark_green, :dark_yellow, :dark_blue, :dark_magenta, :dark_cyan, :grey,
5
+ :dark_grey, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
6
+
7
+ TYPES = [:std, :bold, :underline, :blink]
8
+
9
+ @@colors_map = {
10
+ black: 0,
11
+ dark_red: 1,
12
+ dark_green: 2,
13
+ dark_yellow: 3,
14
+ dark_blue: 4,
15
+ dark_magenta: 5,
16
+ dark_cyan: 6,
17
+ grey: 7,
18
+ dark_grey: 10,
19
+ red: 11,
20
+ green: 12,
21
+ yellow: 13,
22
+ blue: 14,
23
+ magenta: 15,
24
+ cyan: 16,
25
+ white: 17
26
+ }
27
+
28
+ @@types_map = {
29
+ std: 0,
30
+ bold: 1,
31
+ underline: 4,
32
+ blink: 5
33
+ }
34
+
35
+ RESET = "\e[0m"
36
+
37
+ attr_reader :types, :foreground, :background
38
+
39
+ def initialize(types = :std, foreground = :white, background = nil)
40
+
41
+ unless COLORS.include?(foreground)
42
+ raise "not found color #{foreground}"
44
43
  end
45
44
 
46
- if font & BOLD != 0
47
- if foreground < 10
48
- @bash_command << "\e[1;3#{foreground}m"
49
- else
50
- @bash_command << "\e[1;9#{foreground-10}m"
51
- end
45
+ @foreground, @background = foreground, background
46
+
47
+ @types = prepare_types types
48
+
49
+ sequence = []
50
+ @types.each do |type|
51
+ sequence << @@types_map[type]
52
52
  end
53
53
 
54
- if font & UNDERLINE != 0
55
- if foreground < 10
56
- @bash_command << "\e[4;3#{foreground}m"
57
- else
58
- @bash_command << "\e[4;9#{foreground-10}m"
59
- end
54
+ color_index = @@colors_map[@foreground]
55
+
56
+ sequence << if color_index < 10
57
+ "3#{color_index}"
58
+ else
59
+ "9#{color_index - 10}"
60
+ end
61
+
62
+ if @background
63
+ color_index = @@colors_map[@background]
64
+ sequence << if color_index < 10
65
+ "4#{color_index}"
66
+ else
67
+ "10#{color_index - 10}"
68
+ end
60
69
  end
61
70
 
62
- if background.instance_of? Fixnum
63
- if background < 10
64
- @bash_command << "\e[4#{background}m"
71
+ @bash_command = "\e[#{sequence.join(';')}m"
72
+ end
73
+
74
+
75
+ # @return [Array]
76
+ def prepare_types(types)
77
+ case types
78
+ when Symbol then
79
+ [types]
80
+ when Array then
81
+ if types.size > 2
82
+ types.delete_if { |type| type == :std }
83
+ else
84
+ types
85
+ end
65
86
  else
66
- @bash_command << "\e[10#{background-10}m"
67
- end
87
+ raise "types must be Array or Symbol"
68
88
  end
69
-
70
89
  end
71
90
 
72
- def to_bash
73
- @bash_command
91
+ def inspect
92
+ "<Font types=%s, foreground=%s, background=%s>" %
93
+ [@types, @foreground, (@background ? @background : 'nil')]
74
94
  end
75
95
 
76
96
  def to_s
77
- @string
97
+ @bash_command
78
98
  end
79
99
 
100
+ alias to_bash to_s
101
+
80
102
  class << self
103
+
104
+ # @param [Symbol] exclude_color
81
105
  def rand_color (exclude_color = nil)
82
- color = rand(14)
83
- color += 1 if color == exclude_color
84
- color = 0 if color > 14
85
- color += 3 if color > 7
86
- color
87
- end
88
- end
106
+ color = rand(16)
107
+ color += 2 if color > 7
108
+
109
+ if color == @@colors_map[exclude_color]
110
+ color += 1
111
+ color = 0 if color > 17
112
+ end
89
113
 
114
+ @@colors_map.each do |name, code|
115
+ return name if code == color
116
+ end
90
117
 
118
+ end
91
119
  end
120
+
121
+ end
92
122
  end
@@ -9,6 +9,20 @@ module Bash_Visual
9
9
 
10
10
  attr_accessor :console
11
11
 
12
+
13
+ # @param [Hash] options
14
+ #
15
+ # options = {
16
+ # coordinates: [x, y],
17
+ # window_size: [width, height],
18
+ # font: Font.new
19
+ # start: Scroll::ENDING,
20
+ # adapt_size_message: true,
21
+ # prefix: -> { '>' }
22
+ # separator: '-'
23
+ # }
24
+ #
25
+ #
12
26
  def initialize(options)
13
27
 
14
28
  @x, @y = options[:coordinates]
@@ -32,18 +46,21 @@ module Bash_Visual
32
46
  @is_wrap = true
33
47
  @start = options[:start] ? ENDING : BEGINNING
34
48
  @separator = options[:separator] ? options[:separator] : false
35
- @font = options[:font] ? options[:font] : nil
49
+ @type = options[:type] ? options[:type] : nil
36
50
 
37
51
  @stack = []
38
- @console = Console.new @font,Console::OUTPUT_STRING
52
+ @console = Console.new @type, Console::OUTPUT_STRING
39
53
  @mutex = Mutex.new
54
+ nil
40
55
  end
41
56
 
42
57
  def scroll(positions = 1, direction = @direction * positions)
43
58
 
44
59
  end
45
60
 
46
- def add (message, font = @font)
61
+ # @param [String] message
62
+ # @param [Bash_Visual::Font] font
63
+ def add(message, font = @type)
47
64
 
48
65
  if @stack.size.zero?
49
66
  print @console.draw_rectangle(@x + 1, @y + 1, @area_width, @area_height, font)
@@ -51,13 +68,16 @@ module Bash_Visual
51
68
 
52
69
  @stack << {
53
70
  message: prefix() << message.to_s,
54
- font: font
71
+ type: font
55
72
  }
56
73
 
57
74
  redraw()
58
75
  #@stack.slice!(-index, index)
76
+ nil
59
77
  end
60
78
 
79
+
80
+ # @param [Object] prefix
61
81
  def prefix= (prefix)
62
82
  @prefix = prefix
63
83
  end
@@ -77,9 +97,9 @@ module Bash_Visual
77
97
  @stack.reverse.each do |item|
78
98
 
79
99
  message = item[:message].dup.lines.to_a
80
- font = item[:font]
100
+ font = item[:type]
81
101
  unless font.background
82
- font = Font.new font.font, font.foreground, @font.background
102
+ font = Font.new font.type, font.foreground, @type.background
83
103
  end
84
104
 
85
105
  avail_area = print_message(message, font, avail_area)
@@ -89,7 +109,9 @@ module Bash_Visual
89
109
  end
90
110
 
91
111
  # сделать переносы в массиве строк
92
- def rows_wrap! arr, max_len
112
+ # @param [String] arr
113
+ # @param [Integer] max_len
114
+ def rows_wrap!(arr, max_len)
93
115
  max_used_len = 1
94
116
  arr.each_with_index do |row, i|
95
117
  len = row.size
@@ -101,9 +123,11 @@ module Bash_Visual
101
123
  max_used_len
102
124
  end
103
125
 
104
- def write (x, y, message, font)
105
-
106
-
126
+ # @param [Integer] x
127
+ # @param [Integer] y
128
+ # @param [Array] message
129
+ # @param [Bash_Visual::Font] font
130
+ def write(x, y, message, font)
107
131
  string = ''
108
132
  message.each_with_index { |text, i|
109
133
  string << @console.write_to_position(x, y + i, text, font)
@@ -1,3 +1,3 @@
1
1
  module Bash_Visual
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.6'
3
3
  end
@@ -32,7 +32,7 @@ module Bash_Visual
32
32
  end
33
33
 
34
34
  if @separator == true
35
- message[message.size-1] = Font.new(Font::UNDERLINE, font.foreground, font.background).to_bash +
35
+ message[message.size-1] = Font.new(:underline, font.foreground, font.background).to_bash +
36
36
  message.last.ljust(available_area_width, ' ')
37
37
  end
38
38
 
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ $:.unshift(File.dirname(__FILE__))
3
+
4
+ require 'helper'
5
+
6
+ class BuilderTest < Test::Unit::TestCase
7
+
8
+ def test_clear
9
+ font_mock = Bash_Visual::Font.new
10
+ font_mock.to_bash {'X'}
11
+
12
+ builder = Bash_Visual::Builder.new(font_mock)
13
+
14
+ result = builder.clear
15
+ assert_equal("\e[0;0H\e[2J", result)
16
+ end
17
+
18
+ end
data/test/font_test.rb ADDED
@@ -0,0 +1,84 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'helper'
4
+
5
+ class FontTest < Test::Unit::TestCase
6
+
7
+ include Bash_Visual
8
+
9
+ def test_inspect
10
+
11
+ font = Font.new
12
+
13
+ expected = '<Font types=%s, foreground=%s, background=nil>' %
14
+ [font.types, font.foreground]
15
+
16
+ assert_equal(expected, font.inspect)
17
+ end
18
+
19
+ def test_font_all
20
+ font = Font.new([:underline, :bold, :blink])
21
+ expected = ["\e[1;4;5;97m", "\e[1;5;4;97m","\e[4;1;5;97m", "\e[4;5;1;97m", "\e[5;1;4;97m", "\e[5;4;1;97m"]
22
+
23
+ assert_include expected, font.to_s
24
+ end
25
+
26
+ def test_font_without_blink
27
+ font = Font.new [:underline, :bold]
28
+
29
+ expected = ["\e[4;1;97m", "\e[1;4;97m"]
30
+ assert_include expected, font.to_s
31
+ end
32
+
33
+ def test_font_underline
34
+ font = Font.new(:underline)
35
+
36
+ assert_equal "\e[4;97m", font.to_s
37
+ end
38
+
39
+ def test_font_bold
40
+ font = Font.new([:bold])
41
+
42
+ assert_equal "\e[1;97m", font.to_s
43
+ end
44
+
45
+ def test_font_foreground
46
+ font = Font.new(:std, :dark_cyan)
47
+ assert_equal "\e[0;36m", font.to_s
48
+
49
+ font = Font.new(:std, :green)
50
+ assert_equal "\e[0;92m", font.to_s
51
+ end
52
+
53
+
54
+ def test_font_background
55
+ font = Font.new(:std, :white, :dark_cyan)
56
+ assert_equal "\e[0;97;46m", font.to_s
57
+
58
+ font = Font.new(:std, :white, :green)
59
+ assert_equal "\e[0;97;102m", font.to_s
60
+ end
61
+
62
+
63
+ def test_rand_color
64
+ mock_class_method(Font, "def rand (max=0); 3; end")
65
+ color = Font::rand_color
66
+ assert_equal :dark_yellow, color
67
+
68
+ mock_class_method(Font, "def rand (max=0); 15; end")
69
+ color = Font::rand_color :white
70
+ assert_not_equal(color, :white)
71
+
72
+ end
73
+
74
+ protected
75
+
76
+ def mock_class_method(class_name, eval_string)
77
+ eigenclass = class << class_name
78
+ self
79
+ end
80
+ eigenclass.class_eval eval_string
81
+
82
+ end
83
+
84
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "simplecov"
2
+ require "test/unit"
3
+ require "bash-visual"
4
+
5
+ SimpleCov.coverage_dir('../.coverage')
6
+ SimpleCov.start
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bash-visual
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
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-04-26 00:00:00.000000000 Z
12
+ date: 2012-07-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Bash visualisation tools
15
15
  email:
@@ -20,11 +20,13 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
22
  - Gemfile
23
- - Gemfile.lock
24
23
  - LICENSE
25
24
  - README.md
26
25
  - Rakefile
27
26
  - bash-visual.gemspec
27
+ - example/hello_world.rb
28
+ - example/random.rb
29
+ - example/spectrum.rb
28
30
  - lib/bash-visual.rb
29
31
  - lib/bash-visual/builder.rb
30
32
  - lib/bash-visual/console.rb
@@ -33,7 +35,10 @@ files:
33
35
  - lib/bash-visual/scroll.rb
34
36
  - lib/bash-visual/version.rb
35
37
  - lib/bash-visual/vertical_scroll.rb
36
- homepage: https://github.com/AlmazKo/BashConsole
38
+ - test/builder_test.rb
39
+ - test/font_test.rb
40
+ - test/helper.rb
41
+ homepage: https://github.com/AlmazKo/BashVisual
37
42
  licenses:
38
43
  - MIT
39
44
  post_install_message:
data/Gemfile.lock DELETED
@@ -1,14 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- bash-visual (1.0.4)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
-
10
- PLATFORMS
11
- ruby
12
-
13
- DEPENDENCIES
14
- bash-visual!