Integrate starship

This commit is contained in:
Jeff 2024-01-26 18:28:37 -05:00
parent 3d21196768
commit 0eee9f0936

View File

@ -4,12 +4,11 @@ use clap::{Parser, Subcommand};
use crossterm::event::{KeyCode, KeyModifiers}; use crossterm::event::{KeyCode, KeyModifiers};
use nu_ansi_term::Style; use nu_ansi_term::Style;
use reedline::{ use reedline::{
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultHinter, EditCommand, Emacs, default_emacs_keybindings, DefaultHinter, EditCommand, Emacs, Highlighter, Prompt, Reedline,
Highlighter, Prompt, Reedline, ReedlineEvent, ReedlineMenu, Signal, SqliteBackedHistory, ReedlineEvent, Signal, SqliteBackedHistory, StyledText,
StyledText,
}; };
use std::{borrow::Cow, fs::read_to_string, path::PathBuf, time::SystemTime}; use std::{borrow::Cow, 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};
@ -171,36 +170,52 @@ impl Highlighter for DustHighlighter {
} }
} }
struct DustPrompt; struct DustPrompt {
left: String,
right: String,
}
impl DustPrompt {
fn new() -> Self {
Self {
left: String::new(),
right: String::new(),
}
}
fn reload(&mut self) {
let output = Command::new("starship")
.arg("prompt")
.output()
.unwrap()
.stdout;
self.left = String::from_utf8_lossy(&output).trim().to_string();
}
}
impl Prompt for DustPrompt { impl Prompt for DustPrompt {
fn render_prompt_left(&self) -> Cow<str> { fn render_prompt_left(&self) -> Cow<str> {
let path = std::env::current_dir() Cow::Borrowed(&self.left)
.map(|path| path.file_name().unwrap().to_string_lossy().to_string())
.unwrap_or_else(|_| "No workdir".to_string());
Cow::Owned(path)
} }
fn render_prompt_right(&self) -> Cow<str> { fn render_prompt_right(&self) -> Cow<str> {
let time = humantime::format_rfc3339_seconds(SystemTime::now()).to_string(); Cow::Borrowed(&self.right)
Cow::Owned(time)
} }
fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> { fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> {
Cow::Borrowed(" > ") Cow::Borrowed("")
} }
fn render_prompt_multiline_indicator(&self) -> Cow<str> { fn render_prompt_multiline_indicator(&self) -> Cow<str> {
Cow::Borrowed(" > ") Cow::Borrowed("")
} }
fn render_prompt_history_search_indicator( fn render_prompt_history_search_indicator(
&self, &self,
_history_search: reedline::PromptHistorySearch, _history_search: reedline::PromptHistorySearch,
) -> Cow<str> { ) -> Cow<str> {
Cow::Borrowed(" ? ") Cow::Borrowed("")
} }
} }
@ -215,14 +230,14 @@ fn run_shell(context: Map) -> Result<()> {
); );
keybindings.add_binding( keybindings.add_binding(
KeyModifiers::NONE, KeyModifiers::NONE,
KeyCode::Tab, KeyCode::Enter,
ReedlineEvent::UntilFound(vec![ ReedlineEvent::Multiple(vec![
ReedlineEvent::Menu("completion_menu".to_string()), ReedlineEvent::SubmitOrNewline,
ReedlineEvent::MenuNext, ReedlineEvent::ExecuteHostCommand("output('hi')".to_string()),
]), ]),
); );
keybindings.add_binding( keybindings.add_binding(
KeyModifiers::CONTROL, KeyModifiers::NONE,
KeyCode::Tab, KeyCode::Tab,
ReedlineEvent::Edit(vec![EditCommand::InsertString(" ".to_string())]), ReedlineEvent::Edit(vec![EditCommand::InsertString(" ".to_string())]),
); );
@ -238,19 +253,18 @@ fn run_shell(context: Map) -> Result<()> {
.with_history(history) .with_history(history)
.with_highlighter(Box::new(DustHighlighter::new(context))) .with_highlighter(Box::new(DustHighlighter::new(context)))
.with_hinter(hinter) .with_hinter(hinter)
.with_menu(ReedlineMenu::WithCompleter { .use_kitty_keyboard_enhancement(true);
menu: Box::new(ColumnarMenu::default().with_name("completion_menu")), let mut prompt = DustPrompt::new();
completer: Box::new(DefaultCompleter::new_with_wordlen(
vec!["test".to_string()], prompt.reload();
2,
)),
});
let prompt = DustPrompt;
loop { loop {
let sig = line_editor.read_line(&prompt); let sig = line_editor.read_line(&prompt);
match sig { match sig {
Ok(Signal::Success(buffer)) => { Ok(Signal::Success(buffer)) => {
prompt.reload();
if buffer.trim().is_empty() { if buffer.trim().is_empty() {
continue; continue;
} }