@@ -25,6 +25,18 @@ pub static LOAD_ITEM: menu::Item<Ctx> = menu::Item {
2525 help : Some ( "Load a file into the application area" ) ,
2626} ;
2727
28+ pub static TYPE_ITEM : menu:: Item < Ctx > = menu:: Item {
29+ item_type : menu:: ItemType :: Callback {
30+ function : typefn,
31+ parameters : & [ menu:: Parameter :: Mandatory {
32+ parameter_name : "file" ,
33+ help : Some ( "The file to type" ) ,
34+ } ] ,
35+ } ,
36+ command : "type" ,
37+ help : Some ( "Type a file to the console" ) ,
38+ } ;
39+
2840/// Called when the "dir" command is executed.
2941fn dir ( _menu : & menu:: Menu < Ctx > , _item : & menu:: Item < Ctx > , _args : & [ & str ] , _ctx : & mut Ctx ) {
3042 fn work ( ) -> Result < ( ) , embedded_sdmmc:: Error < bios:: Error > > {
@@ -99,3 +111,41 @@ fn load(_menu: &menu::Menu<Ctx>, _item: &menu::Item<Ctx>, args: &[&str], ctx: &m
99111 }
100112 }
101113}
114+
115+ /// Called when the "type" command is executed.
116+ fn typefn ( _menu : & menu:: Menu < Ctx > , _item : & menu:: Item < Ctx > , args : & [ & str ] , ctx : & mut Ctx ) {
117+ fn work ( ctx : & mut Ctx , filename : & str ) -> Result < ( ) , embedded_sdmmc:: Error < bios:: Error > > {
118+ let bios_block = crate :: fs:: BiosBlock ( ) ;
119+ let time = crate :: fs:: BiosTime ( ) ;
120+ let mut mgr = embedded_sdmmc:: VolumeManager :: new ( bios_block, time) ;
121+ // Open the first partition
122+ let volume = mgr. open_volume ( VolumeIdx ( 0 ) ) ?;
123+ let root_dir = mgr. open_root_dir ( volume) ?;
124+ let file = mgr. open_file_in_dir ( root_dir, filename, embedded_sdmmc:: Mode :: ReadOnly ) ?;
125+ let buffer = ctx. tpa . as_slice_u8 ( ) ;
126+ let count = mgr. read ( file, buffer) ?;
127+ if count != mgr. file_length ( file) ? as usize {
128+ osprintln ! ( "File too large! Max {} bytes allowed." , buffer. len( ) ) ;
129+ return Ok ( ( ) ) ;
130+ }
131+ let Ok ( s) = core:: str:: from_utf8 ( & buffer[ 0 ..count] ) else {
132+ osprintln ! ( "File is not valid UTF-8" ) ;
133+ return Ok ( ( ) ) ;
134+ } ;
135+ osprintln ! ( "{}" , s) ;
136+ Ok ( ( ) )
137+ }
138+
139+ // index can't panic - we always have enough args
140+ let r = work ( ctx, args[ 0 ] ) ;
141+ // reset SGR
142+ osprint ! ( "\u{001b} [0m" ) ;
143+ match r {
144+ Ok ( _) => { }
145+ Err ( e) => {
146+ osprintln ! ( "Error: {:?}" , e) ;
147+ }
148+ }
149+ }
150+
151+ // End of file
0 commit comments