Forwarding Prompt's multiline indicator (#185)

* Multiline continuation prompt introduced

* Hardcoded multiline continuation prompt styling

A way to convert `crossterm::style::Color` in `nu_ansi_term::Color` must
be found/implemented. This enables creating a Style from it.
Otherwise, it is necessary that the Style of the prompt is accessible
from everywhere.

* Forward Prompt's multline prompt to rendering func

* Finish #185

Co-authored-by: Antonio Natilla <antonio.natilla@studenti.unimi.it>
Co-authored-by: sholderbach <ho.steve@web.de>
This commit is contained in:
Antonio Natilla 2021-12-01 12:18:49 +01:00 committed by GitHub
parent 2bc7046066
commit 82bc3acb64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 14 deletions

View File

@ -817,7 +817,7 @@ impl Reedline {
if self.require_wrapping() {
let position = cursor::position()?;
self.wrap(position)?;
self.wrap(position, prompt)?;
}
self.editor.remember_undo_state(false);
@ -911,9 +911,9 @@ impl Reedline {
/// TODO! FIX the naming and provide an accurate doccomment
/// This function repaints and updates offsets but does not purely concern it self with wrapping
fn wrap(&mut self, original_position: (u16, u16)) -> io::Result<()> {
fn wrap(&mut self, original_position: (u16, u16), prompt: &dyn Prompt) -> io::Result<()> {
let (original_column, original_row) = original_position;
self.buffer_paint(self.prompt_widget.offset)?;
self.buffer_paint(prompt, self.prompt_widget.offset)?;
let (new_column, _) = cursor::position()?;
@ -990,7 +990,7 @@ impl Reedline {
if self.input_mode == InputMode::HistorySearch {
self.history_search_paint(prompt)?;
} else {
self.buffer_paint(self.prompt_widget.offset)?;
self.buffer_paint(prompt, self.prompt_widget.offset)?;
}
Ok(())
@ -1035,14 +1035,17 @@ impl Reedline {
///
/// Requires coordinates where the input buffer begins after the prompt.
/// Performs highlighting and hinting at the moment!
fn buffer_paint(&mut self, prompt_offset: (u16, u16)) -> Result<()> {
fn buffer_paint(&mut self, prompt: &dyn Prompt, prompt_offset: (u16, u16)) -> Result<()> {
let cursor_position_in_buffer = self.editor.offset();
let buffer_to_paint = self.editor.get_buffer();
let highlighted_line = self
.highlighter
.highlight(buffer_to_paint)
.render_around_insertion_point(cursor_position_in_buffer);
.render_around_insertion_point(
cursor_position_in_buffer,
prompt.render_prompt_multiline_indicator(),
);
let hint: String = self.hinter.handle(
buffer_to_paint,
cursor_position_in_buffer,
@ -1072,7 +1075,10 @@ impl Reedline {
let highlighted_line = self
.highlighter
.highlight(buffer_to_paint)
.render_around_insertion_point(cursor_position_in_buffer);
.render_around_insertion_point(
cursor_position_in_buffer,
prompt.render_prompt_multiline_indicator(),
);
let hint: String = self.hinter.handle(
buffer_to_paint,
cursor_position_in_buffer,

View File

@ -1,3 +1,5 @@
use std::borrow::{Borrow, Cow};
use nu_ansi_term::{Color, Style};
/// A representation of a buffer with styling, used for doing syntax highlighting
@ -30,7 +32,7 @@ impl StyledText {
pub fn render_around_insertion_point(
&self,
insertion_point: usize,
// prompt_style: &Style,
multiline_prompt: Cow<str>,
) -> (String, String) {
let mut current_idx = 0;
let mut left_string = String::new();
@ -38,17 +40,33 @@ impl StyledText {
let prompt_style = Style::new().fg(Color::LightBlue);
for pair in &self.buffer {
if current_idx >= insertion_point {
right_string.push_str(&render_as_string(pair, &prompt_style));
right_string.push_str(&render_as_string(
pair,
&prompt_style,
multiline_prompt.borrow(),
));
} else if pair.1.len() + current_idx <= insertion_point {
left_string.push_str(&render_as_string(pair, &prompt_style));
left_string.push_str(&render_as_string(
pair,
&prompt_style,
multiline_prompt.borrow(),
));
} else if pair.1.len() + current_idx > insertion_point {
let offset = insertion_point - current_idx;
let left_side = pair.1[..offset].to_string();
let right_side = pair.1[offset..].to_string();
left_string.push_str(&render_as_string(&(pair.0, left_side), &prompt_style));
right_string.push_str(&render_as_string(&(pair.0, right_side), &prompt_style));
left_string.push_str(&render_as_string(
&(pair.0, left_side),
&prompt_style,
multiline_prompt.borrow(),
));
right_string.push_str(&render_as_string(
&(pair.0, right_side),
&prompt_style,
multiline_prompt.borrow(),
));
}
current_idx += pair.1.len();
}
@ -57,11 +75,16 @@ impl StyledText {
}
}
fn render_as_string(renderable: &(Style, String), prompt_style: &Style) -> String {
fn render_as_string(
renderable: &(Style, String),
prompt_style: &Style,
multiline_prompt: &str,
) -> String {
let mut rendered = String::new();
let formatted_multiline_prompt = format!("\n{}", multiline_prompt);
for (line_number, line) in renderable.1.split('\n').enumerate() {
if line_number != 0 {
rendered.push_str(&prompt_style.paint("\n: ").to_string());
rendered.push_str(&prompt_style.paint(&formatted_multiline_prompt).to_string());
}
rendered.push_str(&renderable.0.paint(line).to_string());
}