mirror of
https://github.com/nushell/nushell
synced 2024-12-25 12:33:17 +00:00
Merge pull request #2 from jonathandturner/colorprompt
Workaround color prompt by making our own helper
This commit is contained in:
commit
34a866df99
3 changed files with 59 additions and 8 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -415,7 +415,7 @@ dependencies = [
|
|||
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"nom 5.0.0-beta1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustyline 4.0.0 (git+https://github.com/jonathandturner/rustyline.git)",
|
||||
"rustyline 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sysinfo 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
@ -564,7 +564,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
[[package]]
|
||||
name = "rustyline"
|
||||
version = "4.0.0"
|
||||
source = "git+https://github.com/jonathandturner/rustyline.git#15cc18b1dd550873c9a44d90e8992f18c46b3744"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -827,7 +827,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8f0a0bcab2fd7d1d7c54fa9eae6f43eddeb9ce2e7352f8518a814a4f65d60c58"
|
||||
"checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96"
|
||||
"checksum rustc-demangle 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288"
|
||||
"checksum rustyline 4.0.0 (git+https://github.com/jonathandturner/rustyline.git)" = "<none>"
|
||||
"checksum rustyline 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf0a3bbb3167469f834da68a6636b93d4f6838f5438dd53ac02668abee8b997a"
|
||||
"checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f"
|
||||
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
|
||||
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
|
||||
|
|
|
@ -7,7 +7,7 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rustyline = { version = "4.0.0", git = "https://github.com/jonathandturner/rustyline.git" }
|
||||
rustyline = "4.0.0"
|
||||
sysinfo = "0.8.3"
|
||||
chrono = "0.4.6"
|
||||
chrono-tz = "0.5.1"
|
||||
|
|
59
src/main.rs
59
src/main.rs
|
@ -3,6 +3,7 @@
|
|||
|
||||
#[allow(unused)]
|
||||
use crate::prelude::*;
|
||||
use std::borrow::Cow::{self, Borrowed, Owned};
|
||||
|
||||
mod commands;
|
||||
mod context;
|
||||
|
@ -20,10 +21,13 @@ crate use crate::env::{Environment, Host};
|
|||
crate use crate::errors::ShellError;
|
||||
crate use crate::format::{EntriesListView, GenericView};
|
||||
use crate::object::Value;
|
||||
use rustyline::completion::{Completer, FilenameCompleter, Pair};
|
||||
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
||||
use rustyline::hint::{Hinter, HistoryHinter};
|
||||
|
||||
use ansi_term::Color;
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::Editor;
|
||||
use rustyline::{ColorMode, Config, Editor, Helper, self};
|
||||
use std::collections::VecDeque;
|
||||
use std::error::Error;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
@ -44,8 +48,55 @@ impl<T> MaybeOwned<'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
struct MyHelper(FilenameCompleter, MatchingBracketHighlighter, HistoryHinter);
|
||||
impl Completer for MyHelper {
|
||||
type Candidate = Pair;
|
||||
|
||||
fn complete(
|
||||
&self,
|
||||
line: &str,
|
||||
pos: usize,
|
||||
ctx: &rustyline::Context<'_>,
|
||||
) -> Result<(usize, Vec<Pair>), ReadlineError> {
|
||||
self.0.complete(line, pos, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Hinter for MyHelper {
|
||||
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
|
||||
self.2.hint(line, pos, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Highlighter for MyHelper {
|
||||
fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> {
|
||||
Owned("\x1b[32m".to_owned() + &prompt[0..prompt.len() - 2] + "\x1b[m> ")
|
||||
}
|
||||
|
||||
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
|
||||
Owned("\x1b[1m".to_owned() + hint + "\x1b[m")
|
||||
}
|
||||
|
||||
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
|
||||
self.1.highlight(line, pos)
|
||||
}
|
||||
|
||||
fn highlight_char(&self, line: &str, pos: usize) -> bool {
|
||||
self.1.highlight_char(line, pos)
|
||||
}
|
||||
}
|
||||
|
||||
impl Helper for MyHelper {}
|
||||
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
let mut rl = Editor::<()>::new();
|
||||
let config = Config::builder().color_mode(ColorMode::Forced).build();
|
||||
let h = MyHelper(
|
||||
FilenameCompleter::new(),
|
||||
MatchingBracketHighlighter::new(),
|
||||
HistoryHinter {},
|
||||
);
|
||||
let mut rl: Editor<MyHelper> = Editor::with_config(config);
|
||||
rl.set_helper(Some(h));
|
||||
if rl.load_history("history.txt").is_err() {
|
||||
println!("No previous history.");
|
||||
}
|
||||
|
@ -64,14 +115,14 @@ fn main() -> Result<(), Box<Error>> {
|
|||
("select", Box::new(select::Select)),
|
||||
("reject", Box::new(reject::Reject)),
|
||||
("to-array", Box::new(to_array::ToArray)),
|
||||
("where", Box::new(where_::Where))
|
||||
("where", Box::new(where_::Where)),
|
||||
]);
|
||||
}
|
||||
|
||||
loop {
|
||||
let readline = rl.readline(&format!(
|
||||
"{}> ",
|
||||
Color::Green.paint(context.lock().unwrap().env.cwd().display().to_string())
|
||||
context.lock().unwrap().env.cwd().display().to_string()
|
||||
));
|
||||
|
||||
match readline {
|
||||
|
|
Loading…
Reference in a new issue