1
0

Improve shell ergonomics

This commit is contained in:
Jeff 2024-01-28 12:04:33 -05:00
parent a0b38d329b
commit 3bb9090afa
5 changed files with 114 additions and 21 deletions

View File

@ -31,7 +31,7 @@ pub enum BuiltInValue {
} }
impl BuiltInValue { impl BuiltInValue {
pub const fn name(&self) -> &'static str { pub fn name(&self) -> &'static str {
match self { match self {
BuiltInValue::Args => "args", BuiltInValue::Args => "args",
BuiltInValue::AssertEqual => "assert_equal", BuiltInValue::AssertEqual => "assert_equal",
@ -44,6 +44,19 @@ impl BuiltInValue {
} }
} }
pub fn description(&self) -> &'static str {
match self {
BuiltInValue::Args => "The command line arguments sent to this program.",
BuiltInValue::AssertEqual => "Error if the two values are not equal.",
BuiltInValue::Fs => "File and directory tools.",
BuiltInValue::Json => "JSON formatting tools.",
BuiltInValue::Length => BuiltInFunction::Length.description(),
BuiltInValue::Output => "output",
BuiltInValue::Random => "random",
BuiltInValue::String => "string",
}
}
pub fn r#type(&self) -> Type { pub fn r#type(&self) -> Type {
match self { match self {
BuiltInValue::Args => Type::list(Type::String), BuiltInValue::Args => Type::list(Type::String),

View File

@ -25,6 +25,14 @@ impl Callable for Json {
} }
} }
fn description(&self) -> &'static str {
match self {
Json::Create => "Convert a value to a JSON string.",
Json::CreatePretty => "Convert a value to a formatted JSON string.",
Json::Parse => "Convert JSON to a value",
}
}
fn r#type(&self) -> Type { fn r#type(&self) -> Type {
match self { match self {
Json::Create => Type::function(vec![Type::Any], Type::String), Json::Create => Type::function(vec![Type::Any], Type::String),

View File

@ -15,6 +15,7 @@ use self::{json::Json, string::StringFunction};
pub trait Callable { pub trait Callable {
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
fn r#type(&self) -> Type; fn r#type(&self) -> Type;
fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value>; fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value>;
} }
@ -49,6 +50,21 @@ impl Callable for BuiltInFunction {
} }
} }
fn description(&self) -> &'static str {
match self {
BuiltInFunction::AssertEqual => "assert_equal",
BuiltInFunction::FsRead => "read",
BuiltInFunction::Json(json_function) => json_function.name(),
BuiltInFunction::Length => "length",
BuiltInFunction::Output => "output",
BuiltInFunction::RandomBoolean => "boolean",
BuiltInFunction::RandomFloat => "float",
BuiltInFunction::RandomFrom => "from",
BuiltInFunction::RandomInteger => "integer",
BuiltInFunction::String(string_function) => string_function.name(),
}
}
fn r#type(&self) -> Type { fn r#type(&self) -> Type {
match self { match self {
BuiltInFunction::AssertEqual => Type::function(vec![Type::Any, Type::Any], Type::None), BuiltInFunction::AssertEqual => Type::function(vec![Type::Any, Type::Any], Type::None),

View File

@ -79,6 +79,41 @@ impl Callable for StringFunction {
} }
} }
fn description(&self) -> &'static str {
match self {
StringFunction::AsBytes => "TODO",
StringFunction::EndsWith => "TODO",
StringFunction::Find => "TODO",
StringFunction::Insert => "TODO",
StringFunction::IsAscii => "TODO",
StringFunction::IsEmpty => "TODO",
StringFunction::Lines => "TODO",
StringFunction::Matches => "TODO",
StringFunction::Parse => "TODO",
StringFunction::Remove => "TODO",
StringFunction::ReplaceRange => "TODO",
StringFunction::Retain => "TODO",
StringFunction::Split => "TODO",
StringFunction::SplitAt => "TODO",
StringFunction::SplitInclusive => "TODO",
StringFunction::SplitN => "TODO",
StringFunction::SplitOnce => "TODO",
StringFunction::SplitTerminator => "TODO",
StringFunction::SplitWhitespace => "TODO",
StringFunction::StartsWith => "TODO",
StringFunction::StripPrefix => "TODO",
StringFunction::ToLowercase => "TODO",
StringFunction::ToUppercase => "TODO",
StringFunction::Trim => "TODO",
StringFunction::TrimEnd => "TODO",
StringFunction::TrimEndMatches => "TODO",
StringFunction::TrimMatches => "TODO",
StringFunction::TrimStart => "TODO",
StringFunction::TrimStartMatches => "TODO",
StringFunction::Truncate => "TODO",
}
}
fn r#type(&self) -> Type { fn r#type(&self) -> Type {
match self { match self {
StringFunction::AsBytes => { StringFunction::AsBytes => {

View File

@ -9,7 +9,9 @@ use reedline::{
StyledText, Suggestion, StyledText, Suggestion,
}; };
use std::{borrow::Cow, fs::read_to_string, path::PathBuf, process::Command}; use std::{
borrow::Cow, collections::BTreeSet, fs::read_to_string, path::PathBuf, process::Command,
};
use dust_lang::{built_in_values, Interpreter, Map, Result, Value}; use dust_lang::{built_in_values, Interpreter, Map, Result, Value};
@ -123,7 +125,7 @@ impl DustHighlighter {
} }
} }
const HIGHLIGHT_TERMINATORS: [char; 8] = [' ', ':', '(', ')', '{', '}', '[', ']']; const INPUT_TERMINATORS: [char; 8] = [' ', ':', '(', ')', '{', '}', '[', ']'];
impl Highlighter for DustHighlighter { impl Highlighter for DustHighlighter {
fn highlight(&self, line: &str, _cursor: usize) -> reedline::StyledText { fn highlight(&self, line: &str, _cursor: usize) -> reedline::StyledText {
@ -153,14 +155,14 @@ impl Highlighter for DustHighlighter {
let mut styled = StyledText::new(); let mut styled = StyledText::new();
for word in line.split_inclusive(&HIGHLIGHT_TERMINATORS) { for word in line.split_inclusive(&INPUT_TERMINATORS) {
let word_is_highlighted = let word_is_highlighted =
highlight_identifier(&mut styled, &word[0..word.len() - 1], &self.context); highlight_identifier(&mut styled, &word[0..word.len() - 1], &self.context);
if word_is_highlighted { if word_is_highlighted {
let final_char = word.chars().last().unwrap(); let final_char = word.chars().last().unwrap();
if HIGHLIGHT_TERMINATORS.contains(&final_char) { if INPUT_TERMINATORS.contains(&final_char) {
let mut terminator_style = Style::new(); let mut terminator_style = Style::new();
terminator_style.foreground = Some(Color::Cyan); terminator_style.foreground = Some(Color::Cyan);
@ -246,19 +248,25 @@ impl DustCompleter {
} }
impl Completer for DustCompleter { impl Completer for DustCompleter {
fn complete(&mut self, _line: &str, pos: usize) -> Vec<Suggestion> { fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
let variables = self.context.variables().unwrap(); let mut suggestions = Vec::new();
let mut suggestions = Vec::with_capacity(variables.len()); let last_word = if let Some(word) = line.rsplit(INPUT_TERMINATORS).next() {
word
} else {
""
};
for (key, (value, r#type)) in variables.iter() { for built_in_value in built_in_values() {
if built_in_value.name().contains(last_word) {
suggestions.push(Suggestion { suggestions.push(Suggestion {
value: key.clone(), value: built_in_value.name().to_string(),
description: Some(value.to_string()), description: Some(built_in_value.description().to_string()),
extra: Some(vec![r#type.to_string()]), extra: None,
span: Span::new(pos, pos), span: Span::new(pos - last_word.len(), pos),
append_whitespace: false, append_whitespace: false,
}); });
} }
}
suggestions suggestions
} }
@ -286,7 +294,7 @@ fn run_shell(context: Map) -> Result<()> {
keybindings.add_binding( keybindings.add_binding(
KeyModifiers::CONTROL, KeyModifiers::CONTROL,
KeyCode::Char('h'), KeyCode::Char('h'),
ReedlineEvent::Menu("help menu".to_string()), ReedlineEvent::Menu("variable menu".to_string()),
); );
let edit_mode = Box::new(Emacs::new(keybindings)); let edit_mode = Box::new(Emacs::new(keybindings));
@ -295,16 +303,29 @@ fn run_shell(context: Map) -> Result<()> {
.expect("Error loading history."), .expect("Error loading history."),
); );
let hinter = Box::new(DefaultHinter::default().with_style(Style::new().dimmed())); let hinter = Box::new(DefaultHinter::default().with_style(Style::new().dimmed()));
let mut built_in_info = BTreeSet::new();
for built_in_value in built_in_values() {
built_in_info.insert((
built_in_value.name().to_string(),
built_in_value.description().to_string(),
built_in_value.r#type().to_string(),
));
}
let completer = DustCompleter::new(context.clone()); let completer = DustCompleter::new(context.clone());
let mut line_editor = Reedline::create() let mut line_editor = Reedline::create()
.with_edit_mode(edit_mode) .with_edit_mode(edit_mode)
.with_history(history) .with_history(history)
.with_highlighter(Box::new(DustHighlighter::new(context))) .with_highlighter(Box::new(DustHighlighter::new(context.clone())))
.with_hinter(hinter) .with_hinter(hinter)
.use_kitty_keyboard_enhancement(true) .use_kitty_keyboard_enhancement(true)
.with_completer(Box::new(completer)) .with_completer(Box::new(completer))
.with_menu(ReedlineMenu::EngineCompleter(Box::new( .with_menu(ReedlineMenu::EngineCompleter(Box::new(
ColumnarMenu::default().with_name("help menu"), ColumnarMenu::default()
.with_name("variable menu")
.with_text_style(Style::new().fg(Color::White)),
))); )));
let mut prompt = StarshipPrompt::new(); let mut prompt = StarshipPrompt::new();
@ -333,11 +354,11 @@ fn run_shell(context: Map) -> Result<()> {
} }
} }
Ok(Signal::CtrlD) | Ok(Signal::CtrlC) => { Ok(Signal::CtrlD) | Ok(Signal::CtrlC) => {
println!("\nAborted!"); println!("\nLeaving the Dust shell.");
break; break;
} }
x => { x => {
println!("Event: {:?}", x); println!("Unknown event: {:?}", x);
} }
} }
} }