reterm 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +131 -0
  4. data/designer/main.rb +10 -0
  5. data/designer/src/ComponentParams.rb +120 -0
  6. data/designer/src/ComponentsList.rb +90 -0
  7. data/designer/src/CreatedList.rb +156 -0
  8. data/designer/src/Designer.rb +114 -0
  9. data/designer/src/ToggleArea.rb +67 -0
  10. data/designer/src/common.rb +1 -0
  11. data/designer/src/component_factory.rb +30 -0
  12. data/designer/src/component_map.rb +116 -0
  13. data/designer/src/glade/ComponentParams.glade +96 -0
  14. data/designer/src/glade/Designer.glade +184 -0
  15. data/designer/src/images/add.png +0 -0
  16. data/designer/src/images/asciitext.png +0 -0
  17. data/designer/src/images/blank.png +0 -0
  18. data/designer/src/images/blank_sm.png +0 -0
  19. data/designer/src/images/button.png +0 -0
  20. data/designer/src/images/dial.png +0 -0
  21. data/designer/src/images/entry.png +0 -0
  22. data/designer/src/images/hslider.png +0 -0
  23. data/designer/src/images/label.png +0 -0
  24. data/designer/src/images/matrix.png +0 -0
  25. data/designer/src/images/orig/Check.png +0 -0
  26. data/designer/src/images/orig/ascii_text.png +0 -0
  27. data/designer/src/images/orig/button.png +0 -0
  28. data/designer/src/images/orig/dial.png +0 -0
  29. data/designer/src/images/orig/entry.png +0 -0
  30. data/designer/src/images/orig/hslider.png +0 -0
  31. data/designer/src/images/orig/label.png +0 -0
  32. data/designer/src/images/orig/matrix.png +0 -0
  33. data/designer/src/images/orig/radio.png +0 -0
  34. data/designer/src/images/orig/rocker.png +0 -0
  35. data/designer/src/images/orig/slist.png +0 -0
  36. data/designer/src/images/orig/vslider.png +0 -0
  37. data/designer/src/images/radio.png +0 -0
  38. data/designer/src/images/remove.png +0 -0
  39. data/designer/src/images/rocker.png +0 -0
  40. data/designer/src/images/slist.png +0 -0
  41. data/designer/src/images/vslider.png +0 -0
  42. data/lib/reterm.rb +22 -0
  43. data/lib/reterm/color_pair.rb +66 -0
  44. data/lib/reterm/component.rb +40 -0
  45. data/lib/reterm/components.rb +32 -0
  46. data/lib/reterm/components/ascii_text.rb +46 -0
  47. data/lib/reterm/components/button.rb +31 -0
  48. data/lib/reterm/components/dial.rb +95 -0
  49. data/lib/reterm/components/dialog.rb +34 -0
  50. data/lib/reterm/components/entry.rb +38 -0
  51. data/lib/reterm/components/hslider.rb +32 -0
  52. data/lib/reterm/components/image.rb +55 -0
  53. data/lib/reterm/components/label.rb +20 -0
  54. data/lib/reterm/components/matrix.rb +43 -0
  55. data/lib/reterm/components/radio.rb +33 -0
  56. data/lib/reterm/components/rocker.rb +71 -0
  57. data/lib/reterm/components/slist.rb +32 -0
  58. data/lib/reterm/components/template.rb +23 -0
  59. data/lib/reterm/components/vslider.rb +61 -0
  60. data/lib/reterm/init.rb +95 -0
  61. data/lib/reterm/layout.rb +63 -0
  62. data/lib/reterm/layouts.rb +16 -0
  63. data/lib/reterm/layouts/horizontal.rb +19 -0
  64. data/lib/reterm/layouts/vertical.rb +19 -0
  65. data/lib/reterm/loader.rb +126 -0
  66. data/lib/reterm/menu.rb +81 -0
  67. data/lib/reterm/mixins/cdk_component.rb +45 -0
  68. data/lib/reterm/mixins/component_input.rb +37 -0
  69. data/lib/reterm/mixins/event_dispatcher.rb +20 -0
  70. data/lib/reterm/mixins/nav_input.rb +126 -0
  71. data/lib/reterm/panel.rb +37 -0
  72. data/lib/reterm/resize.rb +27 -0
  73. data/lib/reterm/terminal.rb +33 -0
  74. data/lib/reterm/version.rb +3 -0
  75. data/lib/reterm/window.rb +260 -0
  76. metadata +155 -0
