This commit is contained in:
♥ Minnie ♥ 2025-06-07 15:04:26 +08:00
parent 28afb92461
commit 4eb0c39a66
Signed by: jasmine
GPG key ID: 8563E358D4E8040E
2 changed files with 47 additions and 45 deletions

38
src/core.rs Normal file
View file

@ -0,0 +1,38 @@
use std::io::{self, Write};
use std::process::Command;
pub fn display_prompt() {
print!("> ");
io::stdout().flush().expect("unable to flush buffer");
}
pub fn get_input() -> String {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("failed to readline");
// Remove newline character from the input.
input.pop();
input
}
pub fn parse_input(s: &str) -> Vec<&str> {
s.split_whitespace().collect()
}
pub fn get_command(tokens: Vec<&str>) -> &str {
tokens[0]
}
pub fn get_args(tokens: Vec<&str>) -> Vec<&str> {
tokens[1..].to_vec()
}
pub fn execute(cmd: &str, args: Vec<&str>) {
let status = Command::new(cmd)
.args(args)
.status()
.expect("failed to execute process");
assert!(status.success());
}

View file

@ -1,52 +1,16 @@
use std::io::{self, Write};
use std::process::Command;
fn display_prompt() {
print!("> ");
io::stdout().flush().expect("unable to flush buffer");
}
fn get_input() -> String {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("failed to readline");
// Remove newline character from the input.
input.pop();
input
}
fn parse_input(s: &str) -> Vec<&str> {
s.split_whitespace().collect()
}
fn get_command(tokens: Vec<&str>) -> &str {
tokens[0]
}
fn get_args(tokens: Vec<&str>) -> Vec<&str> {
tokens[1..].to_vec()
}
fn run_command(cmd: &str, args: Vec<&str>) {
let status = Command::new(cmd)
.args(args)
.status()
.expect("failed to execute process");
assert!(status.success());
}
mod core;
mod inbuilt;
fn main() {
loop {
// Display the prompt for the user.
display_prompt();
core::display_prompt();
// Get the users input.
let input = get_input();
let input = core::get_input();
// Parse the users input and create tokens.
let tokens = parse_input(&input);
let tokens = core::parse_input(&input);
// If the user enters empty input don't execute the command.
if tokens.is_empty() {
@ -54,10 +18,10 @@ fn main() {
}
// Convert our tokens to our command and arguments.
let cmd = get_command(tokens.clone());
let args = get_args(tokens);
let cmd = core::get_command(tokens.clone());
let args = core::get_args(tokens);
// Run the command and arguments.
run_command(cmd, args);
// Execute command and argument.
core::execute(cmd, args);
}
}