reorganizing extension commands
parent
3f6eaa8e61
commit
0a0dfc2ee9
@ -0,0 +1,5 @@
|
||||
mod tail;
|
||||
mod command;
|
||||
|
||||
pub use tail::Tail;
|
||||
pub use command::Command;
|
@ -0,0 +1,7 @@
|
||||
pub trait Command {
|
||||
fn name() -> String;
|
||||
|
||||
fn create() -> Self;
|
||||
|
||||
fn exec(&mut self, args: Vec<&str>) -> anyhow::Result<bool>;
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
use crate::{
|
||||
ext::Command,
|
||||
output,
|
||||
};
|
||||
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Seek, SeekFrom, Read, Write},
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct Tail { }
|
||||
|
||||
impl Command for Tail {
|
||||
fn name() -> String {
|
||||
String::from("tail")
|
||||
}
|
||||
|
||||
fn create() -> Self {
|
||||
Self{ }
|
||||
}
|
||||
|
||||
fn exec(&mut self, args: Vec<&str>) -> Result<bool> {
|
||||
let mut stdout = output::Writer::stdout()?;
|
||||
if args.len() > 0 {
|
||||
let fname = args[0];
|
||||
match File::options().read(true).open(fname) {
|
||||
Ok(mut f) => {
|
||||
_ = f.seek(SeekFrom::End(0));
|
||||
let mut one_byte: [u8; 1] = [0; 1];
|
||||
let mut buf: Vec<u8> = vec![];
|
||||
loop {
|
||||
match f.read(&mut one_byte) {
|
||||
Ok(n) => {
|
||||
if n == 1 {
|
||||
buf.push(one_byte[0]);
|
||||
if let Ok(s) = std::str::from_utf8(&buf) {
|
||||
stdout.write(s.as_bytes())?;
|
||||
buf.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("failed to open file: {}", e);
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("need a file name");
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue