rndk 0.2.0 → 1.0.0

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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -4
  3. data/TODO +21 -1
  4. data/demos/appointment.rb +279 -299
  5. data/demos/clock.rb +13 -8
  6. data/demos/rss-reader.rb +84 -0
  7. data/examples/01-hello-world.rb +13 -11
  8. data/examples/02-colors.rb +14 -21
  9. data/examples/03-markup.rb +7 -7
  10. data/examples/04-quick-widgets.rb +2 -2
  11. data/examples/05-position-widget.rb +50 -31
  12. data/examples/06-callbacks.rb +77 -0
  13. data/examples/07-traverse.rb +90 -0
  14. data/examples/10-all-widgets.rb +165 -0
  15. data/examples/calendar.rb +20 -32
  16. data/examples/entry.rb +15 -20
  17. data/examples/label.rb +11 -11
  18. data/examples/scroll.rb +16 -60
  19. data/examples/slider.rb +18 -19
  20. data/examples/viewer.rb +65 -0
  21. data/lib/rndk.rb +28 -7
  22. data/lib/rndk/alphalist.rb +309 -313
  23. data/lib/rndk/button.rb +239 -157
  24. data/lib/rndk/buttonbox.rb +136 -103
  25. data/lib/rndk/calendar.rb +246 -203
  26. data/lib/rndk/core/color.rb +63 -13
  27. data/lib/rndk/core/display.rb +1 -1
  28. data/lib/rndk/core/draw.rb +11 -11
  29. data/lib/rndk/core/markup.rb +21 -21
  30. data/lib/rndk/core/quick_widgets.rb +75 -96
  31. data/lib/rndk/core/screen.rb +145 -102
  32. data/lib/rndk/core/traverse.rb +150 -136
  33. data/lib/rndk/core/utils.rb +5 -6
  34. data/lib/rndk/core/widget.rb +207 -191
  35. data/lib/rndk/core/widget_bind.rb +108 -0
  36. data/lib/rndk/dialog.rb +88 -56
  37. data/lib/rndk/entry.rb +79 -64
  38. data/lib/rndk/fscale.rb +38 -20
  39. data/lib/rndk/fslider.rb +38 -23
  40. data/lib/rndk/graph.rb +92 -53
  41. data/lib/rndk/itemlist.rb +80 -62
  42. data/lib/rndk/label.rb +111 -77
  43. data/lib/rndk/radio.rb +138 -128
  44. data/lib/rndk/scale.rb +123 -122
  45. data/lib/rndk/scroll.rb +444 -391
  46. data/lib/rndk/scroller.rb +21 -21
  47. data/lib/rndk/slider.rb +149 -140
  48. data/lib/rndk/template.rb +74 -61
  49. data/lib/rndk/version.rb +1 -1
  50. data/lib/rndk/viewer.rb +499 -298
  51. metadata +8 -14
  52. data/demos/fileview.rb +0 -141
  53. data/lib/rndk/dscale.rb +0 -13
  54. data/lib/rndk/fselect.rb +0 -938
  55. data/lib/rndk/histogram.rb +0 -412
  56. data/lib/rndk/marquee.rb +0 -244
  57. data/lib/rndk/matrix.rb +0 -1189
  58. data/lib/rndk/mentry.rb +0 -619
  59. data/lib/rndk/menu.rb +0 -478
  60. data/lib/rndk/selection.rb +0 -630
  61. data/lib/rndk/swindow.rb +0 -766
  62. data/lib/rndk/uscale.rb +0 -14
  63. data/lib/rndk/uslider.rb +0 -14
@@ -11,19 +11,24 @@ require 'rndk/label'
11
11
 
12
12
  begin
13
13
  # Set up RNDK
14
- rndkscreen = RNDK::Screen.new
14
+ screen = RNDK::Screen.new
15
15
  RNDK::Color.init
16
16
 
17
17
  # Initial time string
18
18
  mesg = ['</1/B>HH:MM:SS']
19
19
 
20
20
  # Declare the labels.
