diff --git a/examples/demo.rs b/examples/demo.rs index 0cf51f5..3f49b1e 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -102,7 +102,7 @@ fn main() -> Result<()> { // Adding vi as text editor line_editor = line_editor.with_buffer_editor("vi".into(), "nu".into()); - let prompt = DefaultPrompt::new(); + let prompt = DefaultPrompt::default(); loop { let sig = line_editor.read_line(&prompt); diff --git a/src/lib.rs b/src/lib.rs index ea28116..5ad8860 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -253,8 +253,8 @@ pub use history::{ mod prompt; pub use prompt::{ - DefaultPrompt, Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, - PromptViMode, + DefaultPrompt, DefaultPromptSegment, Prompt, PromptEditMode, PromptHistorySearch, + PromptHistorySearchStatus, PromptViMode, }; mod edit_mode; diff --git a/src/prompt/default.rs b/src/prompt/default.rs index 86473a0..9da8110 100644 --- a/src/prompt/default.rs +++ b/src/prompt/default.rs @@ -11,23 +11,52 @@ pub static DEFAULT_VI_INSERT_PROMPT_INDICATOR: &str = ": "; pub static DEFAULT_VI_NORMAL_PROMPT_INDICATOR: &str = "〉"; pub static DEFAULT_MULTILINE_INDICATOR: &str = "::: "; -/// Simple two-line [`Prompt`] displaying the current working directory and the time above the entry line. +/// Simple [`Prompt`] displaying a configurable left and a right prompt. +/// For more fine-tuned configuration, implement the [`Prompt`] trait. +/// For the default configuration, use [`DefaultPrompt::default()`] #[derive(Clone)] -pub struct DefaultPrompt; +pub struct DefaultPrompt { + /// What segment should be rendered in the left (main) prompt + pub left_prompt: DefaultPromptSegment, + /// What segment should be rendered in the right prompt + pub right_prompt: DefaultPromptSegment, +} + +/// A struct to control the appearance of the left or right prompt in a [`DefaultPrompt`] +#[derive(Clone)] +pub enum DefaultPromptSegment { + /// A basic user-defined prompt (i.e. just text) + Basic(String), + /// The path of the current working directory + WorkingDirectory, + /// The current date and time + CurrentDateTime, + /// An empty prompt segment + Empty, +} + +/// Given a prompt segment, render it to a Cow that we can use to +/// easily implement [`Prompt`]'s `render_prompt_left` and `render_prompt_right` +/// functions. +fn render_prompt_segment(prompt: &DefaultPromptSegment) -> Cow { + match &prompt { + DefaultPromptSegment::Basic(s) => Cow::Borrowed(s), + DefaultPromptSegment::WorkingDirectory => { + let prompt = get_working_dir().unwrap_or_else(|_| String::from("no path")); + Cow::Owned(prompt) + } + DefaultPromptSegment::CurrentDateTime => Cow::Owned(get_now()), + DefaultPromptSegment::Empty => Cow::Borrowed(""), + } +} impl Prompt for DefaultPrompt { fn render_prompt_left(&self) -> Cow { - { - let left_prompt = get_working_dir().unwrap_or_else(|_| String::from("no path")); - - Cow::Owned(left_prompt) - } + render_prompt_segment(&self.left_prompt) } fn render_prompt_right(&self) -> Cow { - { - Cow::Owned(get_now()) - } + render_prompt_segment(&self.right_prompt) } fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow { @@ -64,14 +93,25 @@ impl Prompt for DefaultPrompt { impl Default for DefaultPrompt { fn default() -> Self { - DefaultPrompt::new() + DefaultPrompt { + left_prompt: DefaultPromptSegment::WorkingDirectory, + right_prompt: DefaultPromptSegment::CurrentDateTime, + } } } impl DefaultPrompt { - /// Constructor for the default prompt, which takes the amount of spaces required between the left and right-hand sides of the prompt - pub fn new() -> DefaultPrompt { - DefaultPrompt {} + /// Constructor for the default prompt, which takes a configurable left and right prompt. + /// For less customization, use [`DefaultPrompt::default`]. + /// For more fine-tuned configuration, implement the [`Prompt`] trait. + pub fn new( + left_prompt: DefaultPromptSegment, + right_prompt: DefaultPromptSegment, + ) -> DefaultPrompt { + DefaultPrompt { + left_prompt, + right_prompt, + } } } diff --git a/src/prompt/mod.rs b/src/prompt/mod.rs index 771563f..83d2e3b 100644 --- a/src/prompt/mod.rs +++ b/src/prompt/mod.rs @@ -5,4 +5,4 @@ pub use base::{ Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, PromptViMode, }; -pub use default::DefaultPrompt; +pub use default::{DefaultPrompt, DefaultPromptSegment};