11extern crate menu;
22
3- use embedded_io:: Write ;
4-
53use menu:: * ;
64use pancurses:: { endwin, initscr, noecho, Input } ;
5+ use std:: fmt:: Write ;
6+
7+ #[ derive( Default ) ]
8+ struct Context {
9+ _inner : u32 ,
10+ }
711
8- const ROOT_MENU : Menu < Output > = Menu {
12+ const ROOT_MENU : Menu < Output , Context > = Menu {
913 label : "root" ,
1014 items : & [
1115 & Item {
@@ -81,37 +85,11 @@ It contains multiple paragraphs and should be preceeded by the parameter list.
8185 exit : Some ( exit_root) ,
8286} ;
8387
84- struct Output {
85- window : pancurses:: Window ,
86- input : Vec < u8 > ,
87- }
88-
89- impl embedded_io:: ErrorType for Output {
90- type Error = core:: convert:: Infallible ;
91- }
92-
93- impl embedded_io:: ReadReady for Output {
94- fn read_ready ( & mut self ) -> Result < bool , Self :: Error > {
95- Ok ( !self . input . is_empty ( ) )
96- }
97- }
98-
99- impl embedded_io:: Read for Output {
100- fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
101- let len = ( & self . input [ ..] ) . read ( buf) . unwrap ( ) ;
102- self . input . drain ( ..len) ;
103- Ok ( len)
104- }
105- }
106-
107- impl embedded_io:: Write for Output {
108- fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
109- let string = String :: from_utf8 ( buf. to_vec ( ) ) . unwrap ( ) ;
110- self . window . printw ( string) ;
111- Ok ( buf. len ( ) )
112- }
88+ struct Output ( pancurses:: Window ) ;
11389
114- fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
90+ impl std:: fmt:: Write for Output {
91+ fn write_str ( & mut self , s : & str ) -> Result < ( ) , std:: fmt:: Error > {
92+ self . 0 . printw ( s) ;
11593 Ok ( ( ) )
11694 }
11795}
@@ -121,94 +99,111 @@ fn main() {
12199 window. scrollok ( true ) ;
122100 noecho ( ) ;
123101 let mut buffer = [ 0u8 ; 64 ] ;
124- let mut context = Output {
125- window,
126- input : Vec :: new ( ) ,
127- } ;
128- let mut r = Runner :: new ( ROOT_MENU , & mut buffer, & mut context) ;
102+ let mut context = Context :: default ( ) ;
103+ let mut r = Runner :: new ( ROOT_MENU , & mut buffer, Output ( window) , & mut context) ;
129104 loop {
130- match context . window . getch ( ) {
105+ match r . interface . 0 . getch ( ) {
131106 Some ( Input :: Character ( '\n' ) ) => {
132- context . input . push ( b'\r' ) ;
107+ r . input_byte ( b'\r' , & mut context ) ;
133108 }
134109 Some ( Input :: Character ( c) ) => {
135110 let mut buf = [ 0 ; 4 ] ;
136- context
137- . input
138- . extend_from_slice ( c. encode_utf8 ( & mut buf) . as_bytes ( ) ) ;
139- r. process ( & mut context) ;
111+ for b in c. encode_utf8 ( & mut buf) . bytes ( ) {
112+ r. input_byte ( b, & mut context) ;
113+ }
140114 }
141115 Some ( Input :: KeyDC ) => break ,
142116 Some ( input) => {
143- context
144- . input
145- . extend_from_slice ( format ! ( "{:?}" , input) . as_bytes ( ) ) ;
117+ r. interface . 0 . addstr ( & format ! ( "{:?}" , input) ) ;
146118 }
147119 None => ( ) ,
148120 }
149- r. process ( & mut context) ;
150121 }
151122 endwin ( ) ;
152123}
153124
154- fn enter_root ( _menu : & Menu < Output > , context : & mut Output ) {
155- writeln ! ( context , "In enter_root" ) . unwrap ( ) ;
125+ fn enter_root ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
126+ writeln ! ( interface , "In enter_root" ) . unwrap ( ) ;
156127}
157128
158- fn exit_root ( _menu : & Menu < Output > , context : & mut Output ) {
159- writeln ! ( context , "In exit_root" ) . unwrap ( ) ;
129+ fn exit_root ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
130+ writeln ! ( interface , "In exit_root" ) . unwrap ( ) ;
160131}
161132
162- fn select_foo ( _menu : & Menu < Output > , item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
163- 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+ _context : & mut Context ,
138+ interface : & mut Output ,
139+ ) {
140+ writeln ! ( interface, "In select_foo. Args = {:?}" , args) . unwrap ( ) ;
164141 writeln ! (
165- context ,
142+ interface ,
166143 "a = {:?}" ,
167144 :: menu:: argument_finder( item, args, "a" )
168145 )
169146 . unwrap ( ) ;
170147 writeln ! (
171- context ,
148+ interface ,
172149 "b = {:?}" ,
173150 :: menu:: argument_finder( item, args, "b" )
174151 )
175152 . unwrap ( ) ;
176153 writeln ! (
177- context ,
154+ interface ,
178155 "verbose = {:?}" ,
179156 :: menu:: argument_finder( item, args, "verbose" )
180157 )
181158 . unwrap ( ) ;
182159 writeln ! (
183- context ,
160+ interface ,
184161 "level = {:?}" ,
185162 :: menu:: argument_finder( item, args, "level" )
186163 )
187164 . unwrap ( ) ;
188165 writeln ! (
189- context ,
166+ interface ,
190167 "no_such_arg = {:?}" ,
191168 :: menu:: argument_finder( item, args, "no_such_arg" )
192169 )
193170 . unwrap ( ) ;
194171}
195172
196- fn select_bar ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
197- 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+ _context : & mut Context ,
178+ interface : & mut Output ,
179+ ) {
180+ writeln ! ( interface, "In select_bar. Args = {:?}" , args) . unwrap ( ) ;
198181}
199182
200- fn enter_sub ( _menu : & Menu < Output > , context : & mut Output ) {
201- writeln ! ( context , "In enter_sub" ) . unwrap ( ) ;
183+ fn enter_sub ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
184+ writeln ! ( interface , "In enter_sub" ) . unwrap ( ) ;
202185}
203186
204- fn exit_sub ( _menu : & Menu < Output > , context : & mut Output ) {
205- writeln ! ( context , "In exit_sub" ) . unwrap ( ) ;
187+ fn exit_sub ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
188+ writeln ! ( interface , "In exit_sub" ) . unwrap ( ) ;
206189}
207190
208- fn select_baz ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
209- 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+ _context : & mut Context ,
196+ interface : & mut Output ,
197+ ) {
198+ writeln ! ( interface, "In select_baz: Args = {:?}" , args) . unwrap ( ) ;
210199}
211200
212- fn select_quux ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
213- writeln ! ( context, "In select_quux: Args = {:?}" , args) . unwrap ( ) ;
201+ fn select_quux (
202+ _menu : & Menu < Output , Context > ,
203+ _item : & Item < Output , Context > ,
204+ args : & [ & str ] ,
205+ _context : & mut Context ,
206+ interface : & mut Output ,
207+ ) {
208+ writeln ! ( interface, "In select_quux: Args = {:?}" , args) . unwrap ( ) ;
214209}
0 commit comments