use super::route::{Tool, resolve, run_tool};
use crate::rcli::send::write_out;
use crate::rcli::session::Session;
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct Prefs {
pub(crate) expanded: bool,
pub(crate) timing: bool,
}
const HELP: &[u8] = b"\\dt [pattern] tables\n\\di [table|pattern] indexes\n\\dv [pattern] views\n\\d name describe a table, index or view\n\\d+ name describe, with VERIFY\n\\i file run the commands in a file\n\\conninfo the connection and the server\n\\x expanded display on/off\n\\timing timing on/off\n\\<tool> [args] any tool kevy-cli --kevy runs: \\query, \\explain, \\dump --schema, ...\n";
pub(crate) fn run(s: &mut Session, line: &[u8]) {
let Some(words) = crate::rcli::splitargs::split_args(&line[1..]) else {
write_out(b"Invalid argument(s)\n");
return;
};
let Some((name, args)) = words.split_first() else {
write_out(b"Invalid command \\. Try \\? for help.\n");
return;
};
let tool = match name.as_slice() {
b"dt" => Tool::Tables,
b"di" => Tool::Indexes,
b"dv" => Tool::Views,
b"d" => Tool::Describe,
b"d+" => Tool::DescribePlus,
b"conninfo" => Tool::Status,
b"i" => return file(s, args),
b"x" => return toggle(&mut s.rds.expanded, b"Expanded display"),
b"timing" => return toggle(&mut s.rds.timing, b"Timing"),
b"?" => return write_out(HELP),
_ => match resolve(name, args) {
Some((tool, rest)) => {
run_tool(s, tool, rest);
return;
}
None => {
return write_out(
&[&b"Invalid command \\"[..], name, b". Try \\? for help.\n"].concat(),
);
}
},
};
run_tool(s, tool, args);
}
fn file(s: &mut Session, args: &[Vec<u8>]) {
let Some(path) = args.first() else {
write_out(b"\\i: missing required argument\n");
return;
};
run_tool(s, Tool::Run, &[b"-f".to_vec(), path.clone()]);
}
fn toggle(flag: &mut bool, what: &[u8]) {
*flag = !*flag;
write_out(&[what, if *flag { &b" is on.\n"[..] } else { b" is off.\n" }].concat());
}