ppcurses 0.0.7 → 0.0.8

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/README.md CHANGED
@@ -1 +1,6 @@
1
- blah
1
+ # PPCurses #
2
+
3
+ Some convenience code to initialize curses and some rudimentary GUI classes. See the **test** directory for example usage programs.
4
+
5
+ # References #
6
+ http://www.ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses.html
@@ -0,0 +1,7 @@
1
+ require "curses"
2
+
3
+ module PPCurses
4
+ class BaseAction
5
+
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ require "curses"
2
+
3
+ class GetBooleanAction < PromptAction
4
+
5
+ def initialize(prompt)
6
+ super(prompt)
7
+ @state = false;
8
+ end
9
+
10
+ def printPrompt()
11
+ super()
12
+ @win.addstr("No [")
13
+ if (@state == false) then @win.addstr("X") else @win.addstr(" ") end
14
+ @win.addstr("] Yes [")
15
+ if (@state == true) then @win.addstr("X") else @win.addstr(" ") end
16
+ @win.addstr("]")
17
+ end
18
+
19
+
20
+ def execute()
21
+ printPrompt()
22
+ # Enables reading arrow keys in getch
23
+ @win.keypad(true)
24
+ while(1)
25
+ noecho
26
+ c = @win.getch
27
+
28
+ if c == KEY_LEFT then @state = false end
29
+ if c == KEY_RIGHT then @state = true end
30
+ if c == 10 then break end
31
+
32
+ echo
33
+ printPrompt()
34
+ end
35
+ echo
36
+ # Go to next line so that further actions to overwrite
37
+ # the choice
38
+ @win.setpos(@win.cury() + 1, @parent.winPadding())
39
+ end
40
+
41
+ def data
42
+ if @state == false then return "0" end
43
+ "1"
44
+ end
45
+
46
+ end
47
+
@@ -0,0 +1,120 @@
1
+ require "curses"
2
+
3
+ class GetDataAction < BaseAction
4
+
5
+ def initialize( actions )
6
+ @actions = actions
7
+ unless @actions.nil?
8
+ @actions.each do |action|
9
+ action.setParentAction(self)
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ def beforeActions()
16
+ # Stub for classes that extend
17
+ end
18
+
19
+ def afterActions()
20
+ # Stub for classes that extend
21
+ end
22
+
23
+ def winPadding()
24
+ return 2
25
+ end
26
+
27
+ def winWidth()
28
+ Curses.cols - winPadding()
29
+ end
30
+
31
+ def winHeight()
32
+ Curses.lines - winPadding()
33
+ end
34
+
35
+ def createWindow()
36
+ @win = Window.new( winHeight(), winWidth(),
37
+ winPadding()/2, winPadding()/2)
38
+
39
+ # Assign window to actions
40
+ unless @actions.nil?
41
+ @actions.each do |action|
42
+ action.setWindow(@win)
43
+ end
44
+ end
45
+ @win.clear
46
+ @win.box("|", "-")
47
+ end
48
+
49
+ def execute()
50
+ createWindow()
51
+ echo
52
+
53
+ y = @win.cury + 1
54
+ @win.setpos(y,winPadding())
55
+
56
+ self.beforeActions()
57
+
58
+ @actions.each do |action|
59
+ action.execute
60
+ #y = @win.cury + 1
61
+ #@win.setpos(y,winPadding())
62
+ end
63
+
64
+ self.afterActions()
65
+
66
+ noecho
67
+ @win.clear
68
+ @win.refresh
69
+ @win.close
70
+ end
71
+
72
+ def printLine(string)
73
+ @win.setpos(@win.cury(), winPadding())
74
+ @win.addstr(string)
75
+ @win.setpos(@win.cury() + 1, winPadding())
76
+ end
77
+
78
+ def printSuccessLine(string)
79
+ init_pair(1, COLOR_GREEN, COLOR_BLACK)
80
+ @win.attron(color_pair(1))
81
+ self.printLine(string)
82
+ @win.attroff(color_pair(1))
83
+ end
84
+
85
+ def printErrorLine(string)
86
+ init_pair(1, COLOR_RED, COLOR_BLACK)
87
+ @win.attron(color_pair(1))
88
+ self.printLine(string)
89
+ @win.attroff(color_pair(1))
90
+ end
91
+
92
+ def promptToChangeData(preparedSQL)
93
+ self.printLine(preparedSQL)
94
+
95
+ @win.addstr("Proceed? ")
96
+ echo
97
+ c = @win.getch()
98
+ noecho
99
+
100
+ if c == "y" or c == "Y" then
101
+ self.printLine("")
102
+ begin
103
+ @db.execute preparedSQL
104
+ self.printSuccessLine("Execution successful")
105
+ rescue SQLite3::Exception => e
106
+ self.printErrorLine("Exception occurred")
107
+ self.printErrorLine(e.message)
108
+ ensure
109
+ self.printLine("")
110
+ self.printLine("< Press any key to continue > ")
111
+ @win.getch()
112
+ end
113
+
114
+ end
115
+
116
+
117
+ end
118
+
119
+ end
120
+
@@ -0,0 +1,60 @@
1
+ require "curses"
2
+
3
+ class GetEnumeratedStringAction < PromptAction
4
+
5
+ # enumeration is a list of possible values
6
+ # i.e. CD, Vinyl, MP3
7
+ def initialize(prompt, enumeration)
8
+ super(prompt)
9
+
10
+ # verify enumeration is an array
11
+ if ( enumeration.respond_to?('each_with_index') == false ) then
12
+ raise
13
+ end
14
+ @options = enumeration
15
+ @currOption = 0
16
+ end
17
+
18
+ def printPrompt()
19
+ super()
20
+ @options.each_with_index do |option, index|
21
+ @win.addstr(option)
22
+ if ( index == @currOption ) then
23
+ @win.addstr(" [X] ")
24
+ else
25
+ @win.addstr(" [ ] ")
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ def execute()
32
+ printPrompt()
33
+ # Enables reading arrow keys in getch
34
+ @win.keypad(true)
35
+ while(1)
36
+ noecho
37
+ c = @win.getch
38
+
39
+ if c == KEY_LEFT then @currOption = @currOption-1 end
40
+ if c == KEY_RIGHT then @currOption= @currOption+1 end
41
+ if c == 10 then break end
42
+
43
+ if (@currOption < 0 ) then @currOption = @options.length-1 end
44
+ if (@currOption > @options.length-1 ) then @currOption = 0 end
45
+
46
+ echo
47
+ printPrompt()
48
+ end
49
+ echo
50
+ # Go to next line so that further actions to overwrite
51
+ # the choice
52
+ @win.setpos(@win.cury() + 1, @parent.winPadding())
53
+ end
54
+
55
+ def data
56
+ @options[@currOption]
57
+ end
58
+
59
+ end
60
+
@@ -0,0 +1,17 @@
1
+ require "curses"
2
+
3
+ class GetIntegerAction < PromptAction
4
+
5
+ def execute()
6
+ x = @parent.winPadding()
7
+ y = @win.cury()
8
+ begin
9
+ @win.setpos(y,x)
10
+ @win.clrtoeol()
11
+ @win.addstr(@prompt)
12
+ @data = @win.getstr()
13
+ end while not @data =~ /^\d+$/
14
+ end
15
+
16
+ end
17
+
@@ -0,0 +1,5 @@
1
+ require "curses"
2
+
3
+ class GetStringAction < PromptAction
4
+ end
5
+
@@ -0,0 +1,25 @@
1
+ require "curses"
2
+
3
+
4
+ class InsertSQLDataAction < GetDataAction
5
+
6
+ def initialize( actions , sql, db )
7
+ super(actions)
8
+ @sql = sql
9
+ @db = db
10
+ end
11
+
12
+ def winHeight()
13
+ return 7 + @actions.length
14
+ end
15
+
16
+ def afterActions()
17
+ preparedSql = @sql
18
+ @actions.each do |action|
19
+ preparedSql = preparedSql.sub("%s", action.data)
20
+ end
21
+
22
+ self.promptToChangeData(preparedSql)
23
+ end
24
+
25
+ end
@@ -0,0 +1,46 @@
1
+ require "curses"
2
+
3
+ class LiftAction < GetDataAction
4
+
5
+ def initialize(nameMenu, repMenu, db)
6
+ @nameMenu = nameMenu
7
+ @repMenu = repMenu
8
+ @db = db
9
+
10
+ @prompt = GetIntegerAction.new("Weight (pounds) : ")
11
+ super( [ @prompt ] )
12
+
13
+ @sql = "INSERT into LIFTS(name, weight, reps) values ('%s', %s, %s)"
14
+
15
+ end
16
+
17
+ def winHeight()
18
+ return 9
19
+ end
20
+
21
+ def liftName()
22
+ @nameMenu.getSelectedMenuName()
23
+ end
24
+
25
+ def repsName()
26
+ @repMenu.getSelectedMenuName()
27
+ end
28
+
29
+ def repsInteger()
30
+ return Integer(repsName().chars.first).to_s
31
+ end
32
+
33
+ def beforeActions()
34
+ self.printLine("Input data for " + repsName() + " " + liftName() )
35
+ end
36
+
37
+ def afterActions()
38
+ preparedSql = @sql.sub("%s", liftName() )
39
+ preparedSql = preparedSql.sub("%s", @prompt.data() )
40
+ preparedSql = preparedSql.sub("%s", repsInteger() )
41
+
42
+ self.promptToChangeData(preparedSql)
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,11 @@
1
+ require "curses"
2
+
3
+ class NulAction < BaseAction
4
+ def initialize( )
5
+ end
6
+
7
+
8
+ def execute()
9
+ end
10
+
11
+ end
@@ -0,0 +1,32 @@
1
+ require "curses"
2
+
3
+
4
+ class PromptAction < BaseAction
5
+
6
+ def initialize(prompt)
7
+ @prompt = prompt
8
+ end
9
+
10
+ def setWindow(win)
11
+ @win = win
12
+ end
13
+
14
+ def setParentAction(action)
15
+ @parent = action
16
+ end
17
+
18
+ def printPrompt()
19
+ @win.setpos(@win.cury(), @parent.winPadding())
20
+ @win.addstr(@prompt)
21
+ end
22
+
23
+ def execute()
24
+ printPrompt()
25
+ @data = @win.getstr()
26
+ end
27
+
28
+ def data
29
+ @data
30
+ end
31
+ end
32
+
@@ -0,0 +1,17 @@
1
+ require 'ppcurses/actions/BaseAction.rb'
2
+
3
+ module PPCurses
4
+ class ShowMenuAction < BaseAction
5
+
6
+ def initialize( menu )
7
+ @menu = menu
8
+
9
+ end
10
+
11
+ def execute()
12
+ @menu.show()
13
+ @menu.getMenuSelection()
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+
2
+ class CompositeMenu
3
+
4
+ def initialize(menu1, menu2)
5
+ @menu1 = menu1
6
+ @menu2 = menu2
7
+
8
+ @menu1.setSubMenu(menu2)
9
+
10
+ end
11
+
12
+ def show()
13
+ @menu1.show()
14
+ @menu2.show()
15
+ end
16
+
17
+
18
+ def getMenuSelection()
19
+ @menu1.getMenuSelection()
20
+ end
21
+
22
+ end
@@ -0,0 +1,102 @@
1
+ # Curses reference:
2
+ # http://www.ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses.html
3
+
4
+ require "curses"
5
+ require './ppcurses/BaseMenu.rb'
6
+ include Curses
7
+
8
+ class RadioMenu < BaseMenu
9
+
10
+ def initialize( menuItems, actionItems )
11
+ @items = Array.new
12
+
13
+ @menuLength = 0
14
+
15
+ menuItems.each do |item|
16
+ @items.push item
17
+ @menuLength += item.length + 5
18
+ end
19
+
20
+ @selection = 0
21
+
22
+ unless actionItems.nil?
23
+ @actions = Array.new
24
+ actionItems.each do |item|
25
+ @actions.push item
26
+ end
27
+ end
28
+
29
+ winWidth = @menuLength + 4
30
+
31
+ @win = Window.new(3, winWidth ,0, (cols - winWidth) / 2)
32
+
33
+ @win.timeout=-1
34
+ # Enables reading arrow keys in getch
35
+ @win.keypad(true)
36
+ end
37
+
38
+ def show()
39
+ @win.box("|", "-")
40
+ y = 1
41
+ x = 2
42
+
43
+ @win.setpos(y, x)
44
+
45
+ for i in 0...@items.length
46
+ @win.addstr( @items[i] )
47
+ if @selection == i then @win.addstr(" [*] ") else @win.addstr(" [ ] " ) end
48
+ end
49
+
50
+ @win.refresh
51
+
52
+ end
53
+
54
+
55
+ def getMenuSelection()
56
+
57
+ while(1)
58
+ c = @win.getch
59
+
60
+ processed = self.handleMenuSelection(c)
61
+
62
+ if c == 27 then # ESCAPE
63
+ @win.clear
64
+ @win.refresh
65
+ break
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+
73
+ def handleMenuSelection(c)
74
+ n_choices = @items.length
75
+
76
+ if c == KEY_RIGHT then
77
+ if @selection < n_choices - 1 then @selection += 1 else @selection = 0 end
78
+ self.show()
79
+ end
80
+
81
+ if c == KEY_LEFT then
82
+ if @selection > 0 then @selection -= 1 else @selection = n_choices-1 end
83
+ self.show()
84
+ end
85
+
86
+ if c == 10 then # ENTER
87
+ unless @actions.nil?
88
+ @actions[@selection].execute()
89
+ self.show()
90
+ end
91
+ end
92
+ end
93
+
94
+
95
+
96
+ def close()
97
+ @win.close()
98
+ end
99
+
100
+ end
101
+
102
+
data/lib/ppcurses.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  module PPCurses
3
3
  require 'ppcurses/Screen.rb'
4
4
  require 'ppcurses/menu/Menu.rb'
5
+ require 'ppcurses/actions/ShowMenuAction.rb'
5
6
  end
6
7
 
7
8
 
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
  require 'ppcurses'
5
5
 
6
6
  def displayMenu()
7
- mainMenu = PPCurses::Menu.new( [ "1RM", "3RM", "5RM" ], nil )
7
+ mainMenu = PPCurses::Menu.new( [ "Press", "<ESCAPE>", "to Quit" ], nil )
8
8
  mainMenu.show()
9
9
  mainMenu.getMenuSelection()
10
10
  mainMenu.close()
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'ppcurses'
5
+
6
+ def displayMenu()
7
+ mainMenu = PPCurses::Menu.new( [ "Press", "<ESCAPE>", "to Quit" ], nil )
8
+ innerMenu = PPCurses::Menu.new( [ "Wow", "another", "menu" ], nil )
9
+
10
+ menuAction = ShowMenuAction.new(innerMenu)
11
+ mainMenu.setGlobalAction(menuAction)
12
+
13
+ mainMenu.show()
14
+ mainMenu.getMenuSelection()
15
+ mainMenu.close()
16
+ end
17
+
18
+ screen = PPCurses::Screen.new()
19
+ screen.run { displayMenu() }
20
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppcurses
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthieu Cormier
@@ -29,9 +29,23 @@ extra_rdoc_files: []
29
29
  files:
