Skip to content

Commit e55f281

Browse files
committed
Add "type" commands.
Prints UTF-8 files to the console, if they fit in the TPA.
1 parent f2d069a commit e55f281

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/fs.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2941
fn 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

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub static OS_MENU: menu::Menu<Ctx> = menu::Menu {
2727
&ram::RUN_ITEM,
2828
&ram::LOAD_ITEM,
2929
&fs::LOAD_ITEM,
30+
&fs::TYPE_ITEM,
3031
&screen::CLS_ITEM,
3132
&screen::MODE_ITEM,
3233
&input::KBTEST_ITEM,

0 commit comments

Comments
 (0)