chore: refactor

This commit is contained in:
♥ Minnie ♥ 2025-06-09 10:37:09 +08:00
parent cb8bbbf414
commit 5fc7e98f23
Signed by: jasmine
GPG key ID: 8563E358D4E8040E
2 changed files with 21 additions and 16 deletions

View file

@ -16,7 +16,7 @@ pub fn get_input() -> String {
input input
} }
pub fn parse_input(s: &str) -> Vec<String> { pub fn tokenize(s: &str) -> Vec<String> {
shlex::split(s).unwrap() shlex::split(s).unwrap()
} }

View file

@ -1,5 +1,8 @@
mod core; mod core;
use crate::core::*;
mod inbuilt; mod inbuilt;
use crate::inbuilt::*;
use mlua::prelude::*; use mlua::prelude::*;
@ -29,36 +32,38 @@ fn main() {
// Read, Evaluate, Print and Loop. // Read, Evaluate, Print and Loop.
loop { loop {
// Display the prompt for the user. // Display the prompt for the user.
core::display_prompt(); display_prompt();
// Get the users input. // Get the users input.
let input = core::get_input(); let input = get_input();
// Parse the users input and create tokens. // Tokenize the user input.
let tokens = core::parse_input(&input); let tokens = tokenize(&input);
// If the user enters empty input don't execute the command. // If the user enters empty input skip to the next iteration.
if tokens.is_empty() { if tokens.is_empty() {
continue; continue;
} }
// Convert tokens into command and arguments. // Convert tokens into command and arguments.
let (mut cmd, mut args) = (core::get_command(&tokens), core::get_args(&tokens)); let (mut cmd, mut args) = (get_command(&tokens), get_args(&tokens));
// Check our configuration for aliases, convert alias to corresponding command. // Check our configuration for aliases, convert alias to corresponding command and
// arguments.
if let Some(alias_cmd) = resolve_alias(&config, &cmd) { if let Some(alias_cmd) = resolve_alias(&config, &cmd) {
// Tokenize the alias command and update cmd and args accordingly. // Tokenize the alias command and update cmd and args accordingly.
let alias_tokens = core::parse_input(&alias_cmd); let alias_tokens = tokenize(&alias_cmd);
cmd = core::get_command(&alias_tokens); cmd = get_command(&alias_tokens);
args.extend(core::get_args(&alias_tokens)); args.extend(get_args(&alias_tokens));
} }
// Execute inbuilt commands if supplied, otherwise execute command and argument. // Execute inbuilt command if match found, otherwise execute external command with
// arguments.
match cmd.as_str() { match cmd.as_str() {
"cd" => inbuilt::cd(&args), "cd" => cd(&args),
"help" => inbuilt::help(), "help" => help(),
"exit" => inbuilt::exit(), "exit" => exit(),
_ => core::execute(&cmd, &args), _ => execute(&cmd, &args),
} }
} }
} }