Skip to content

Commit

Permalink
Color prompt (#79)
Browse files Browse the repository at this point in the history
implement colored prompt via helper
  • Loading branch information
wolfv authored Sep 8, 2024
1 parent f59bcb5 commit a8cd958
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ anyhow = "1.0.87"
clap = { version = "4.5.17", features = ["derive"] }
deno_task_shell = { path = "../deno_task_shell" }
futures = "0.3.30"
rustyline = "14.0.0"
rustyline = { version = "14.0.0", features = ["derive"] }
tokio = "1.40.0"
uu_ls = "0.0.27"
dirs = "5.0.1"
Expand Down
8 changes: 7 additions & 1 deletion crates/shell/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use std::path::Path;

pub struct ShellCompleter;

impl Default for ShellCompleter {
fn default() -> Self {
ShellCompleter
}
}

impl Completer for ShellCompleter {
type Candidate = Pair;

Expand Down Expand Up @@ -38,7 +44,7 @@ impl Completer for ShellCompleter {
}

fn extract_word(line: &str, pos: usize) -> (usize, &str) {
if line.ends_with(" ") {
if line.ends_with(' ') {
return (pos, "");
}
let words: Vec<_> = line[..pos].split_whitespace().collect();
Expand Down
43 changes: 43 additions & 0 deletions crates/shell/src/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use rustyline::{
highlight::Highlighter, validate::MatchingBracketValidator, Completer, Helper, Hinter,
Validator,
};

use crate::completion;

use std::borrow::Cow::Borrowed;

#[derive(Helper, Completer, Hinter, Validator)]
pub(crate) struct ShellPromptHelper {
#[rustyline(Completer)]
completer: completion::ShellCompleter,

#[rustyline(Validator)]
validator: MatchingBracketValidator,

pub colored_prompt: String,
}

impl Default for ShellPromptHelper {
fn default() -> Self {
Self {
completer: completion::ShellCompleter,
validator: MatchingBracketValidator::new(),
colored_prompt: String::new(),
}
}
}

impl Highlighter for ShellPromptHelper {
fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
&'s self,
prompt: &'p str,
default: bool,
) -> std::borrow::Cow<'b, str> {
if default {
Borrowed(&self.colored_prompt)
} else {
Borrowed(prompt)
}
}
}
23 changes: 16 additions & 7 deletions crates/shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::rc::Rc;

use anyhow::Context;
use clap::Parser;
use completion::ShellCompleter;
use deno_task_shell::parser::debug_parse;
use deno_task_shell::{
execute_sequential_list, AsyncCommandBehavior, ExecuteResult, ShellCommand, ShellPipeReader,
Expand All @@ -15,6 +14,7 @@ use rustyline::{CompletionType, Config, Editor};

mod commands;
mod completion;
mod helper;

fn commands() -> HashMap<String, Rc<dyn ShellCommand>> {
HashMap::from([(
Expand Down Expand Up @@ -80,9 +80,12 @@ async fn interactive() -> anyhow::Result<()> {

let mut rl = Editor::with_config(config)?;

let h = ShellCompleter {};
let helper = helper::ShellPromptHelper::default();
rl.set_helper(Some(helper));

rl.set_helper(Some(h));
// let h = ShellCompleter {};

// rl.set_helper(Some(h));

let mut state = init_state();

Expand All @@ -102,6 +105,7 @@ async fn interactive() -> anyhow::Result<()> {
if !state.last_command_cd() {
state.update_git_branch();
}

let mut git_branch: String = "".to_string();
if state.git_repository() {
git_branch = match state.git_branch().strip_prefix("ref: refs/heads/") {
Expand All @@ -117,10 +121,15 @@ async fn interactive() -> anyhow::Result<()> {
git_branch = "(".to_owned() + &git_branch + ")";
}

let prompt = cwd
.strip_prefix(home_str)
.map(|stripped| format!("~{}{git_branch}$ ", stripped.replace('\\', "/")))
.unwrap_or_else(|| format!("{}{git_branch}$ ", cwd));
let display_cwd = if let Some(stripped) = cwd.strip_prefix(home_str) {
format!("~{}", stripped.replace('\\', "/"))
} else {
cwd.to_string()
};

let prompt = format!("{}{git_branch}$ ", display_cwd);
let color_prompt = format!("\x1b[34m{}\x1b[31m{git_branch}\x1b[0m$ ", display_cwd);
rl.helper_mut().unwrap().colored_prompt = color_prompt;
rl.readline(&prompt)
};

Expand Down

0 comments on commit a8cd958

Please sign in to comment.