vedeu 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bc23985fc8270da6cebe20612c4aaa02967a07f
4
- data.tar.gz: bf6587149152e3111183fe10abe958376a024872
3
+ metadata.gz: 79cbcf71bd0a9c214cd3764d10cfc21e8b6aeffa
4
+ data.tar.gz: 570893355905af7e58d89e64cc419ee361133dd3
5
5
  SHA512:
6
- metadata.gz: dd0458ce9a0b1e2b93788a4adef0439e7946db8a5e4e508ce68a662d885dd726b900692af0b8dfd385f24ddb78be19a0ddf5ce628c165243de0672a499a82cc8
7
- data.tar.gz: 6a56e31aba151c295f6989e43947a1e0e6b7571cd5c65d8b17077abf3c15eee7d3fa558559f9e72db63421e1afedb5aeba46eb7b5052b937a49efe5191cb78c0
6
+ metadata.gz: 0d143574721a8b5a0e07eb6fb081c3768fa3c994c28a72303b07f9c1abc3a384262fa8c7abe493dceb5c3069f060b0521c804d1c7cff6bf5864fb2cd9922ba22
7
+ data.tar.gz: 502eb3bba9db28fc1ca44b1f6cd7dd017c18fc3d1f0ce6783d23b56459d758d3dd9cd0d1fb6dc54448fc13342b001304123b4914b206e43617f437cfa5980967
@@ -25,6 +25,7 @@ require 'vedeu/models/store'
25
25
  require 'vedeu/models/stream'
26
26
 
27
27
  require 'vedeu/api/api'
28
+ require 'vedeu/api/composition'
28
29
  require 'vedeu/api/events'
29
30
  require 'vedeu/api/grid'
30
31
  require 'vedeu/api/helpers'
@@ -42,6 +43,7 @@ require 'vedeu/output/clear'
42
43
  require 'vedeu/output/render'
43
44
  require 'vedeu/output/view'
44
45
 
46
+ require 'vedeu/support/buffer'
45
47
  require 'vedeu/support/position'
46
48
  require 'vedeu/support/esc'
47
49
  require 'vedeu/support/input'
@@ -42,7 +42,15 @@ module Vedeu
42
42
  end
43
43
 
44
44
  def view(name, &block)
45
- API::Interface.build({ name: name }, &block)
45
+ {
46
+ interfaces: [
47
+ API::Interface.build({ name: name }, &block)
48
+ ]
49
+ }
50
+ end
51
+
52
+ def views(&block)
53
+ API::Composition.build(&block)
46
54
  end
47
55
 
48
56
  def width
@@ -0,0 +1,29 @@
1
+ module Vedeu
2
+ module API
3
+ class Composition < Vedeu::Composition
4
+ def self.build(&block)
5
+ new(&block).attributes
6
+ end
7
+
8
+ def initialize(&block)
9
+ super
10
+
11
+ if block_given?
12
+ @self_before_instance_eval = eval('self', block.binding)
13
+
14
+ instance_eval(&block)
15
+ end
16
+ end
17
+
18
+ def view(name, &block)
19
+ attributes[:interfaces] << Interface.build({ name: name }, &block)
20
+ end
21
+
22
+ private
23
+
24
+ def method_missing(method, *args, &block)
25
+ @self_before_instance_eval.send(method, *args, &block)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -26,10 +26,8 @@ module Vedeu
26
26
 
27
27
  def define(&block)
28
28
  instance_eval(&block) if block_given?
29
- stored_attributes = Vedeu::Store.create(attributes)
30
- interface = Vedeu::Interface.new(stored_attributes)
31
- Vedeu::Buffers.create(interface)
32
- interface
29
+
30
+ Vedeu::Store.create(attributes)
33
31
  end
34
32
 
35
33
  def line(&block)
@@ -88,6 +86,8 @@ module Vedeu
88
86
  attributes[:style] = value
89
87
  end
90
88
 
89
+ private
90
+
91
91
  def y_out_of_bounds?(value)
92
92
  value < 1 || value > Terminal.height
93
93
  end
@@ -18,10 +18,6 @@ module Vedeu
18
18
  Application.start(configuration)
19
19
 
20
20
  @exit_code = 0
21
- # rescue Vedeu::API::InvalidHeight, Vedeu::API::InvalidWidth
22
- # Vedeu.error("Cannot run this application.\n " \
23
- # "Your terminal window is too small.")
24
-
25
21
  rescue StandardError => uncaught_exception
26
22
  puts uncaught_exception.message
27
23
  puts uncaught_exception.backtrace.join("\n")
@@ -1,34 +1,19 @@
1
1
  module Vedeu
2
2
  class ColourTranslator
3
- def initialize(html_colour = '', options = {})
3
+ def initialize(html_colour = '')
4
4
  @html_colour = html_colour
5
- @options = options
6
5
  end
7
6
 
8
7
  def background
9
8
  return '' unless valid?
10
9
 
11
- if truecolor?
12
- r, g, b = translate
13
- ["\e[48;2;", r, ';', g, ';', b, 'm'].join
14
-
15
- else
16
- ["\e[48;5;", translate, 'm'].join
17
-
18
- end
10
+ sprintf("\e[48;2;%s;%s;%sm", *translate)
19
11
  end
