11extern crate menu;
22
3+ use embedded_io:: Write ;
4+
35use menu:: * ;
46use pancurses:: { endwin, initscr, noecho, Input } ;
5- use std:: fmt:: Write ;
6-
7- #[ derive( Default ) ]
8- struct Context {
9- _inner : u32 ,
10- }
117
12- const ROOT_MENU : Menu < Output , Context > = Menu {
8+ const ROOT_MENU : Menu < Output > = Menu {
139 label : "root" ,
1410 items : & [
1511 & Item {
@@ -85,11 +81,37 @@ It contains multiple paragraphs and should be preceeded by the parameter list.
8581 exit : Some ( exit_root) ,
8682} ;
8783
88- struct Output ( pancurses:: Window ) ;
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+ }
89113
90- impl std:: fmt:: Write for Output {
91- fn write_str ( & mut self , s : & str ) -> Result < ( ) , std:: fmt:: Error > {
92- self . 0 . printw ( s) ;
114+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
93115 Ok ( ( ) )
94116 }
95117}
@@ -99,111 +121,94 @@ fn main() {
99121 window. scrollok ( true ) ;
100122 noecho ( ) ;
101123 let mut buffer = [ 0u8 ; 64 ] ;
102- let mut context = Context :: default ( ) ;
103- let mut r = Runner :: new ( ROOT_MENU , & mut buffer, Output ( window) , & mut context) ;
124+ let mut context = Output {
125+ window,
126+ input : Vec :: new ( ) ,
127+ } ;
128+ let mut r = Runner :: new ( ROOT_MENU , & mut buffer, & mut context) ;
104129 loop {
105- match r . interface . 0 . getch ( ) {
130+ match context . window . getch ( ) {
106131 Some ( Input :: Character ( '\n' ) ) => {
107- r . input_byte ( b'\r' , & mut context ) ;
132+ context . input . push ( b'\r' ) ;
108133 }
109134 Some ( Input :: Character ( c) ) => {
110135 let mut buf = [ 0 ; 4 ] ;
111- for b in c. encode_utf8 ( & mut buf) . bytes ( ) {
112- r. input_byte ( b, & mut context) ;
113- }
136+ context
137+ . input
138+ . extend_from_slice ( c. encode_utf8 ( & mut buf) . as_bytes ( ) ) ;
139+ r. process ( & mut context) ;
114140 }
115141 Some ( Input :: KeyDC ) => break ,
116142 Some ( input) => {
117- r. interface . 0 . addstr ( & format ! ( "{:?}" , input) ) ;
143+ context
144+ . input
145+ . extend_from_slice ( format ! ( "{:?}" , input) . as_bytes ( ) ) ;
118146 }
119147 None => ( ) ,
120148 }
149+ r. process ( & mut context) ;
121150 }
122151 endwin ( ) ;
123152}
124153
125- fn enter_root ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
126- writeln ! ( interface , "In enter_root" ) . unwrap ( ) ;
154+ fn enter_root ( _menu : & Menu < Output > , context : & mut Output ) {
155+ writeln ! ( context , "In enter_root" ) . unwrap ( ) ;
127156}
128157
129- fn exit_root ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
130- writeln ! ( interface , "In exit_root" ) . unwrap ( ) ;
158+ fn exit_root ( _menu : & Menu < Output > , context : & mut Output ) {
159+ writeln ! ( context , "In exit_root" ) . unwrap ( ) ;
131160}
132161
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 ( ) ;
162+ fn select_foo ( _menu : & Menu < Output > , item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
163+ writeln ! ( context, "In select_foo. Args = {:?}" , args) . unwrap ( ) ;
141164 writeln ! (
142- interface ,
165+ context ,
143166 "a = {:?}" ,
144167 :: menu:: argument_finder( item, args, "a" )
145168 )
146169 . unwrap ( ) ;
147170 writeln ! (
148- interface ,
171+ context ,
149172 "b = {:?}" ,
150173 :: menu:: argument_finder( item, args, "b" )
151174 )
152175 . unwrap ( ) ;
153176 writeln ! (
154- interface ,
177+ context ,
155178 "verbose = {:?}" ,
156179 :: menu:: argument_finder( item, args, "verbose" )
157180 )
158181 . unwrap ( ) ;
159182 writeln ! (
160- interface ,
183+ context ,
161184 "level = {:?}" ,
162185 :: menu:: argument_finder( item, args, "level" )
163186 )
164187 . unwrap ( ) ;
165188 writeln ! (
166- interface ,
189+ context ,
167190 "no_such_arg = {:?}" ,
168191 :: menu:: argument_finder( item, args, "no_such_arg" )
169192 )
170193 . unwrap ( ) ;
171194}
172195
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 ( ) ;
196+ fn select_bar ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
197+ writeln ! ( context, "In select_bar. Args = {:?}" , args) . unwrap ( ) ;
181198}
182199
183- fn enter_sub ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
184- writeln ! ( interface , "In enter_sub" ) . unwrap ( ) ;
200+ fn enter_sub ( _menu : & Menu < Output > , context : & mut Output ) {
201+ writeln ! ( context , "In enter_sub" ) . unwrap ( ) ;
185202}
186203
187- fn exit_sub ( _menu : & Menu < Output , Context > , _context : & mut Context , interface : & mut Output ) {
188- writeln ! ( interface , "In exit_sub" ) . unwrap ( ) ;
204+ fn exit_sub ( _menu : & Menu < Output > , context : & mut Output ) {
205+ writeln ! ( context , "In exit_sub" ) . unwrap ( ) ;
189206}
190207
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 ( ) ;
208+ fn select_baz ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
209+ writeln ! ( context, "In select_baz: Args = {:?}" , args) . unwrap ( ) ;
199210}
200211
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 ( ) ;
212+ fn select_quux ( _menu : & Menu < Output > , _item : & Item < Output > , args : & [ & str ] , context : & mut Output ) {
213+ writeln ! ( context, "In select_quux: Args = {:?}" , args) . unwrap ( ) ;
209214}
0 commit comments