ncumbra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/examples/ex4.rb ADDED
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/env ruby
2
+ # example showing listbox
3
+ # 2018-03-19 -
4
+ require 'umbra'
5
+ require 'umbra/label'
6
+ require 'umbra/listbox'
7
+ require 'umbra/box'
8
+ require 'umbra/togglebutton'
9
+
10
+ def startup
11
+ require 'logger'
12
+ require 'date'
13
+
14
+ path = File.join(ENV["LOGDIR"] || "./" ,"v.log")
15
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
16
+ $log = Logger.new(path)
17
+ $log.level = Logger::DEBUG
18
+ today = Time.now.to_s
19
+ $log.info "listbox demo #{$0} started on #{today}"
20
+ FFI::NCurses.init_pair(10, FFI::NCurses::BLACK, FFI::NCurses::GREEN) # statusline
21
+ end
22
+ def statusline win, str, column = 1
23
+ # LINES-2 prints on second last line so that box can be seen
24
+ win.printstring( FFI::NCurses.LINES-2, column, str, 6, REVERSE)
25
+ end
26
+ begin
27
+ include Umbra
28
+ init_curses
29
+ startup
30
+ #win = Window.new
31
+ win = Window.new #20,100, 0, 20
32
+ statusline(win, " "*(win.width-0), 0)
33
+ win.box
34
+ statusline(win, "Press C-q to quit #{win.height}:#{win.width}", 20)
35
+ str = " Demo of listbox "
36
+ win.title str
37
+ alist = ["one item"*4, "another item"*10, "yet another item"*15]
38
+ #alist = []
39
+ (1..50).each do |i|
40
+ alist << "#{i} entry"
41
+ end
42
+
43
+ # check with only a few rows - DONE
44
+ # check with exactly 20 rows
45
+ # check with long lines
46
+ catch(:close) do
47
+ form = Form.new win
48
+ win.printstring(3,1,"Just testing that listbox is correctly positioned")
49
+ box = Box.new row: 4, col: 2, width: 80, height: 20
50
+ lb = Listbox.new list: alist
51
+ box.fill lb
52
+ win.printstring(box.row+1,0,"XX")
53
+ win.printstring(box.row+1,box.col+box.width,"XX")
54
+ win.printstring(box.row+box.height,1,"This prints below the listbox")
55
+ brow = box.row+box.height+3
56
+ tb = ToggleButton.new onvalue: "Toggle", offvalue: "No Toggle", row: brow, col: 10, value: true
57
+ ab = Button.new text: "Processes" , row: brow, col: 30
58
+ logb = Button.new text: "LogFile" , row: brow, col: 50
59
+
60
+ tb.command do
61
+ if tb.value
62
+ # we no longer have border in listboxes or textboxes
63
+ #lb.border true
64
+ box.title = "Toggled"
65
+ else
66
+ #lb.border false
67
+ box.title = "Untoggled"
68
+ end
69
+ box.repaint_required = true
70
+ end
71
+ # bind the most common event for a listbox which is ENTER_ROW
72
+ lb.command do |ix|
73
+ statusline(win, "Sitting on offset #{lb.current_index}, #{ix.first} ")
74
+ end
75
+ ab.command do
76
+ lb.color_pair = create_color_pair(COLOR_BLACK, COLOR_CYAN)
77
+ #lb.attr = REVERSE
78
+ lb.list = %x{ ps aux }.split("\n")
79
+ box.title = "Processes"
80
+ box.repaint_required = true
81
+ end
82
+ logb.command do
83
+ lb.list=[]
84
+ lb.repaint_required=true
85
+ box.title = "Log File"
86
+ box.repaint_required = true
87
+ # We require a timeout in getch for this to update
88
+ # without thread process hangs and no update happens
89
+ t = Thread.new do
90
+ IO.popen("tail -f v.log") do |output|
91
+ ctr = 0
92
+ while line = output.gets do
93
+ lb.list << line.chomp
94
+ lb.goto_end
95
+ lb.repaint_required=true
96
+ form.repaint
97
+ ctr += 1
98
+ if ctr > 100
99
+ sleep(1)
100
+ ctr = 0
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ # bind to another event of listbox
107
+ lb.bind_event(:LEAVE_ROW) { |ix| statusline(win, "LEFT ROW #{ix.first}", 50) }
108
+ lb.bind_event(:LIST_SELECTION_EVENT) { |w| alert("You selected row #{w.selected_index || "none"} ") }
109
+ form.add_widget box, lb
110
+ form.add_widget tb
111
+ form.add_widget ab
112
+ form.add_widget logb
113
+ form.pack
114
+ form.select_first_field
115
+ win.wrefresh
116
+
117
+ y = x = 1
118
+ while (ch = win.getkey) != FFI::NCurses::KEY_CTRL_Q
119
+ begin
120
+ form.handle_key ch
121
+ rescue => e
122
+ puts e
123
+ puts e.backtrace.join("\n")
124
+ end
125
+ win.wrefresh
126
+ end
127
+ end # close
128
+
129
+ rescue => e
130
+ win.destroy if win
131
+ win = nil
132
+ FFI::NCurses.endwin
133
+ puts e
134
+ puts e.backtrace.join("\n")
135
+ ensure
136
+ win.destroy if win
137
+ FFI::NCurses.endwin
138
+ if e
139
+ puts e
140
+ puts e.backtrace.join("\n")
141
+ end
142
+ end
data/examples/ex5.rb ADDED
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+ # example showing Textbox
3
+ # 2018-03-19 -
4
+ require 'umbra'
5
+ require 'umbra/label'
6
+ require 'umbra/box'
7
+ require 'umbra/textbox'
8
+ require 'umbra/togglebutton'
9
+
10
+ def startup
11
+ require 'logger'
12
+ require 'date'
13
+
14
+ path = File.join(ENV["LOGDIR"] || "./" ,"v.log")
15
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
16
+ $log = Logger.new(path)
17
+ $log.level = Logger::DEBUG
18
+ today = Time.now.to_s
19
+ $log.info "Textbox demo #{$0} started on #{today}"
20
+ FFI::NCurses.init_pair(10, FFI::NCurses::BLACK, FFI::NCurses::GREEN) # statusline
21
+ end
22
+ def statusline win, str, column = 1
23
+ # LINES-2 prints on second last line so that box can be seen
24
+ win.printstring( FFI::NCurses.LINES-2, column, str, 6, REVERSE)
25
+ end
26
+ begin
27
+ include Umbra
28
+ init_curses
29
+ startup
30
+ #win = Window.new
31
+ win = Window.new #20,100, 0, 20
32
+ statusline(win, " "*(win.width-0), 0)
33
+ win.box
34
+ statusline(win, "Press C-q to quit #{win.height}:#{win.width}", 20)
35
+ str = " Demo of Textbox "
36
+ win.title str
37
+ =begin
38
+ alist = ["one item"*4, "another item"*10, "yet another item"*15]
39
+ #alist = []
40
+ (1..50).each do |i|
41
+ alist << "#{i} entry"
42
+ end
43
+ =end
44
+ filename = "./notes"
45
+
46
+ catch(:close) do
47
+ form = Form.new win
48
+ win.printstring(3,1,"Just testing that Textbox is correctly positioned")
49
+ box = Box.new row: 4, col: 2, width: 50, height: 20
50
+ lb = Textbox.new file_name: filename
51
+ box.fill lb
52
+ box.title = filename
53
+ win.printstring(box.row+1,0,"XX")
54
+ win.printstring(box.row+1,box.col+box.width,"XX")
55
+ win.printstring(box.row+box.height,1,"This prints below the Textbox")
56
+ brow = box.row+box.height+3
57
+ tb = ToggleButton.new onvalue: "Left", offvalue: "Center", row: brow, col: 10, value: true
58
+
59
+ tb.command do
60
+ if tb.value
61
+ box.justify = :center
62
+ else
63
+ box.justify = :left
64
+ end
65
+ box.repaint_required = true
66
+ end
67
+ lb.bind_event(:CURSOR_MOVE) {|arr|
68
+ col_offset , current_index, curpos, pcol = arr
69
+ blen = lb.current_row().size
70
+ statusline(win, "offset: #{col_offset} , curpos: #{curpos} , currind: #{current_index} , pcol #{pcol}, len:#{blen}.....", 20)
71
+ }
72
+ form.add_widget box, lb
73
+ form.add_widget tb
74
+ form.pack
75
+ form.select_first_field
76
+ win.wrefresh
77
+
78
+ y = x = 1
79
+ while (ch = win.getkey) != FFI::NCurses::KEY_CTRL_Q
80
+ begin
81
+ form.handle_key ch
82
+ rescue => e
83
+ puts e
84
+ puts e.backtrace.join("\n")
85
+ end
86
+ win.wrefresh
87
+ end
88
+ end # close
89
+
90
+ rescue => e
91
+ win.destroy if win
92
+ win = nil
93
+ FFI::NCurses.endwin
94
+ puts e
95
+ puts e.backtrace.join("\n")
96
+ ensure
97
+ win.destroy if win
98
+ FFI::NCurses.endwin
99
+ if e
100
+ puts e
101
+ puts e.backtrace.join("\n")
102
+ end
103
+ end
data/examples/exbox.rb ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env ruby
2
+ # example showing box
3
+ # 2018-03-19 -
4
+ require 'umbra'
5
+ require 'umbra/label'
6
+ require 'umbra/listbox'
7
+ require 'umbra/textbox'
8
+ require 'umbra/togglebutton'
9
+ require 'umbra/box'
10
+
11
+ def startup
12
+ require 'logger'
13
+ require 'date'
14
+
15
+ path = File.join(ENV["LOGDIR"] || "./" ,"v.log")
16
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
17
+ $log = Logger.new(path)
18
+ $log.level = Logger::DEBUG
19
+ today = Time.now.to_s
20
+ $log.info "Box demo #{$0} started on #{today}"
21
+ FFI::NCurses.init_pair(10, FFI::NCurses::BLACK, FFI::NCurses::GREEN) # statusline
22
+ end
23
+ def statusline win, str, column = 1
24
+ # LINES-2 prints on second last line so that box can be seen
25
+ win.printstring( FFI::NCurses.LINES-2, column, str, 6, REVERSE)
26
+ end
27
+ begin
28
+ include Umbra
29
+ init_curses
30
+ startup
31
+ win = Window.new #20,100, 0, 20
32
+ statusline(win, " "*(win.width-0), 0)
33
+ win.box
34
+ statusline(win, "Press C-q to quit #{win.height}:#{win.width}", 20)
35
+ str = " Demo of Box "
36
+ win.title str
37
+ alist = ["one item"*4, "another item"*10, "yet another item"*15]
38
+ #alist = []
39
+ (1..50).each do |i|
40
+ alist << "#{i} entry"
41
+ end
42
+
43
+ # check with only a few rows - DONE
44
+ # check with exactly 20 rows
45
+ # check with long lines
46
+ catch(:close) do
47
+ form = Form.new win
48
+ box = Box.new row: 2,col: 2, height: 24, width: 80, title: "A box", justify: :left
49
+ win.printstring(3,1,"Just testing that listbox is correctly positioned")
50
+ #lb = Listbox.new list: alist, row: 4, col: 2, width: 70, height: 18
51
+ lb = Listbox.new list: alist
52
+ lb2 = Textbox.new list: %x{ ps aux }.split("\n")
53
+ #box.fill lb
54
+ box.add lb, lb2
55
+ win.printstring(box.row+1,0,"XX")
56
+ win.printstring(box.row+1,lb.col+lb.width,"XX")
57
+ win.printstring(box.row+box.height,1,"This prints below the listbox")
58
+ brow = box.row+box.height+3
59
+ tb = ToggleButton.new onvalue: "Border", offvalue: "No Border", row: brow, col: 10, value: true
60
+ ab = Button.new text: "Processes" , row: brow, col: 30
61
+ logb = Button.new text: "LogFile" , row: brow, col: 50
62
+ bbox = Box.new row: brow-2, col: 8, height: 3, width: 65, visible: false
63
+ bbox.flow tb,ab,logb
64
+
65
+ tb.command do
66
+ if tb.value
67
+ #lb.border true
68
+ else
69
+ #lb.border false
70
+ end
71
+ lb.repaint_required=true
72
+ end
73
+ # bind the most common event for a listbox which is ENTER_ROW
74
+ lb.command do |ix|
75
+ statusline(win, "Sitting on offset #{lb.current_index}, #{ix.first} ")
76
+ end
77
+ ab.command do
78
+ lb.color_pair = create_color_pair(COLOR_BLACK, COLOR_CYAN)
79
+ #lb.attr = REVERSE
80
+ lb.list = %x{ ps aux }.split("\n")
81
+ end
82
+ logb.command do
83
+ lb.list=[]
84
+ lb.repaint_required=true
85
+ # We require a timeout in getch for this to update
86
+ # without thread process hangs and no update happens
87
+ t = Thread.new do
88
+ IO.popen("tail -f v.log") do |output|
89
+ ctr = 0
90
+ while line = output.gets do
91
+ lb.list << line.chomp
92
+ lb.goto_end
93
+ lb.repaint_required=true
94
+ form.repaint
95
+ ctr += 1
96
+ if ctr > 100
97
+ sleep(1)
98
+ ctr = 0
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ # bind to another event of listbox
105
+ lb.bind_event(:LEAVE_ROW) { |ix| statusline(win, "LEFT ROW #{ix.first}", 50) }
106
+ lb.bind_event(:LIST_SELECTION_EVENT) { |w| alert("You selected row #{w.selected_index || "none"} ") }
107
+ form.add_widget lb, lb2
108
+ form.add_widget tb
109
+ form.add_widget ab
110
+ form.add_widget logb, box, bbox
111
+ form.pack
112
+ form.select_first_field
113
+ win.wrefresh
114
+
115
+ y = x = 1
116
+ while (ch = win.getkey) != FFI::NCurses::KEY_CTRL_Q
117
+ begin
118
+ form.handle_key ch
119
+ rescue => e
120
+ puts e
121
+ puts e.backtrace.join("\n")
122
+ alert e.to_s
123
+ end
124
+ win.wrefresh
125
+ end
126
+ end # close
127
+
128
+ rescue => e
129
+ win.destroy if win
130
+ win = nil
131
+ FFI::NCurses.endwin
132
+ puts e
133
+ puts e.backtrace.join("\n")
134
+ ensure
135
+ win.destroy if win
136
+ FFI::NCurses.endwin
137
+ if e
138
+ puts e
139
+ puts e.backtrace.join("\n")
140
+ end
141
+ end
data/examples/exm1.rb ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ # example showing only labels and fields a window
3
+ # 2018-03-10
4
+ require 'umbra'
5
+ require 'umbra/label'
6
+ require 'umbra/button'
7
+ require 'umbra/labeledfield'
8
+ require 'umbra/messagebox'
9
+
10
+ def create_footer_window h = 2 , w = FFI::NCurses.COLS, t = FFI::NCurses.LINES-2, l = 0
11
+ ewin = Window.new(h, w , t, l)
12
+ end
13
+ def old_alert str
14
+ win = create_footer_window
15
+ #FFI::NCurses.init_pair(12, COLOR_WHITE, FFI::NCurses::RED)
16
+ cp = create_color_pair(COLOR_RED, COLOR_WHITE)
17
+ win.wbkgd(FFI::NCurses.COLOR_PAIR(cp)) # white on red, defined here
18
+ win.printstring(0,1, str)
19
+ win.wrefresh
20
+ win.getkey
21
+ win.destroy
22
+ end
23
+ def _alert array, title="Alert"
24
+ mb = MessageBox.new title: title, buttons: "Ok" do
25
+ text array
26
+ end
27
+ mb.run
28
+ end
29
+ def _alert_message str, title="Alert"
30
+ mb = MessageBox.new title: title, buttons: "Okay" do
31
+ message str
32
+ end
33
+ mb.run
34
+ end
35
+ def _alert_fields str
36
+ #mb = MessageBox.new height: 20, width: 60, row: 5, col: 5, title: "Enter yor name" do
37
+ mb = MessageBox.new title: "Testing Messageboxes", width: 80 do
38
+ add Label.new text: "Name"
39
+ add Field.new name:"name", default: "Rahul", width: 25, color_pair: CP_CYAN, attr: REVERSE
40
+ add LabeledField.new label:"Age:", name:"age", text:"25", col: 15, color_pair: CP_CYAN, attr: REVERSE
41
+ # unfortunately this exceeds since width only takes field into account not label
42
+ add LabeledField.new label:"Address:", name:"address", width:50 , col: 15, color_pair: CP_CYAN, attr: REVERSE
43
+ end
44
+ mb.run
45
+ end
46
+ def startup
47
+ require 'logger'
48
+ require 'date'
49
+
50
+ path = File.join(ENV["LOGDIR"] || "./" ,"v.log")
51
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
52
+ $log = Logger.new(path)
53
+ $log.level = Logger::DEBUG
54
+ today = Date.today
55
+ $log.info "MessageBox demo #{$0} started on #{today}"
56
+ FFI::NCurses.init_pair(10, FFI::NCurses::BLACK, FFI::NCurses::GREEN) # statusline
57
+ end
58
+ def statusline win, str, col = 0
59
+ win.printstring( FFI::NCurses.LINES-1, col, str, 10)
60
+ end
61
+ begin
62
+ include Umbra
63
+ init_curses
64
+ startup
65
+ #FFI::NCurses.init_pair(12, COLOR_WHITE, FFI::NCurses::RED)
66
+ win = Window.new
67
+ statusline(win, " "*(win.width-0), 0)
68
+ statusline(win, "Press C-q to quit #{win.height}:#{win.width}", 20)
69
+ title = Label.new( :text => "Demo of MessageBox", :row => 0, :col => 0 , :width => FFI::NCurses.COLS-1,
70
+ :justify => :center, :color_pair => CP_BLACK)
71
+
72
+ form = Form.new win
73
+ form.add_widget title
74
+
75
+ b1 = Button.new text: "String", row: 10, col: 10
76
+ b2 = Button.new text: "Array", row: 10, col: 30
77
+ b3 = Button.new text: "Fields", row: 10, col: 50
78
+ b4 = Button.new text: "Confirm", row: 10, col: 70
79
+ message_label = Label.new({text: "Message comes here C-q to quit",
80
+ :name=>"message_label",:row => win.height-2, :col => 2, :width => 60,
81
+ :height => 2, :color_pair => CP_MAGENTA})
82
+ b1.command do
83
+ _alert_message "This is an alert with a string"
84
+ end
85
+ b2.command do
86
+ array = File.read($0).split("\n")
87
+ _alert(array, $0)
88
+ end
89
+ b3.command do
90
+ _alert_fields "dummy"
91
+ end
92
+ b4.command do
93
+ ret = confirm "Do you wish to go?"
94
+ message_label.text = "You selected #{ret}"
95
+ #message_label.touch
96
+ end
97
+ form.add_widget message_label, b1, b2, b3, b4
98
+ form.pack
99
+ form.select_first_field
100
+ win.wrefresh
101
+
102
+ y = x = 1
103
+ while (ch = win.getkey) != FFI::NCurses::KEY_CTRL_Q
104
+ begin
105
+ form.handle_key ch
106
+ rescue => e
107
+ #_alert(e.to_s.strip + ".")
108
+
109
+ _alert(["Error in Messagebox: #{e} ", *e.backtrace], "Exception")
110
+ #_alert(e.backtrace)
111
+ $log.error e
112
+ $log.error e.backtrace.join("\n")
113
+ e = nil
114
+ end
115
+ win.wrefresh
116
+ end
117
+
118
+ rescue => e
119
+ win.destroy if win
120
+ FFI::NCurses.endwin
121
+ $log.error e
122
+ $log.error e.backtrace.join("\n")
123
+ puts "printing inside rescue"
124
+ puts e
125
+ puts e.backtrace.join("\n")
126
+ e = nil
127
+ ensure
128
+ win.destroy if win
129
+ FFI::NCurses.endwin
130
+ if e
131
+ $log.error e
132
+ $log.error e.backtrace.join("\n")
133
+ puts "printing inside ensure"
134
+ puts e
135
+ puts e.backtrace.join("\n")
136
+ end
137
+ end
data/examples/keys.rb ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ require 'umbra'
4
+ require 'umbra/form'
5
+
6
+ def startup
7
+ require 'logger'
8
+ require 'date'
9
+
10
+ path = File.join(ENV["LOGDIR"] || "./" ,"v.log")
11
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
12
+ $log = Logger.new(path)
13
+ $log.level = Logger::DEBUG
14
+ today = Date.today
15
+ $log.info "Started up on #{today}"
16
+ FFI::NCurses.init_pair(10, FFI::NCurses::BLACK, FFI::NCurses::GREEN) # statusline
17
+ end
18
+ begin
19
+ include Umbra
20
+ init_curses
21
+ startup
22
+ txt = "Demo of keys. Press various keys to see what value comes"
23
+ win = Window.new
24
+ win.printstr txt
25
+ win.printstr("Press Ctrl-Q to quit", 1, 50)
26
+
27
+ win.wrefresh
28
+
29
+ form = Form.new win
30
+
31
+ ch = 0
32
+ y = x = 1
33
+ while (ch = win.getch) != FFI::NCurses::KEY_CTRL_Q
34
+ next if ch == -1
35
+ if ch == 2727 #or ch == -1
36
+ $log.debug "t.rb got #{ch}"
37
+ break
38
+ end
39
+ rv = nil
40
+ #rv = win.mvwin(y, x)
41
+
42
+ case ch
43
+ when Integer
44
+ win.printstring y, x, "#{ch.to_s}:#{FFI::NCurses::keyname(ch)}"
45
+ when String
46
+ win.printstring y, x, "#{ch.to_s}"
47
+ end
48
+ y += 1
49
+ # start a new column if we exceed last line
50
+ if y >= FFI::NCurses.LINES-1
51
+ y = 1
52
+ x += 10
53
+ end
54
+
55
+ win.wrefresh
56
+ end
57
+
58
+ rescue Object => e
59
+ @window.destroy if @window
60
+ FFI::NCurses.endwin
61
+ puts e
62
+ puts e.backtrace.join("\n")
63
+ ensure
64
+ @window.destroy if @window
65
+ FFI::NCurses.endwin
66
+ puts
67
+ end