Skip to content

Commit

Permalink
Simplify to just with_buffer_editor
Browse files Browse the repository at this point in the history
The old `with_buffer_editor` would have not set the file and
`with_buffer_editor_command` had unclear semantics if you should provide
the file to the command or not.

Instead of going the route of deprecation let's just make the breaking
change and update `with_buffer_editor` to a more practical signature.
Expect a `Command` and a `PathBuf` for the file.
If the file is not among the args push it.
  • Loading branch information
sholderbach committed Sep 28, 2023
1 parent 383cbf6 commit b5d7ecf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn main() -> std::io::Result<()> {
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(command, temp_file);
line_editor = line_editor.with_buffer_editor(command, temp_file);

let prompt = DefaultPrompt::default();

Expand Down
44 changes: 19 additions & 25 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use crossterm::event::{DisableBracketedPaste, EnableBracketedPaste};
use crossterm::execute;
use itertools::Itertools;

use crate::{enums::ReedlineRawEvent, CursorConfig};
#[cfg(feature = "bashisms")]
Expand Down Expand Up @@ -444,42 +445,35 @@ impl Reedline {
self
}

/// A builder that configures the text editor used to edit the line buffer
/// # Example
/// ```rust,no_run
/// // Create a reedline object with vim as editor
/// A builder that configures the alternate text editor used to edit the line buffer
///
/// use reedline::{DefaultValidator, Reedline};
/// use std::process::Command;
/// let mut command = Command::new("vim");
/// // Command should contain full arguments including the path of temp_file
/// command.args(vec!["-e", "temp.nu"]);
/// You are responsible for providing a file path that is unique to this reedline session
///
/// let mut line_editor =
/// Reedline::create().with_buffer_editor_command(command, "temp.nu".into());
/// ```
#[must_use]
pub fn with_buffer_editor_command(mut self, command: Command, temp_file: PathBuf) -> Self {
self.buffer_editor = Some(BufferEditor { command, temp_file });
self
}

/// A builder that configures the text editor used to edit the line buffer
/// # Example
/// ```rust,no_run
/// // Create a reedline object with vim as editor
///
/// use reedline::{DefaultValidator, Reedline};
/// use reedline::Reedline;
/// use std::env::temp_dir;
/// use std::process::Command;
///
/// let temp_file = std::env::temp_dir().join("my-random-unique.file");
/// let mut command = Command::new("vim");
/// // you can provide additional flags:
/// command.arg("-p"); // open in a vim tab (just for demonstration)
/// // you don't have to pass the filename to the command
/// let mut line_editor =
/// Reedline::create().with_buffer_editor("vim".into(), "nu".into());
/// Reedline::create().with_buffer_editor("vim".into(), temp_file);
/// ```
#[must_use]
#[deprecated = "use `with_buffer_editor_command` instead"]
pub fn with_buffer_editor(mut self, editor: String, extension: String) -> Self {
pub fn with_buffer_editor(mut self, editor: Command, temp_file: PathBuf) -> Self {
let mut editor = editor;
if !editor.get_args().contains(&temp_file.as_os_str()) {
editor.arg(&temp_file);
}
self.buffer_editor = Some(BufferEditor {
command: Command::new(editor),
temp_file: std::env::temp_dir().join(format!("reedline_buffer.{extension}")),
command: editor,
temp_file,
});
self
}
Expand Down

0 comments on commit b5d7ecf

Please sign in to comment.