moved all stdout up to the top

main
_ 2025-03-21 02:33:00 -05:00
parent 58d6493915
commit 4363fa1f19
1 changed files with 39 additions and 15 deletions

View File

@ -3,16 +3,19 @@ use std::{
borrow::Cow,
fs,
io::{Read as _, Write as _},
rc::Rc,
};
fn main() -> Result<()> {
let app = App::try_new()?;
let mut app = App::try_new()?;
app.run()
}
struct App {
is_terminal: bool,
lk: Option<Lk>,
paths: Vec<Cow<'static, str>>,
path_index: usize,
}
impl App {
@ -32,20 +35,32 @@ impl App {
paths
};
Ok(App { is_terminal, paths })
Ok(App { is_terminal, lk: None, paths, path_index: 0 })
}
fn run(&self) -> Result<()> {
for path in &self.paths {
let metadata = fs::metadata(&**path)?;
let mut lk = if metadata.is_dir() {
Lk::try_new_ls(path)?
} else {
Lk::try_new_cat(path)?
};
while let Some(buf) = lk.poll()? {
std::io::stdout().write_all(&buf)?;
fn poll(&mut self) -> Result<Option<Rc<[u8]>>> {
loop {
if self.path_index >= self.paths.len() {
return Ok(None);
}
let path = &self.paths[self.path_index];
match self.lk.as_mut() {
Some(lk) => if let Some(buf) = lk.poll()? {
return Ok(Some(buf));
} else {
self.lk = None;
self.path_index += 1;
}
None => {
self.lk = Some(Lk::try_new(path)?);
}
};
}
}
fn run(&mut self) -> Result<()> {
while let Some(buf) = self.poll()? {
std::io::stdout().write_all(&buf)?;
}
Ok(())
}
@ -57,6 +72,15 @@ enum Lk {
}
impl Lk {
fn try_new(path: &str) -> Result<Self> {
let metadata = fs::metadata(path)?;
if metadata.is_dir() {
Self::try_new_ls(path)
} else {
Self::try_new_cat(path)
}
}
fn try_new_cat(path: &str) -> Result<Self> {
Ok(Self::Cat(Cat::try_new(path)?))
}
@ -65,7 +89,7 @@ impl Lk {
Ok(Self::Ls(Ls::try_new(path)?))
}
fn poll(&mut self) -> Result<Option<Cow<[u8]>>> {
fn poll(&mut self) -> Result<Option<Rc<[u8]>>> {
match self {
Lk::Cat(x) => x.poll(),
Lk::Ls(x) => x.poll(),
@ -85,7 +109,7 @@ impl Cat {
Ok(Self { f, buf })
}
fn poll(&mut self) -> Result<Option<Cow<[u8]>>> {
fn poll(&mut self) -> Result<Option<Rc<[u8]>>> {
let bytes_read = self.f.read(&mut self.buf)?;
let buf = &self.buf[0..bytes_read];
if bytes_read == 0 {
@ -105,7 +129,7 @@ impl Ls {
Ok(Self { d })
}
fn poll(&mut self) -> Result<Option<Cow<[u8]>>> {
fn poll(&mut self) -> Result<Option<Rc<[u8]>>> {
let Some(entry) = self.d.next() else {
return Ok(None);
};