20
12
 
21
13
  def foreground
22
14
  return '' unless valid?
23
15
 
24
- if truecolor?
25
- r, g, b = translate
26
- ["\e[38;2;", r, ';', g, ';', b, 'm'].join
27
-
28
- else
29
- ["\e[38;5;", translate, 'm'].join
30
-
31
- end
16
+ sprintf("\e[38;2;%s;%s;%sm", *translate)
32
17
  end
33
18
 
34
19
  private
@@ -36,60 +21,15 @@ module Vedeu
36
21
  attr_reader :html_colour
37
22
 
38
23
  def translate
39
- if truecolor?
40
- [red, green, blue]
41
- else
42
- [
43
- 16,
44
- ((red / 51) * 36),
45
- ((green / 51) * 6),
46
- ((blue / 51) * 1)
47
- ].inject(:+)
48
- end
49
- end
50
-
51
- def red
52
- html_colour[1..2].to_i(16)
53
- end
54
-
55
- def green
56
- html_colour[3..4].to_i(16)
57
- end
58
-
59
- def blue
60
- html_colour[5..6].to_i(16)
24
+ [
25
+ html_colour[1..2].to_i(16),
26
+ html_colour[3..4].to_i(16),
27
+ html_colour[5..6].to_i(16)
28
+ ]
61
29
  end
62
30
 
63
31
  def valid?
64
- present? && valid_type? && valid_format?
65
- end
66
-
67
- def present?
68
- return true unless html_colour.nil? || html_colour.empty?
69
-
70
- false
71
- end
72
-
73
- def valid_type?
74
- html_colour.is_a?(String)
75
- end
76
-
77
- def valid_format?
78
- html_colour =~ /^#([A-Fa-f0-9]{6})$/
79
- end
80
-
81
- def truecolor?
82
- options.fetch(:truecolor)
83
- end
84
-
85
- def options
86
- defaults.merge!(@options)
87
- end
88
-
89
- def defaults
90
- {
91
- truecolor: false
92
- }
32
+ html_colour.is_a?(String) && html_colour =~ /^#([A-Fa-f0-9]{6})$/
93
33
  end
94
34
  end
95
35
  end
@@ -9,13 +9,11 @@ module Vedeu
9
9
  end
10
10
 
11
11
  def foreground
12
- @foreground ||= ColourTranslator
13
- .new(attributes[:foreground], { truecolor: false }).foreground
12
+ @foreground ||= ColourTranslator.new(attributes[:foreground]).foreground
14
13
  end
15
14
 
16
15
  def background
17
- @background ||= ColourTranslator
18
- .new(attributes[:background], { truecolor: false }).background
16
+ @background ||= ColourTranslator.new(attributes[:background]).background
19
17
  end
20
18
 
21
19
  def to_s
@@ -39,7 +39,13 @@ module Vedeu
39
39
  end
40
40
 
41
41
  def cursor
42
- @cursor ||= attributes[:cursor]
42
+ @cursor ||= if cursor?
43
+ Esc.string('show_cursor')
44
+
45
+ else
46
+ Esc.string('hide_cursor')
47
+
48
+ end
43
49
  end
44
50
 
45
51
  def delay
@@ -52,6 +58,10 @@ module Vedeu
52
58
 
53
59
  private
54
60
 
61
+ def cursor?
62
+ attributes[:cursor] == true
63
+ end
64
+
55
65
  def defaults