21
- label = RNDK::Label.new(rndkscreen, RNDK::CENTER, RNDK::CENTER, mesg, true, true)
21
+ label = RNDK::Label.new(screen, {
22
+ :x => RNDK::CENTER,
23
+ :y => RNDK::CENTER,
24
+ :text => mesg,
25
+ :shadow => true
26
+ })
22
27
 
23
28
  # Woops, something bad happened
24
29
  if label.nil?
25
- rndkscreen.destroy
26
- RNDK::Screen.end_rndk
30
+ screen.destroy
31
+ RNDK::Screen.finish
27
32
 
28
33
  puts "Cannot create the label. Is the window too small?"
29
34
  exit 1
@@ -47,18 +52,18 @@ begin
47
52
  mesg = [str]
48
53
 
49
54
  # Set the label contents
50
- label.set(mesg, label.box)
55
+ label.set(:text => mesg)
51
56
 
52
57
  # Draw the label and sleep
53
- label.draw(label.box)
58
+ label.draw
54
59
  Ncurses.napms(500)
55
60
  end
56
61
 
57
- RNDK::Screen.end_rndk
62
+ RNDK::Screen.finish
58
63
 
59
64
  # In case something goes wrong
60
65
  rescue Exception => e
61
- RNDK::Screen.end_rndk
66
+ RNDK::Screen.finish
62
67
 
63
68
  puts e
64
69
  puts e.inspect
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # A simple RSS reader.
4
+ # It receives a feed address as an argument, defaulting to the
5
+ # Ruby Language feed if not provided.
6
+ #
7
+ # When an item is selected, it's content pops-up.
8
+ #
9
+ require 'rss'
10
+ require 'open-uri'
11
+ require 'rndk/label'
12
+ require 'rndk/scroll'
13
+
14
+ DEFAULT_URL = 'https://www.ruby-lang.org/en/feeds/news.rss'
15
+
16
+ begin
17
+ # which feed
18
+ url = DEFAULT_URL
19
+ url = ARGV.first if ARGV.first
20
+
21
+ # retrieving things
22
+ puts 'Fetching feeds...'
23
+ feed = RSS::Parser.parse (open url)
24
+
25
+ # starting RNDK
26
+ screen = RNDK::Screen.new
27
+ RNDK::Color.init
28
+
29
+ # building label
30
+ title = ["<C></77>#{feed.channel.title}",
31
+ "</76>Press ESC to quit"]
32
+
33
+ label = RNDK::Label.new(screen, {
34
+ :x => RNDK::CENTER,
35
+ :y => RNDK::TOP,
36
+ :title => "</77>#{feed.channel.title}"
37
+ })
38
+
39
+ # will show the titles at scroll widget
40
+ titles = []
41
+ feed.items.each { |item| titles << item.title }
42
+
43
+ # building scroll
44
+ scroll = RNDK::Scroll.new(screen, {
45
+ :x => RNDK::CENTER,
46
+ :y => 4,
47
+ :width => RNDK::Screen.width/2,
48
+ :height => RNDK::Screen.height/2 - 5,
49
+ :title => "<C></77>Items",
50
+ :items => titles,
51
+ :numbers => true,
52
+ :highlight => RNDK::Color[:cyan],
53
+ })
54
+ screen.refresh
55
+
56
+ loop do
57
+ scroll.activate
58
+
59
+ # Scroll exited, thus the
60
+ # user selected an item
61
+ # or pressed ESC to get out
62
+ if scroll.exit_type == :NORMAL
63
+
64
+ # Getting current item's content
65
+ index = scroll.current_item
66
+ raw_message = feed.items[index].description
67
+
68
+ # Removing '\n' at the end of all the lines.
69
+ message = []
70
+ raw_message.lines.each { |line| message << line.chomp }
71
+
72
+ # Show current item's content on a pop-up
73
+ screen.popup_label message
74
+
75
+ # user pressed ESC - wants to quit
76
+ elsif scroll.exit_type == :ESCAPE_HIT
77
+
78
+ # good bye!
79
+ RNDK::Screen.finish
80
+ exit
81
+ end
82
+ end
83
+ end
84
+
@@ -7,7 +7,7 @@ require 'rndk/label'
7
7
 
