@@ -4,7 +4,12 @@ use menu::*;
44use pancurses:: { endwin, initscr, noecho, Input } ;
55use std:: fmt:: Write ;
66
7- const ROOT_MENU : Menu < Output > = Menu {
7+ #[ derive( Default ) ]
8+ struct Context {
9+ _inner : u32 ,
10+ }
11+
12+ const ROOT_MENU : Menu < Output , Context > = Menu {
813 label : "root" ,
914 items : & [
1015 & Item {
@@ -94,91 +99,111 @@ fn main() {
9499 window. scrollok ( true ) ;
95100 noecho ( ) ;
96101 let mut buffer = [ 0u8 ; 64 ] ;
97- let mut r = Runner :: new ( ROOT_MENU , & mut buffer, Output ( window) ) ;
102+ let mut context = Context :: default ( ) ;
103+ let mut r = Runner :: new ( ROOT_MENU , & mut buffer, Output ( window) , & mut context) ;
98104 loop {
99- match r. context . 0 . getch ( ) {
105+ match r. interface . 0 . getch ( ) {
100106 Some ( Input :: Character ( '\n' ) ) => {
101- r. input_byte ( b'\r' ) ;
107+ r. input_byte ( b'\r' , & mut context ) ;
102108 }
103109 Some ( Input :: Character ( c) ) => {
104110 let mut buf = [ 0 ; 4 ] ;
105111 for b in c. encode_utf8 ( & mut buf) . bytes ( ) {
106- r. input_byte ( b) ;
112+ r. input_byte ( b, & mut context ) ;
107113 }
108114 }
109115 Some ( Input :: KeyDC ) => break ,
110116 Some ( input) => {
111- r. context . 0 . addstr ( & format ! ( "{:?}" , input) ) ;
117+ r. interface . 0 . addstr ( & format ! ( "{:?}" , input) ) ;
112118 }
113119 None => ( ) ,
114120 }
115121 }
116122 endwin ( ) ;
117123}
118124
119- fn enter_root ( _menu : & Menu < Output > , context : & mut Output ) {
120- writeln ! ( context , "In enter_root" ) . unwrap ( ) ;
125+ fn enter_root ( _menu : & Menu < Output , Context > , interface : & mut Output , _context : & mut Context ) {
126+ writeln ! ( interface , "In enter_root" ) . unwrap ( ) ;
121127}
122128
123- fn exit_root ( _menu : & Menu < Output > , context : & mut Output ) {
124- writeln ! ( context , "In exit_root" ) . unwrap ( ) ;
129+ fn exit_root ( _menu : & Menu < Output , Context > , interface : & mut Output , _context : & mut Context ) {
130+ writeln ! ( interface , "In exit_root" ) . unwrap ( ) ;
125131}
126132
127- fn select_foo < ' a > ( _menu : & Menu < Output > , item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
128- writeln ! ( context, "In select_foo. Args = {:?}" , args) . unwrap ( ) ;
133+ fn select_foo (
134+ _menu : & Menu < Output , Context > ,
135+ item : & Item < Output , Context > ,
136+ args : & [ & str ] ,
137+ interface : & mut Output ,
138+ _context : & mut Context ,
139+ ) {
140+ writeln ! ( interface, "In select_foo. Args = {:?}" , args) . unwrap ( ) ;
129141 writeln ! (
130- context ,
142+ interface ,
131143 "a = {:?}" ,
132144 :: menu:: argument_finder( item, args, "a" )
133145 )
134146 . unwrap ( ) ;
135147 writeln ! (
136- context ,
148+ interface ,
137149 "b = {:?}" ,
138150 :: menu:: argument_finder( item, args, "b" )
139151 )
140152 . unwrap ( ) ;
141153 writeln ! (
142- context ,
154+ interface ,
143155 "verbose = {:?}" ,
144156 :: menu:: argument_finder( item, args, "verbose" )
145157 )
146158 . unwrap ( ) ;
147159 writeln ! (
148- context ,
160+ interface ,
149161 "level = {:?}" ,
150162 :: menu:: argument_finder( item, args, "level" )
151163 )
152164 . unwrap ( ) ;
153165 writeln ! (
154- context ,
166+ interface ,
155167 "no_such_arg = {:?}" ,
156168 :: menu:: argument_finder( item, args, "no_such_arg" )
157169 )
158170 . unwrap ( ) ;
159171}
160172
161- fn select_bar < ' a > ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
162- writeln ! ( context, "In select_bar. Args = {:?}" , args) . unwrap ( ) ;
173+ fn select_bar (
174+ _menu : & Menu < Output , Context > ,
175+ _item : & Item < Output , Context > ,
176+ args : & [ & str ] ,
177+ interface : & mut Output ,
178+ _context : & mut Context ,
179+ ) {
180+ writeln ! ( interface, "In select_bar. Args = {:?}" , args) . unwrap ( ) ;
163181}
164182
165- fn enter_sub ( _menu : & Menu < Output > , context : & mut Output ) {
166- writeln ! ( context , "In enter_sub" ) . unwrap ( ) ;
183+ fn enter_sub ( _menu : & Menu < Output , Context > , interface : & mut Output , _context : & mut Context ) {
184+ writeln ! ( interface , "In enter_sub" ) . unwrap ( ) ;
167185}
168186
169- fn exit_sub ( _menu : & Menu < Output > , context : & mut Output ) {
170- writeln ! ( context , "In exit_sub" ) . unwrap ( ) ;
187+ fn exit_sub ( _menu : & Menu < Output , Context > , interface : & mut Output , _context : & mut Context ) {
188+ writeln ! ( interface , "In exit_sub" ) . unwrap ( ) ;
171189}
172190
173- fn select_baz < ' a > ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
174- writeln ! ( context, "In select_baz: Args = {:?}" , args) . unwrap ( ) ;
191+ fn select_baz (
192+ _menu : & Menu < Output , Context > ,
193+ _item : & Item < Output , Context > ,
194+ args : & [ & str ] ,
195+ interface : & mut Output ,
196+ _context : & mut Context ,
197+ ) {
198+ writeln ! ( interface, "In select_baz: Args = {:?}" , args) . unwrap ( ) ;
175199}
176200
177- fn select_quux < ' a > (
178- _menu : & Menu < Output > ,
179- _item : & Item < Output > ,
201+ fn select_quux (
202+ _menu : & Menu < Output , Context > ,
203+ _item : & Item < Output , Context > ,
180204 args : & [ & str ] ,
181- context : & mut Output ,
205+ interface : & mut Output ,
206+ _context : & mut Context ,
182207) {
183- writeln ! ( context , "In select_quux: Args = {:?}" , args) . unwrap ( ) ;
208+ writeln ! ( interface , "In select_quux: Args = {:?}" , args) . unwrap ( ) ;
184209}
0 commit comments