aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/run/builtin.rs38
-rw-r--r--src/run/mod.rs3
2 files changed, 39 insertions, 2 deletions
diff --git a/src/run/builtin.rs b/src/run/builtin.rs
index feba7f9..cdeeffe 100644
--- a/src/run/builtin.rs
+++ b/src/run/builtin.rs
@@ -1,3 +1,5 @@
+use std::{fs::OpenOptions, path::PathBuf};
+
use super::Builtin;
use crate::*;
@@ -43,8 +45,40 @@ impl Builtin for Re {
}
}
+pub struct Sink {
+ name: &'static str,
+ append: bool,
+}
+
+impl Builtin for Sink {
+ fn name(&self) -> &str {
+ self.name
+ }
+
+ fn io(
+ &self,
+ args: &[BString],
+ stdin: &mut dyn Read,
+ _stdout: &mut dyn Write,
+ ) -> std::io::Result<()> {
+ let Some(path) = args.get(0) else {
+ // TODO exit code
+ return Ok(());
+ };
+ let path = PathBuf::from(OsStr::from_bytes(path));
+ let mut file = OpenOptions::new()
+ .write(true)
+ .create(true)
+ .append(self.append)
+ .open(path)?;
+ std::io::copy(stdin, &mut file)?;
+ Ok(())
+ }
+}
+
+pub const fn sink(name: &'static str, append: bool) -> Sink {
+ Sink { name, append }
+}
// TODO
// from" => todo!("read from file"),
-// to" | b"into" => todo!("write into file"),
-// append" => todo!("append to file"),
diff --git a/src/run/mod.rs b/src/run/mod.rs
index 10df604..8eb7ad8 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -164,6 +164,9 @@ const BUILTINS: &[&'static dyn Builtin] = &[
&builtin::Clear,
#[cfg(debug_assertions)]
&builtin::Re,
+ &builtin::sink("to", false),
+ &builtin::sink("into", false),
+ &builtin::sink("append", true),
];
pub struct CommandDispatch {