pub mod command;
pub mod commands;
pub mod exit_status;
pub mod file_buffer;
pub mod helper;
pub mod tilde_range;
#[cfg(feature = "lua")]
pub mod lua;
use command::*;
use exit_status::*;
use file_buffer::*;
#[cfg(feature = "lua")]
use mlua::Lua;
#[derive(Default)]
pub struct EditorState {
pub buffer: FileBuffer,
pub registry: CommandRegistry,
pub prefix: String,
pub cursor: usize,
pub existing_content: String,
#[cfg(feature = "lua")]
pub lua: Option<Lua>,
}
impl EditorState {
pub fn new() -> EditorState {
EditorState {
prefix: "~".to_string(),
..Default::default()
}
}
pub fn instantiate() -> EditorState {
let mut state = EditorState::new();
state.registry = CommandRegistry::instantiate();
state
}
}
pub fn vecify_command(command: &str) -> Vec<&str> {
let mut command_args: Vec<&str> = Vec::new();
for arg in command.split_whitespace() {
command_args.push(arg);
}
command_args
}
pub fn run_sued_command(command_args: Vec<&str>, state: &mut EditorState) -> ExitStatus {
run_command(command_args, state, false)
}
#[cfg(feature = "repl")]
pub fn run_repl_command(command_args: Vec<&str>, state: &mut EditorState) -> ExitStatus {
run_command(command_args, state, true)
}
fn run_command(command_args: Vec<&str>, state: &mut EditorState, is_repl: bool) -> ExitStatus {
let command = command_args.get(0).map(|s| s.to_string());
if command.is_none() {
return ExitStatus::Failure("no command provided".to_string());
}
let command = command.unwrap();
match state.registry.get_command(&command) {
Some(command) => match command.scope {
CommandScope::Global => {
let mut action = command.action.clone();
let result = action.call(command_args, state);
ExitStatus::Success(result)
}
CommandScope::FileOnly => {
if is_repl {
ExitStatus::Failure(format!(
"{} is not available in sued as a library",
command.name
))
} else {
let mut action = command.action.clone();
let result = action.call(command_args, state);
ExitStatus::Success(result)
}
}
CommandScope::REPLOnly => {
if !is_repl || !cfg!(feature = "repl") {
ExitStatus::Failure(format!(
"{} is only available in sued as a library",
command.name
))
} else {
let mut action = command.action.clone();
let result = action.call(command_args, state);
ExitStatus::Success(result)
}
}
CommandScope::Private => {
ExitStatus::Failure(format!("{} is marked as private", command.name))
}
},
None => ExitStatus::Failure(format!("{} is not a command", command)),
}
}
pub fn process_command() -> ! {
panic!("`process_command` is removed as of sued v0.20.0; use `run_sued_command` instead");
}