56
66
  {
57
67
  name: '',
@@ -1,25 +1,29 @@
1
1
  module Vedeu
2
2
  EntityNotFound = Class.new(StandardError)
3
3
 
4
- module Store
5
- extend self
4
+ module Store
5
+ extend self
6
6
 
7
- def create(attributes)
8
- storage.store(attributes[:name], attributes)
9
- end
7
+ def create(attributes)
8
+ storage.store(attributes[:name], attributes)
10
9
 
11
- def query(name)
12
- storage.fetch(name) { fail EntityNotFound, 'Interface was not found.' }
13
- end
10
+ Buffers.create(Interface.new(attributes))
14
11
 
15
- def reset
16
- @storage = {}
17
- end
12
+ storage
13
+ end
14
+
15
+ def query(name)
16
+ storage.fetch(name) { fail EntityNotFound, 'Interface was not found.' }
17
+ end
18
+
19
+ def reset
20
+ @storage = {}
21
+ end
18
22
 
19
- private
23
+ private
20
24
 
21
- def storage
22
- @storage ||= {}
23
- end
25
+ def storage
26
+ @storage ||= {}
24
27
  end
28
+ end
25
29
  end
@@ -5,66 +5,61 @@ module Vedeu
5
5
  extend self
6
6
 
7
7
  def create(interface)
8
- buffers[interface.name][:clear] = Clear.call(interface)
9
- buffers[interface.name][:group] = interface.group
10
-
11
- Vedeu.event("_refresh_#{interface.name}_".to_sym, interface.delay) do
12
- refresh(interface.name)
13
- end
14
-
15
- unless interface.group.nil? || interface.group.empty?
16
- Vedeu.event("_refresh_group_#{interface.group}_".to_sym, interface.delay) do
17
- refresh_group(interface.group)
18
- end
19
- end
8
+ store(interface.name, Buffer.new({
9
+ name: interface.name,
10
+ clear: Clear.call(interface),
11
+ current: '',
12
+ group: interface.group,
13
+ next: ''
14
+ }))
15
+
16
+ create_events(interface.name, interface.group, interface.delay)
20
17
  end
21
18
 
22
19
  def enqueue(name, sequence)
23
- buffers[name][:next].unshift(sequence)
20
+ store(name, query(name).enqueue(sequence))
24
21
  end
25
22
 
26
- def query(name)
27
- buffers.fetch(name) do
28
- fail RefreshFailed, 'Cannot refresh non-existent interface.'
29
- end
23
+ def refresh_all
24
+ buffers.keys.map { |name| refresh(name) }
30
25
  end
31
26
 
32
- def refresh_group(group)
33
- buffers.select.map do |name, buffer|
34
- name if buffer[:group] == group
35
- end.compact.map { |name| refresh(name) }
27
+ def reset
28
+ @buffers = {}
36
29
  end
37
30
 
38
- def refresh(name)
39
- data = query(name)
40
-
41
- sequence = if data[:next].any?
42
- data[:current] = data[:next].pop
31
+ private
43
32
 
44
- elsif data[:current].empty?
45
- data[:clear]
33
+ def create_events(name, group, delay)
34
+ Vedeu.event("_refresh_#{name}_".to_sym, delay) { refresh(name) }
46
35
 
47
- else
48
- data[:current]
36
+ Vedeu.event("_refresh_group_#{group}_".to_sym, delay) do
37
+ refresh_group(group)
38
+ end unless group.nil? || group.empty?
39
+ end
49
40
 
50
- end
51
- Terminal.output(sequence)
41
+ def query(name)
42
+ buffers.fetch(name) {
43
+ fail RefreshFailed, 'Cannot refresh non-existent interface.'
44
+ }
52
45
  end
53
46
 
54
- def refresh_all
55
- buffers.keys.map { |name| refresh(name) }
47
+ def refresh(name)
48
+ store(name, query(name).refresh)
56
49
  end
57
50
 
58
- def reset
59
- @buffers = {}
51
+ def refresh_group(group)
52
+ buffers.select.map do |name, buffer|
53
+ name if buffer.group == group
54
+ end.compact.map { |name| refresh(name) }
60
55
  end
61
56
 
62
- private
57
+ def store(name, buffer)
58
+ buffers.store(name, buffer)
59
+ end
63
60
 
64
61
  def buffers
65
- @buffers ||= Hash.new do |hash, key|
66
- hash[key] = { current: '', next: [], clear: '', group: '' }
67
- end
62
+ @buffers ||= {}
68
63
  end
69
64
  end
70
65
  end
@@ -9,7 +9,7 @@ module Vedeu
9
9
  end
10
10
 
11
11
  def clear
12
- interface_lines.inject([colours]) do |line, index|
12
+ rows.inject([colours]) do |line, index|
13
13
  line << interface.origin(index) { ' ' * interface.width }
14
14
  end.join
15
15
  end
@@ -22,7 +22,7 @@ module Vedeu
22
22
  interface.colour.to_s
23
23
  end
24
24
 
25
- def interface_lines
25
+ def rows
26
26
  interface.height.times
27
27
  end
28
28
  end
@@ -16,6 +16,7 @@ module Vedeu
16
16
  out << line.to_s
17
17
  end
18
18
  end
19
+ out << interface.cursor
19
20
  out.join
20
21
  end
21
22
 
@@ -35,9 +35,7 @@ module Vedeu
35
35
  end
36
36
 
37
37
  def attributes
38
- {
39
- interfaces: [ output ]
40
- }
38
+ output
41
39
  end
42
40
  end
43
41
  end
@@ -0,0 +1,41 @@
1
+ module Vedeu
2
+ class Buffer
3
+ attr_reader :clear, :current, :group, :name, :_next
4
+
5
+ def initialize(vars)
6
+ @vars = vars
7
+ @name = vars.fetch(:name)
8
+ @clear = vars.fetch(:clear)
9
+ @current = vars.fetch(:current)
10
+ @group = vars.fetch(:group)
11
+ @_next = vars.fetch(:next)
12
+ end
13
+
14
+ def enqueue(sequence)
15
+ merge({ next: sequence })
16
+ end
17
+
18
+ def refresh
19
+ sequence = if !_next.empty?
20
+ merge({ current: _next, next: '' }).current
21
+
22
+ elsif current.empty?
23
+ clear
24
+
25
+ else
26
+ current
27
+
28
+ end
29
+
30
+ Terminal.output(sequence)
31
+
32
+ self
33
+ end
34
+
35
+ private
36
+
37
+ def merge(vars)
38
+ Buffer.new(@vars.merge(vars))
39
+ end
40
+ end
41
+ end