30
30
  - lib/ppcurses.rb
31
31
  - lib/ppcurses/menu/BaseMenu.rb
32
+ - lib/ppcurses/menu/RadioMenu.rb
32
33
  - lib/ppcurses/menu/Menu.rb
34
+ - lib/ppcurses/menu/CompositeMenu.rb
33
35
  - lib/ppcurses/Screen.rb
34
- - test/test.rb
36
+ - lib/ppcurses/actions/GetBooleanAction.rb
37
+ - lib/ppcurses/actions/PromptAction.rb
38
+ - lib/ppcurses/actions/InsertSQLDataAction.rb
39
+ - lib/ppcurses/actions/BaseAction.rb
40
+ - lib/ppcurses/actions/GetStringAction.rb
41
+ - lib/ppcurses/actions/GetDataAction.rb
42
+ - lib/ppcurses/actions/LiftAction.rb
43
+ - lib/ppcurses/actions/NulAction.rb
44
+ - lib/ppcurses/actions/GetIntegerAction.rb
45
+ - lib/ppcurses/actions/ShowMenuAction.rb
46
+ - lib/ppcurses/actions/GetEnumeratedStringAction.rb
47
+ - test/menuInMenu.rb
48
+ - test/displayMenu.rb
35
49
  - LICENCE.txt
36
50
  - README.md
37
51
  homepage: https://github.com/mcormier/ppcurses