mirror of
https://github.com/Noratrieb/captain.git
synced 2026-01-14 14:35:02 +01:00
Initial commit
This commit is contained in:
commit
f0ffa77500
24 changed files with 802 additions and 0 deletions
1
cog/.gitignore
vendored
Normal file
1
cog/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
cog/Cargo.toml
Normal file
7
cog/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "cog"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
shlex = "1.3.0"
|
||||
5
cog/README.md
Normal file
5
cog/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# cog
|
||||
|
||||
> Cog: Smaller war ship
|
||||
|
||||
A super simple shell
|
||||
47
cog/src/main.rs
Normal file
47
cog/src/main.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use std::io::Write;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let mut last_success = true;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
|
||||
print!("{}$ ", if last_success { "".into() } else { "[error]" });
|
||||
std::io::stdout().flush()?;
|
||||
std::io::stdin().read_line(&mut line)?;
|
||||
|
||||
if !line.trim().is_empty() {
|
||||
match exec_line(&line) {
|
||||
Ok(success) => last_success = success,
|
||||
Err(err) => {
|
||||
eprintln!("{err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn exec_line(line: &str) -> Result<bool, String> {
|
||||
let commands = shlex::split(line).ok_or_else(|| "invalid command".to_owned())?;
|
||||
if commands.len() < 1 {
|
||||
return Err("invalid command".to_owned());
|
||||
}
|
||||
|
||||
let arg0 = &commands[0];
|
||||
|
||||
if arg0 == "exit" {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
let mut cmd = std::process::Command::new(arg0);
|
||||
cmd.args(&commands[1..]);
|
||||
|
||||
let mut cmd = cmd
|
||||
.spawn()
|
||||
.map_err(|err| format!("failed to spawn {arg0}: {err}"))?;
|
||||
|
||||
let result = cmd
|
||||
.wait()
|
||||
.map_err(|err| format!("failed to wait for {arg0}: {err}"))?;
|
||||
|
||||
Ok(result.success())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue