Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
e177b26348 | |||
2dbc4d6a28 | |||
0a5d3e11e4 | |||
5961309bab | |||
797f401cf3 | |||
3ed8234852 | |||
f73c10d6eb | |||
1d50cd25d5 | |||
65f97a673b | |||
dab3905078 |
16
Cargo.lock
generated
Normal file
16
Cargo.lock
generated
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jshell"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"shell_completion",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shell_completion"
|
||||||
|
version = "0.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73937c192504363290613e241705a02dff92ae7c03f544e2a69bbef24cc1042c"
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
shell_completion = "0.0.2"
|
||||||
|
6
renovate.json
Normal file
6
renovate.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"local>Renovate/renovate-config"
|
||||||
|
]
|
||||||
|
}
|
74
src/main.rs
74
src/main.rs
@ -1,3 +1,75 @@
|
|||||||
|
use std::{env, io::{stdin, stdout, Write}, path::Path, process::{Child, Command, Stdio}};
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
println!("Hello, world!");
|
loop {
|
||||||
|
println!("");
|
||||||
|
println!("{}", env::current_dir().unwrap().to_str().unwrap());
|
||||||
|
print!("> ");
|
||||||
|
let _ = stdout().flush();
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
stdin().read_line(&mut input).unwrap();
|
||||||
|
|
||||||
|
// must be peekable so we know when we are on the last command
|
||||||
|
let mut commands = input.trim().split(" | ").peekable();
|
||||||
|
let mut previous_command = None;
|
||||||
|
|
||||||
|
while let Some(command) = commands.next() {
|
||||||
|
|
||||||
|
let mut parts = command.trim().split_whitespace();
|
||||||
|
let command = parts.next().unwrap();
|
||||||
|
let args = parts;
|
||||||
|
|
||||||
|
match command {
|
||||||
|
"cd" => {
|
||||||
|
let new_dir = args.peekable().peek()
|
||||||
|
.map_or("/", |x| *x);
|
||||||
|
let root = Path::new(new_dir);
|
||||||
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_command = None;
|
||||||
|
},
|
||||||
|
"exit" => return,
|
||||||
|
command => {
|
||||||
|
let stdin = previous_command
|
||||||
|
.map_or(
|
||||||
|
Stdio::inherit(),
|
||||||
|
|output: Child| Stdio::from(output.stdout.unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
|
let stdout = if commands.peek().is_some() {
|
||||||
|
// there is another command piped behind this one
|
||||||
|
// prepare to send output to the next command
|
||||||
|
Stdio::piped()
|
||||||
|
} else {
|
||||||
|
// there are no more commands piped behind this one
|
||||||
|
// send output to shell stdout
|
||||||
|
Stdio::inherit()
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = Command::new(command)
|
||||||
|
.args(args)
|
||||||
|
.stdin(stdin)
|
||||||
|
.stdout(stdout)
|
||||||
|
.spawn();
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(output) => { previous_command = Some(output); },
|
||||||
|
Err(e) => {
|
||||||
|
previous_command = None;
|
||||||
|
eprintln!("{}", e);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(mut final_command) = previous_command {
|
||||||
|
// block until the final command has finished
|
||||||
|
let _ = final_command.wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user