mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 19:27:44 +02:00
Refactor the repl loop
This commit is contained in:
41
src/shell/completer.rs
Normal file
41
src/shell/completer.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use rustyline::completion::{Candidate, Completer};
|
||||
use rustyline::line_buffer::LineBuffer;
|
||||
|
||||
#[derive(Debug)]
|
||||
crate struct NuCompleter;
|
||||
|
||||
impl Completer for NuCompleter {
|
||||
type Candidate = NuPair;
|
||||
|
||||
fn complete(
|
||||
&self,
|
||||
_line: &str,
|
||||
_pos: usize,
|
||||
_context: &rustyline::Context,
|
||||
) -> rustyline::Result<(usize, Vec<NuPair>)> {
|
||||
Ok((
|
||||
0,
|
||||
vec![
|
||||
NuPair("exit", "exit"),
|
||||
NuPair("ls", "ls"),
|
||||
NuPair("ps", "ps"),
|
||||
],
|
||||
))
|
||||
}
|
||||
|
||||
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
|
||||
let end = line.pos();
|
||||
line.replace(start..end, elected)
|
||||
}
|
||||
}
|
||||
|
||||
crate struct NuPair(&'static str, &'static str);
|
||||
|
||||
impl Candidate for NuPair {
|
||||
fn display(&self) -> &str {
|
||||
self.0
|
||||
}
|
||||
fn replacement(&self) -> &str {
|
||||
self.1
|
||||
}
|
||||
}
|
62
src/shell/helper.rs
Normal file
62
src/shell/helper.rs
Normal file
@ -0,0 +1,62 @@
|
||||
use crate::shell::completer::{NuCompleter, NuPair};
|
||||
|
||||
use rustyline::completion::Completer;
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
||||
use rustyline::hint::{Hinter, HistoryHinter};
|
||||
use std::borrow::Cow::{self, Owned};
|
||||
|
||||
crate struct Helper {
|
||||
completer: NuCompleter,
|
||||
highlighter: MatchingBracketHighlighter,
|
||||
hinter: HistoryHinter,
|
||||
}
|
||||
|
||||
impl Helper {
|
||||
crate fn new() -> Helper {
|
||||
Helper {
|
||||
completer: NuCompleter,
|
||||
highlighter: MatchingBracketHighlighter::new(),
|
||||
hinter: HistoryHinter {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Completer for Helper {
|
||||
type Candidate = NuPair;
|
||||
|
||||
fn complete(
|
||||
&self,
|
||||
line: &str,
|
||||
pos: usize,
|
||||
ctx: &rustyline::Context<'_>,
|
||||
) -> Result<(usize, Vec<NuPair>), ReadlineError> {
|
||||
self.completer.complete(line, pos, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Hinter for Helper {
|
||||
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
|
||||
self.hinter.hint(line, pos, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Highlighter for Helper {
|
||||
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.highlighter.highlight(line, pos)
|
||||
}
|
||||
|
||||
fn highlight_char(&self, line: &str, pos: usize) -> bool {
|
||||
self.highlighter.highlight_char(line, pos)
|
||||
}
|
||||
}
|
||||
|
||||
impl rustyline::Helper for Helper {}
|
Reference in New Issue
Block a user