8
8
  begin
9
9
  # Startup Ncurses and RNDK
10
- rndkscreen = RNDK::Screen.new
10
+ screen = RNDK::Screen.new
11
11
 
12
12
  # Set up RNDK colors
13
13
  RNDK::Color.init
@@ -24,32 +24,34 @@ begin
24
24
  ]
25
25
 
26
26
  # Declare the labels.
27
- label = RNDK::Label.new(rndkscreen, # screen
28
- RNDK::CENTER, # x
29
- RNDK::CENTER, # y
30
- mesg, # message
31
- true, # box?
32
- true) # shadow?
27
+ label = RNDK::Label.new(screen, {
28
+ :x => RNDK::CENTER,
29
+ :y => RNDK::CENTER,
30
+ :text => mesg,
31
+ })
32
+
33
33
 
34
34
  if label.nil?
35
35
  # Clean up the memory.
36
- rndkscreen.destroy
36
+ screen.destroy
37
37
 
38
38
  # End curses...
39
- RNDK::Screen.end_rndk
39
+ RNDK::Screen.finish
40
40
 
41
41
  puts "Cannot create the label. Is the window too small?"
42
42
  exit 1
43
43
  end
44
44
 
45
45
  # Draw the RNDK screen and waits for space to be pressed.
46
- rndkscreen.refresh
46
+ screen.refresh
47
47
  label.wait(' ')
48
48
 
49
49
  # Ending Ncurses and RNDK
50
- RNDK::Screen.end_rndk
50
+ RNDK::Screen.finish
51
51
  end
52
52
 
53
53
  # Note: Unlike C's CDK, there's no need to clean up
54
54
  # widgets/screens when finishing execution.
55
+ # You might need to do it when deleting widgets
56
+ # casually, though.
55
57
 
@@ -11,7 +11,7 @@ begin
11
11
  RNDK::Color.init
12
12
 
13
13
  # Disabling the blinking cursor
14
- Ncurses.curs_set 0
14
+ RNDK::blink_cursor true
15
15
 
16
16
  # Welcome to our Calendar Widget!
17
17
  #
@@ -25,30 +25,23 @@ begin
25
25
  #
26
26
  # RNDK::Color[:foreground]
27
27
  #
28
- # On the calendar, if we supply 0 as the
29
- # day/month/year it'll use the current day/month/year.
30
- cal = RNDK::Calendar.new(screen,
31
- RNDK::CENTER, # x
32
- RNDK::CENTER, # y
33
- "Colored calendar", # title
34
- 0, # day
35
- 0, # month
36
- 0, # year
37
- RNDK::Color[:red_yellow], # day attrib
38
- RNDK::Color[:cyan_white], # month attrib
39
- RNDK::Color[:black_cyan], # year attrib
40
- RNDK::Color[:red], # highlight
41
- true, # has box?
42
- false) # has shadow?
28
+ cal = RNDK::Calendar.new(screen, {
29
+ :x => RNDK::CENTER,
30
+ :y => RNDK::CENTER,
31
+ :title => "Colored calendar",
32
+ :day_color => RNDK::Color[:red_yellow],
33
+ :highlight => RNDK::Color[:red],
34
+ })
35
+ # Oh yeah
43
36
  cal.activate
44
37
 
45
- RNDK::Screen.end_rndk
38
+ RNDK::Screen.finish
46
39
 
47
- # Assuring we end RNDK in case something
48
- # bad happens.
49
- # You'll see this a lot on the examples.
40
+ # Assuring we end RNDK in case something
41
+ # bad happens.
42
+ # You'll see this a lot on the examples.
50
43
  rescue Exception => e
51
- RNDK::Screen.end_rndk
44
+ RNDK::Screen.finish
52
45
 
53
46
  puts e
54
47
  puts e.inspect
@@ -57,16 +57,16 @@ begin
57
57
  msg << "<C>All RNDK color pairs (press 'q' to quit)"
58
58
 
59
59
  # Show label with that huge message.
60
- label = RNDK::Label.new(screen,
61
- RNDK::CENTER, # x
62
- 1, # y
63
- msg, # message
64
- true, # has box?
65
- true) # has shadow?
60
+ label = RNDK::Label.new(screen, {
61
+ :x => RNDK::CENTER,
62
+ :y => 1,
63
+ :text => msg,
64
+ :shadow => true
65
+ })
66
66
 
67
67
  screen.refresh
68
68
  label.wait('q') # wait for key to be pressed
69
69
 
70
- RNDK::Screen.end_rndk
70
+ RNDK::Screen.finish
71
71
  end
72
72
 
@@ -57,13 +57,13 @@ begin
57
57
  screen.popup_label ["You just said '#{value}'"]
58
58
 
59
59
  # Finally, the end!
60
- RNDK::Screen.end_rndk
60
+ RNDK::Screen.finish
61
61
 
62
62
  puts "...and as always, thanks for watching!"
63
63
 
64
64
  # Just in case something bad happens.
65
65
  rescue Exception => e
66
- RNDK::Screen.end_rndk
66
+ RNDK::Screen.finish
67
67
 
68
68
  puts e
69
69
  puts e.inspect
@@ -33,69 +33,88 @@
33
33
  require 'rndk/entry'
34
34
 
35
35
  begin
36
+ title = "</77>Entry"
36
37
  label = "</U/5>Position me:<!U!5> "
37
38
 
38
39
  # Set up RNDK
39
40
  curses_win = Ncurses.initscr
40
- rndkscreen = RNDK::Screen.new(curses_win)
41
+ screen = RNDK::Screen.new(curses_win)
41
42
 
42
43
  # Set up RNDK colors
43
44
  RNDK::Color.init
44
45
 
45
46
  # Create the entry field widget.
46
- entry = RNDK::Entry.new(rndkscreen,
47
- RNDK::CENTER, # x
48
- RNDK::CENTER, # y
49
- '',
50
- label,
51
- Ncurses::A_NORMAL,
52
- '.',
53
- :MIXED,
54
- 30, # w
55
- 0,
56
- 256,
57
- true,
58
- false)
47
+ entry = RNDK::Entry.new(screen, {
48
+ :x => RNDK::CENTER,
49
+ :y => RNDK::BOTTOM,
50
+ :label => label,
51
+ :title => title,
52
+ :field_width => 30
53
+ })
59
54
 
60
55
  if entry.nil?
61
- rndkscreen.destroy
62
- RNDK::Screen.end_rndk
56
+ screen.destroy
57
+ RNDK::Screen.finish
63
58
 
64
59
  puts "Cannot create the entry box. Is the window too small?"
65
60
  exit 1
66
61
  end
67
62
 
63
+ # Create a label explaining the keybindings
64
+ help = ["</77>Keybindings",
65
+ "Up Arrow Moves the widget up one row.",
66
+
67
+ "Down Arrow Moves the widget down one row.",
68
+ "Left Arrow Moves the widget left one column",
69
+ "Right Arrow Moves the widget right one column",
70
+ "1 Moves the widget down one row and left one column.",
71
+ "2 Moves the widget down one row.",
72
+ "3 Moves the widget down one row and right one column.",
73
+ "4 Moves the widget left one column.",
74
+ "5 Centers the widget both vertically and horizontally.",
75
+ "6 Moves the widget right one column",
76
+ "7 Moves the widget up one row and left one column.",
77
+ "8 Moves the widget up one row.",
78
+ "9 Moves the widget up one row and right one column.",
79
+ "t Moves the widget to the top of the screen.",
80
+ "b Moves the widget to the bottom of the screen.",
81
+ "l Moves the widget to the left of the screen.",
82
+ "r Moves the widget to the right of the screen.",
83
+ "c Centers the widget between the left and right of the window.",
84
+ "C Centers the widget between the top and bottom of the window.",
85
+ "Escape Returns the widget to its original position.",
86
+ "Return Exits the function and leaves the Widget where"]
87
+
88
+ label = RNDK::Label.new(screen, :text => help)
89
+ label.draw
90
+
68
91
  # Let the user move the widget around the window.
69
- entry.draw(entry.box)
92
+ entry.draw
70
93
  entry.position
71
94
 
72
95
  # Activate the entry field.
73
- info = entry.activate('')
96
+ info = entry.activate
74
97
 
75
98
  # Tell them what they typed.
76
99
  if entry.exit_type == :ESCAPE_HIT
77
- mesg = [
78
- "<C>You hit escape. No information passed back.",
100
+ mesg = ["<C>You hit escape. No information passed back.",
79
101
  "",
80
- "<C>Press any key to continue."
81
- ]
82
- rndkscreen.popup_label mesg
102
+ "<C>Press any key to continue."]
103
+ screen.popup_label mesg
83
104
 
84
105
  elsif entry.exit_type == :NORMAL
85
- mesg = [
86
- "<C>You typed in the following",
87
- "<C>%.*s" % [236, info], # FIXME magic number
106
+ mesg = ["<C>You typed in the following",
107
+ "<C></77>#{info}",
88
108
  "",
89
- "<C>Press any key to continue."
90
- ]
91
- rndkscreen.popup_label mesg
109
+ "<C>Press any key to continue."]
110
+ screen.popup_label mesg
92
111
  end
93
112
 
94
- RNDK::Screen.end_rndk
113
+ RNDK::Screen.finish
95
114
 
96
115
  # Just in case something bad happens.
97
116
  rescue Exception => e
98
- RNDK::Screen.end_rndk
117
+ RNDK::Screen.finish
99
118
 
100
119
  puts e
101
120
  puts e.inspect
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Shows a scrollbar with your files and keep
4
+ # asking to interrupt scrolling.
5
+ #
6
+ # It means you can set custom functions to
7
+ # execute right before and right after processing
8
+ # input on the widget.
9
+ #
10
+ # To exit press ENTER or TAB.
11
+ #
12
+ require 'rndk/scroll'
13
+
14
+ begin
15
+ screen = RNDK::Screen.new
16
+ RNDK::Color.init
17
+
18
+ # Creating scroll based on your current dir's files.
19
+ # Nothing should be new here
20
+ title = "<C></77>Your files"
21
+ list = `ls -l`.lines
22
+
23
+ scroll = RNDK::Scroll.new(screen, {
24
+ :x => RNDK::CENTER,
25
+ :y => RNDK::CENTER,
26
+ :scroll_bar => RNDK::LEFT,
27
+ :width => RNDK::Screen.width/2,
28
+ :height => RNDK::Screen.height/2,
29
+ :title => title,
30
+ :items => list,
31
+ :highlight => RNDK::Color[:green]
32
+ })
33
+
34
+ # Here comes the good stuff.
35
+ # We're assigning blocks to run before and after
36
+ # the widget processes your input.
37
+ #
38
+ # Right before processing input, it'll create
39
+ # this popup dialog.
40
+ # Based on your answer it will stop scrolling or not.
41
+ #
42
+ # That's because a if a `before_processing` block
43
+ # return false, it aborts processing the current
44
+ # char.
45
+ # If it returns true, it keeps going.
46
+ #
47
+ scroll.bind_signal(:before_input) do |key|
48
+ message = ["</73>You just pressed </74>#{Ncurses.keyname key}",
49
+ "</73>Skip current action?"]
50
+ buttons = ["Yes", "No"]
51
+
52
+ choice = screen.popup_dialog(message, buttons)
53
+
54
+ if choice == 0 then false else true end
55
+ end
56
+
57
+ # This is run after the widget processes your
58
+ # input.
59
+ # It's commented to avoid annoyance.
60
+ # Uncomment for awesomeness
61
+
62
+ #scroll.bind_signal(:after_input) { screen.popup_label "</75>You did it!" }
63
+
64
+
65
+ # Finally, we activate the widget!
66
+ scroll.activate
67
+
68
+ RNDK::Screen.finish
69
+
70
+ # Just in case something bad happens.
71
+ rescue Exception => e
72
+ RNDK::Screen.finish
73
+ puts e
74
+ puts e.inspect
75
+ puts e.backtrace
76
+ end
77
+