@@ -0,0 +1,22 @@
1
+ # The Ruby Enhanced Terminal interactive framework
2
+ # facilitating dynmaic/feature rich terminal applications.
3
+ module RETerm
4
+ end # end module RETerm
5
+
6
+ require 'reterm/init'
7
+ require 'reterm/color_pair'
8
+
9
+ require 'reterm/mixins/event_dispatcher'
10
+ require 'reterm/mixins/component_input'
11
+ require 'reterm/mixins/nav_input'
12
+ require 'reterm/mixins/cdk_component'
13
+
14
+ require 'reterm/terminal'
15
+ require 'reterm/window'
16
+ require 'reterm/panel'
17
+ require 'reterm/menu'
18
+
19
+ require 'reterm/components'
20
+ require 'reterm/layouts'
21
+
22
+ require 'reterm/loader'
@@ -0,0 +1,66 @@
1
+ require 'set'
2
+
3
+ module RETerm
4
+ # Defines a pair of colors used for foreground / background
5
+ # rendering associated with id and tag list.
6
+ class ColorPair
7
+ attr_accessor :id
8
+ attr_accessor :tags
9
+
10
+ attr_accessor :fg
11
+ attr_accessor :bg
12
+
13
+ # Redefined system RGB color. Color name should be specified
14
+ # as well as new RGB components
15
+ #
16
+ # @param [String, Symbol] color name of color to change,
17
+ # ex "red", "blue", etc
18
+ # @param [Integer] r value to assign to red component
19
+ # @param [Integer] g value to assign to green component
20
+ # @param [Integer] b value to assign to blue component
21
+ def self.change(color, r, g, b)
22
+ Ncurses::init_color Ncurses.const_get("COLOR_#{color.to_s.upcase}"), r, g, b
23
+ end
24
+
25
+ # Instantiate a new ColorPair, specifying foreground and background
26
+ # colors as well as any tags.
27
+ #
28
+ # A unique id for this color pair will be autogenerated
29
+ #
30
+ # @param [String, Symbol] fg forground color name
31
+ # @param [String, Symbol] bg background color name
32
+ # @param [Array<String, Symbol>] tags array of tags to assign
33
+ # to color pair
34
+ def initialize(fg, bg, *tags)
35
+ @@id ||= 0
36
+ @@id += 1
37
+ @id = @@id
38
+
39
+ @tags = Set.new(tags)
40
+
41
+ # FIXME need to verify input is in valid domain
42
+ # before conveRETermng it to symbol w/ "intern"
43
+ fg = fg.to_s.downcase.intern
44
+ bg = bg.to_s.downcase.intern
45
+
46
+ Ncurses.init_pair(@id,
47
+ Ncurses.const_get("COLOR_#{fg.to_s.upcase}"),
48
+ Ncurses.const_get("COLOR_#{bg.to_s.upcase}"))
49
+ end
50
+
51
+ # Create and store a new Color Pair in a static
52
+ # registry
53
+ def self.register(fg, bg, *tags)
54
+ @@registry ||= []
55
+ @@registry << new(fg, bg, *tags)
56
+ @@registry.last
57
+ end
58
+
59
+ # Return first Color Pair found with the given tag
60
+ # or nil for no matches
61
+ def self.for(tag)
62
+ @@registry ||= []
63
+ @@registry.find { |cp| cp.tags.include?(tag) }
64
+ end
65
+ end # class ColorPair
66
+ end # module RETerm
@@ -0,0 +1,40 @@
1
+ module RETerm
2
+ # A Component is a generic widget container associated with a window.
3
+ # Subclasses each implement a specific UI artifact.
4
+ class Component
5
+ attr_accessor :window
6
+
7
+ # This method is invoked to cleanup the component on shutdown.
8
+ # It should be be overriden by subclasses that needs to clean
9
+ # resources before being deallocated
10
+ def finalize!
11
+ end
12
+
13
+ # Return a boolean indicating if a {ColorPair} has been
14
+ # assign to component
15
+ def colored?
16
+ !!@colors
17
+ end
18
+
19
+ # Assign the specified {ColorPair} to the component
20
+ def colors=(c)
21
+ @colors = c
22
+ end
23
+
24
+ # Returns a boolean indicating if the user should be
25
+ # able to focus on and activate the component. By default
26
+ # this is false, but interactive components should override
27
+ # and return true.
28
+ def activatable?
29
+ false
30
+ end
31
+
32
+ # Actual activation mechanism, invoked by the navigation
33
+ # logic when the user has selected an activatable? component.
34
+ # Should be overriden by interactive subcomponents to
35
+ # process user inpute specific to that component
36
+ def activate!
37
+ raise RuntimeError, "should not be activated"
38
+ end
39
+ end # class Component
40
+ end # module RETerm
@@ -0,0 +1,32 @@
1
+ module RETerm
2
+ # Encapsulates all the Components which have been
3
+ # currently implemented
4
+ module Components
5
+ def self.all
6
+ @a ||= Components.constants
7
+ end
8
+
9
+ def self.names
10
+ @n ||= all.collect { |c| c.to_s }
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'reterm/component'
16
+
17
+ # custom
18
+ require 'reterm/components/dial'
19
+ require 'reterm/components/rocker'
20
+ require 'reterm/components/vslider'
21
+ require 'reterm/components/label'
22
+ require 'reterm/components/ascii_text'
23
+ require 'reterm/components/image'
24
+
25
+ # cdk
26
+ require 'reterm/components/hslider'
27
+ require 'reterm/components/button'
28
+ require 'reterm/components/dialog'
29
+ require 'reterm/components/radio'
30
+ require 'reterm/components/slist'
31
+ require 'reterm/components/entry'
32
+ require 'reterm/components/matrix'
@@ -0,0 +1,46 @@
1
+ module RETerm
2
+ module Components
3
+ # Renders specified text via ascii graphics.
4
+ #
5
+ # This Component depends on the 'artii' gem.
6
+ class AsciiText < Component
7
+
8
+ # Initialize the AsciiText component
9
+ #
10
+ # @param [Hash] args ascii text params
11
+ # @option args [String] :text actual text to render,
12
+ # default empty string
13
+ # @option args [String] :font font to use
14
+ def initialize(args={})
15
+ @text = args[:text] || ""
16
+ @font = args[:font]
17
+ end
18
+
19
+ def draw!
20
+ refresh_win
21
+ end
22
+
23
+ private
24
+
25
+ def atext
26
+ @atext ||= begin
27
+ require 'artii'
28
+ art = @font.nil? ? Artii::Base.new :
29
+ Artii::Base.new(:font => @font)
30
+ art.asciify @text
31
+ end
32
+ end
33
+
34
+ def refresh_win
35
+ y = 1
36
+ atext.split("\n").each { |t|
37
+ window.mvaddstr(y, 1, t)
38
+ y += 1
39
+ }
40
+
41
+ window.refresh
42
+ update_reterm
43
+ end
44
+ end # class AsciiText
45
+ end # module Components
46
+ end # module RETerm
@@ -0,0 +1,31 @@
1
+ module RETerm
2
+ module Components
3
+ # CDK Button Component
4
+ class Button < Component
5
+ include CDKComponent
6
+
7
+ # Initialize the Button component
8
+ #
9
+ # @param [Hash] args button params
10
+ # @option args [String] :title title of button
11
+ def initialize(args={})
12
+ @title = args[:title] || ""
13
+ end
14
+
15
+ private
16
+
17
+ def callback
18
+ proc { |b|
19
+ puts "Clicked"
20
+ }
21
+ end
22
+
23
+ def _component
24
+ CDK::BUTTON.new(window.cdk_scr, # cdk screen
25
+ 2, 1, @title, # x, y, title
26
+ callback, # click callback
27
+ false, false) # box, shadow
28
+ end
29
+ end # Button
30
+ end # module Components
31
+ end # module RETerm
@@ -0,0 +1,95 @@
1
+ module RETerm
2
+ module Components
3
+ class Dial < Component
4
+ include ComponentInput
5
+
6
+ attr_accessor :initial_value, :increment, :range, :labels
7
+
8
+ attr_accessor :value
9
+
10
+ # Enapsulating window should be at least 4x4 for decent effect,
11
+ # at least 7x7 provides best resolution
12
+ #
13
+ # @param [Hash] args dial params
14
+ #
15
+ # @example activating a dial
16
+ # win = Window.new
17
+ # dial = Dial.new
18
+ # win.component = dial
19
+ #
20
+ # val = dial.activate!
21
+ #
22
+ def initialize(args={})
23
+ @initial_value = 0
24
+ @increment = 0.01
25
+ @range = [0, 1]
26
+ @labels = true
27
+ @value = initial_value
28
+ end
29
+
30
+ def draw!
31
+ refresh_win
32
+ end
33
+
34
+ def activatable?
35
+ true
36
+ end
37
+
38
+ def activate!
39
+ refresh_win
40
+ handle_input
41
+ @value
42
+ end
43
+
44
+ private
45
+
46
+ def on_inc
47
+ @value += increment
48
+ @value = 1 if @value > 1
49
+ refresh_win
50
+ end
51
+
52
+ def on_dec
53
+ @value -= increment
54
+ @value = 0 if @value < 0
55
+ refresh_win
56
+ end
57
+
58
+ def refresh_win
59
+ if @labels
60
+ window.mvaddstr(1, 1, '-')
61
+ window.mvaddstr(1, window.cols-2, '+')
62
+ end
63
+
64
+ rad = Math.sqrt((window.rows/2)**2 + (window.cols/2)**2)
65
+ padding = rad/2 + 1
66
+
67
+ rows = window.rows - padding
68
+ cols = window.cols - padding
69
+ rad = Math.sqrt((rows/2)**2 + (cols/2)**2)
70
+
71
+ a = 0
72
+ while a < @value
73
+ ar = a * (2 * Math::PI)
74
+ x = rad * Math.cos(3*Math::PI/2 - ar) + cols/2 + padding/2
75
+ y = rad * Math.sin(3*Math::PI/2 - ar) + rows/2 + padding/2
76
+ a += increment
77
+
78
+ window.mvaddstr(y, x, '*')
79
+ end
80
+
81
+ while a < 1
82
+ ar = a * (2 * Math::PI)
83
+ x = rad * Math.cos(3*Math::PI/2 - ar) + cols/2 + padding/2
84
+ y = rad * Math.sin(3*Math::PI/2 - ar) + rows/2 + padding/2
85
+ a += increment
86
+
87
+ window.mvaddstr(y, x, ' ')
88
+ end
89
+
90
+ window.refresh
91
+ update_reterm
92
+ end
93
+ end # class Dial
94
+ end # module Components
95
+ end # module RETerm
@@ -0,0 +1,34 @@
1
+ module RETerm
2
+ module Components
3
+ # CDK Dialog Component
4
+ class Dialog < Component
5
+ include CDKComponent
6
+
7
+ # Initialize the Dialog component
8
+ #
9
+ # @param [Hash] args dialog params
10
+ # @option args [String, Array<String>] :message string
11
+ # message(s) to assign to dialog
12
+ # @option args [String, Array<String>] :buttons string
13
+ # buttons to assign to dialog
14
+ #
15
+ def initialize(args={})
16
+ @message = [args[:message]].flatten.compact
17
+ @buttons = [args[:buttons]].flatten.compact
18
+
19
+ @buttons = ["</B/24>OK", "</B16>Cancel"] if @buttons.empty?
20
+ end
21
+
22
+ private
23
+
24
+ def _component
25
+ CDK::DIALOG.new(window.cdk_scr,
26
+ CDK::CENTER, CDK::CENTER,
27
+ @message, @message.size,
28
+ @buttons, @buttons.size,
29
+ Ncurses.COLOR_PAIR(2) | Ncurses::A_REVERSE, # highlight
30
+ true, true, false) # seperate, box, shadow
31
+ end
32
+ end # Dialog
33
+ end # module Components
34
+ end # module RETerm
@@ -0,0 +1,38 @@
1
+ module RETerm
2
+ module Components
3
+ # CDK Entry Component
4
+ class Entry < Component
5
+ include CDKComponent
6
+
7
+ # Initialize the Entry component
8
+ #
9
+ # @param [Hash] args entry params
10
+ # @option args [String] :title title to assign
11
+ # to entry
12
+ # @option args [String] :label label to assign
13
+ # to entry
14
+ # @option args [Integer] :min min entry length
15
+ # @option args [Integer] :max max entry length
16
+ def initialize(args={})
17
+ @title = args[:title] || ""
18
+ @label = args[:label] || ""
19
+ @min_len = args[:min] || 0
20
+ @max_len = args[:max] || 100
21
+ end
22
+
23
+ private
24
+
25
+ def _component
26
+ CDK::ENTRY.new(window.cdk_scr, # cdkscreen,
27
+ CDK::CENTER, CDK::CENTER, # xpos, ypos
28
+ @title, @label, # title, label
29
+ Ncurses::A_NORMAL, # field attribute (eg typed chars)
30
+ '.', # filler char
31
+ :MIXED, # display type
32
+ window.cols-@label.size-5, # field width
33
+ 0, 256, # min/max len
34
+ false, false) # box, shadow
35
+ end
36
+ end # Radio
37
+ end # module Components
38
+ end # module RETerm