Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Distinct Builder for Reedline #740

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
63ccb4b
Configure gitignore for vsc
cactusdualcore Feb 1, 2024
cab3c49
Merge branch 'main' of https://github.com/Cactus-man/reedline into main
cactusdualcore Feb 1, 2024
03faf57
Use actual builder to construct `Reedline`, deprecate builder methods…
cactusdualcore Feb 1, 2024
2dcf5b1
Fixed 'external_printer' feature
cactusdualcore Feb 1, 2024
d09289b
Fixed 'sqlite' feature
cactusdualcore Feb 1, 2024
76b3c9d
Introduce "paste" as a new dep
cactusdualcore Feb 3, 2024
9451daf
Write "with_builder_methods" macro
cactusdualcore Feb 3, 2024
37b71ab
transition least complex case to macro
cactusdualcore Feb 3, 2024
4a0cc5e
add boolean case to macro
cactusdualcore Feb 3, 2024
0179722
transition boolean case to macro
cactusdualcore Feb 3, 2024
a1f1e2b
fix examples
cactusdualcore Feb 3, 2024
22094a2
add generic type case with boxing to macro
cactusdualcore Feb 3, 2024
405e5c7
Fix typo that causes compile error
cactusdualcore Feb 3, 2024
ec327d9
transition generic builder methods to macro
cactusdualcore Feb 3, 2024
e5d0e91
separate macro into distinct ones for clarity
cactusdualcore Feb 3, 2024
dd987cb
Fixed wrong name for method in examples
cactusdualcore Feb 3, 2024
6c7576d
update base reedline version to 0.31
cactusdualcore Apr 15, 2024
e69faae
include new `executing_host_command` field in builder
cactusdualcore Apr 15, 2024
b1bc027
removed macros
cactusdualcore Apr 15, 2024
0383e9c
remove 'paste' dependency as it is no longer needed
cactusdualcore Apr 15, 2024
0cc7039
extract builder-style API into standalone file to reduce clutter
cactusdualcore Apr 15, 2024
94c3171
removed deprecation on builder-style API
cactusdualcore Apr 15, 2024
d483ec0
cleaned the Builder API and the builder-style engine API
cactusdualcore Apr 15, 2024
ff0ef3d
fix doctests
cactusdualcore Apr 15, 2024
029a991
appease the clippy gods
cactusdualcore Apr 15, 2024
0d8da0e
Merge branch 'main' of https://github.com/nushell/reedline
cactusdualcore Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ tarpaulin-report.html
*.rsproj
*.rsproj.user
*.sln

# Visual Studio Code
.vscode
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io;

fn main() -> io::Result<()> {
// Create a new Reedline engine with a local History that is not synchronized to a file.
let mut line_editor = Reedline::create();
let mut line_editor = Reedline::new();
let prompt = DefaultPrompt::default();

loop {
Expand Down
13 changes: 7 additions & 6 deletions examples/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> io::Result<()> {
"abadarabc".into(),
];

let completer = Box::new(DefaultCompleter::new_with_wordlen(commands, 2));
let completer = DefaultCompleter::new_with_wordlen(commands, 2);

// Use the interactive menu to select options from the completer
let columnar_menu = ColumnarMenu::default()
Expand All @@ -71,12 +71,13 @@ fn main() -> io::Result<()> {
let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);

let edit_mode = Box::new(Emacs::new(keybindings));
let edit_mode = Emacs::new(keybindings);

let mut line_editor = Reedline::create()
.with_completer(completer)
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode);
let mut line_editor = Reedline::builder()
.with_completions(completer)
.add_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode)
.build();

let prompt = DefaultPrompt::default();

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Prompt for CustomPrompt {

fn main() -> io::Result<()> {
println!("Custom prompt demo:\nAbort with Ctrl-C or Ctrl-D");
let mut line_editor = Reedline::create();
let mut line_editor = Reedline::new();

let prompt = CustomPrompt(Cell::new(0), "Custom Prompt");

Expand Down
13 changes: 7 additions & 6 deletions examples/cwd_aware_hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ fn create_item(cwd: &str, cmd: &str, exit_status: i64) -> reedline::HistoryItem
}
}

fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> Box<dyn reedline::History> {
fn create_filled_example_history(home_dir: &str, orig_dir: &str) -> impl reedline::History {
use reedline::History;
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
let mut history = Box::new(reedline::FileBackedHistory::new(100));
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
let mut history = Box::new(reedline::SqliteBackedHistory::in_memory().unwrap());
let mut history = reedline::SqliteBackedHistory::in_memory().unwrap();

history.save(create_item(orig_dir, "dummy", 0)).unwrap(); // add dummy item so ids start with 1
history.save(create_item(orig_dir, "ls /usr", 0)).unwrap();
Expand All @@ -56,11 +56,12 @@ fn main() -> io::Result<()> {
orig_dir.to_string_lossy().as_ref(),
);

let mut line_editor = Reedline::create()
.with_hinter(Box::new(
let mut line_editor = Reedline::builder()
.with_hints(
CwdAwareHinter::default().with_style(Style::new().bold().italic().fg(Color::Yellow)),
))
.with_history(history);
)
.with_history(history)
.build();

let prompt = DefaultPrompt::default();

Expand Down
66 changes: 29 additions & 37 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use {
reedline::{
default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
ColumnarMenu, DefaultCompleter, DefaultHinter, DefaultPrompt, DefaultValidator,
EditCommand, EditMode, Emacs, ExampleHighlighter, Keybindings, ListMenu, Reedline,
ReedlineEvent, ReedlineMenu, Signal, Vi,
EditCommand, Emacs, ExampleHighlighter, Keybindings, ListMenu, Reedline, ReedlineEvent,
ReedlineMenu, Signal, Vi,
},
};

Expand All @@ -33,16 +33,14 @@ fn main() -> reedline::Result<()> {
};

#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
let history = Box::new(
reedline::SqliteBackedHistory::with_file(
"history.sqlite3".into(),
history_session_id,
Some(chrono::Utc::now()),
)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
);
let history = reedline::SqliteBackedHistory::with_file(
"history.sqlite3".into(),
history_session_id,
Some(chrono::Utc::now()),
)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
let history = Box::new(FileBackedHistory::with_file(50, "history.txt".into())?);
let history = FileBackedHistory::with_file(50, "history.txt".into())?;
let commands = vec![
"test".into(),
"clear".into(),
Expand Down Expand Up @@ -71,41 +69,38 @@ fn main() -> reedline::Result<()> {
"こんにちは世界".into(),
"こんばんは世界".into(),
];
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
let completer = DefaultCompleter::new_with_wordlen(commands.clone(), 2);

let cursor_config = CursorConfig {
vi_insert: Some(SetCursorStyle::BlinkingBar),
vi_normal: Some(SetCursorStyle::SteadyBlock),
emacs: None,
};

let mut line_editor = Reedline::create()
.with_history_session_id(history_session_id)
let mut builder = Reedline::builder()
.with_history_session_id(history_session_id.unwrap())
.with_history(history)
.with_history_exclusion_prefix(Some(" ".to_string()))
.with_completer(completer)
.with_quick_completions(true)
.with_partial_completions(true)
.with_history_exclusion_prefix(" ".to_string())
.with_completions(completer)
.use_quick_completions(true)
.use_partial_completions(true)
.with_cursor_config(cursor_config)
.use_bracketed_paste(true)
.use_kitty_keyboard_enhancement(true)
.with_highlighter(Box::new(ExampleHighlighter::new(commands)))
.with_hinter(Box::new(
DefaultHinter::default().with_style(Style::new().fg(Color::DarkGray)),
))
.with_validator(Box::new(DefaultValidator))
.with_ansi_colors(true);
.with_highlighter(ExampleHighlighter::new(commands))
.with_hints(DefaultHinter::default().with_style(Style::new().fg(Color::DarkGray)))
.with_validator(DefaultValidator)
.use_ansi_colors(true);

// Adding default menus for the compiled reedline
line_editor = line_editor
.with_menu(ReedlineMenu::EngineCompleter(Box::new(
builder = builder.add_menus(vec![
ReedlineMenu::EngineCompleter(Box::new(
ColumnarMenu::default().with_name("completion_menu"),
)))
.with_menu(ReedlineMenu::HistoryMenu(Box::new(
ListMenu::default().with_name("history_menu"),
)));
)),
ReedlineMenu::HistoryMenu(Box::new(ListMenu::default().with_name("history_menu"))),
]);

let edit_mode: Box<dyn EditMode> = if vi_mode {
if vi_mode {
let mut normal_keybindings = default_vi_normal_keybindings();
let mut insert_keybindings = default_vi_insert_keybindings();

Expand All @@ -114,22 +109,19 @@ fn main() -> reedline::Result<()> {

add_newline_keybinding(&mut insert_keybindings);

Box::new(Vi::new(insert_keybindings, normal_keybindings))
builder = builder.with_edit_mode(Vi::new(insert_keybindings, normal_keybindings));
} else {
let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);
add_newline_keybinding(&mut keybindings);

Box::new(Emacs::new(keybindings))
builder = builder.with_edit_mode(Emacs::new(keybindings));
};

line_editor = line_editor.with_edit_mode(edit_mode);

// Adding vi as text editor
let temp_file = temp_dir().join("temp_file.nu");
let mut command = Command::new("vi");
command.arg(&temp_file);
line_editor = line_editor.with_buffer_editor(command, temp_file);
let mut line_editor = builder.with_buffer_editor(command, temp_file).build();

let prompt = DefaultPrompt::default();

Expand Down
2 changes: 1 addition & 1 deletion examples/external_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() {
}
});

let mut line_editor = Reedline::create().with_external_printer(printer);
let mut line_editor = Reedline::builder().with_external_printer(printer).build();
let prompt = DefaultPrompt::default();

loop {
Expand Down
5 changes: 3 additions & 2 deletions examples/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ fn main() -> io::Result<()> {
"hello world reedline".into(),
"this is the reedline crate".into(),
];
let mut line_editor =
Reedline::create().with_highlighter(Box::new(ExampleHighlighter::new(commands)));
let mut line_editor = Reedline::builder()
.with_highlighter(ExampleHighlighter::new(commands))
.build();
let prompt = DefaultPrompt::default();

loop {
Expand Down
6 changes: 3 additions & 3 deletions examples/hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use reedline::{DefaultHinter, DefaultPrompt, Reedline, Signal};
use std::io;

fn main() -> io::Result<()> {
let mut line_editor = Reedline::create().with_hinter(Box::new(
DefaultHinter::default().with_style(Style::new().italic().fg(Color::LightGray)),
));
let mut line_editor = Reedline::builder()
.with_hints(DefaultHinter::default().with_style(Style::new().italic().fg(Color::LightGray)))
.build();
let prompt = DefaultPrompt::default();

loop {
Expand Down
8 changes: 3 additions & 5 deletions examples/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
use std::io;

fn main() -> io::Result<()> {
let history = Box::new(
FileBackedHistory::with_file(5, "history.txt".into())
.expect("Error configuring history with file"),
);
let history = FileBackedHistory::with_file(5, "history.txt".into())
.expect("Error configuring history with file");

let mut line_editor = Reedline::create().with_history(history);
let mut line_editor = Reedline::builder().with_history(history).build();
let prompt = DefaultPrompt::default();

loop {
Expand Down
13 changes: 7 additions & 6 deletions examples/ide_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn main() -> io::Result<()> {
"abadarabc".into(),
];

let completer = Box::new(DefaultCompleter::new_with_wordlen(commands, 2));
let completer = DefaultCompleter::new_with_wordlen(commands, 2);

// Use the interactive menu to select options from the completer
let mut ide_menu = IdeMenu::default()
Expand All @@ -107,12 +107,13 @@ fn main() -> io::Result<()> {
let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);

let edit_mode = Box::new(Emacs::new(keybindings));
let edit_mode = Emacs::new(keybindings);

let mut line_editor = Reedline::create()
.with_completer(completer)
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode);
let mut line_editor = Reedline::builder()
.with_completions(completer)
.add_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode)
.build();

let prompt = DefaultPrompt::default();

Expand Down
30 changes: 15 additions & 15 deletions examples/transient_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,31 @@ fn main() -> io::Result<()> {
"hello world reedline".into(),
"this is the reedline crate".into(),
];
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
let completer = DefaultCompleter::new_with_wordlen(commands.clone(), 2);
// Use the interactive menu to select options from the completer
let completion_menu = Box::new(ColumnarMenu::default().with_name("completion_menu"));

let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);

let edit_mode = Box::new(Emacs::new(keybindings));
let edit_mode = Emacs::new(keybindings);

let mut line_editor = Reedline::create()
.with_hinter(Box::new(
DefaultHinter::default().with_style(Style::new().fg(Color::LightGray)),
))
.with_completer(completer)
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
#[allow(unused_mut)]
let mut builder = Reedline::builder()
.with_hints(DefaultHinter::default().with_style(Style::new().fg(Color::LightGray)))
.with_completions(completer)
.add_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode)
.with_highlighter(Box::new(ExampleHighlighter::new(commands)))
.with_validator(Box::new(CustomValidator {}))
.with_ansi_colors(true)
.with_history_exclusion_prefix(Some(String::from(" ")))
.with_transient_prompt(Box::new(TransientPrompt {}));
.with_highlighter(ExampleHighlighter::new(commands))
.with_validator(CustomValidator {})
.use_ansi_colors(true)
.with_history_exclusion_prefix(String::from(" "))
.with_transient_prompt(TransientPrompt {});
#[cfg(any(feature = "sqlite", feature = "sqlite-dynlib"))]
{
line_editor = line_editor.with_history(Box::new(SqliteBackedHistory::in_memory().unwrap()));
}
builder = builder.with_history(SqliteBackedHistory::in_memory().unwrap());
};
let mut line_editor = builder.build();

let prompt = DefaultPrompt::default();

Expand Down
6 changes: 4 additions & 2 deletions examples/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ impl Validator for CustomValidator {
}

fn main() -> io::Result<()> {
println!("Input \"complete\" followed by [Enter], will accept the input line (Signal::Succeed will be called)\nPressing [Enter] will in other cases give you a multi-line prompt.\nAbort with Ctrl-C or Ctrl-D");
let mut line_editor = Reedline::create().with_validator(Box::new(CustomValidator));
println!("Input \"complete\" followed by [Enter], will accept the input line (Signal::Succeed will be called)");
println!("Pressing [Enter] will in other cases give you a multi-line prompt.");
println!("Abort with Ctrl-C or Ctrl-D");
let mut line_editor = Reedline::builder().with_validator(CustomValidator).build();

let prompt = DefaultPrompt::default();

Expand Down
4 changes: 2 additions & 2 deletions src/completion/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use std::{
/// "hello world reedline".into(),
/// "this is the reedline crate".into(),
/// ];
/// let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
/// let completer = DefaultCompleter::new_with_wordlen(commands.clone(), 2);
///
/// let mut line_editor = Reedline::create().with_completer(completer);
/// let mut line_editor = Reedline::builder().with_completions(completer).build();
/// ```
#[derive(Debug, Clone)]
pub struct DefaultCompleter {
Expand Down
Loading
Loading