Skip to content

Commit

Permalink
add optional custom delimiter for text_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Aug 24, 2024
1 parent 263d6aa commit a4d7c0f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::{HashMap, VecDeque};
use super::*;

// TODO notes:
// - I wonder if we can wrap the lines in Additional Settings, including the keyname ( display_name) and value ( display_value )
// - the instructions do not wrap. I wonder though if we can wrap on strings like " | ", would there be a need for a method wrapped_spans_with_delim() ?
// - add ability to attach filepaths to a profile. This should be done as a NewProfileCreationStep. As a first step, just make it a simple input field.
// - replace input field in previous step by an EditWindow that can contain multiple lines for input
Expand Down Expand Up @@ -577,7 +576,8 @@ impl NewProfileCreator {
style: Style,
) -> Vec<ListItem> {
let simple_string = SimpleString::from(message);
let wrapped_spans = simple_string.wrapped_spans(width, Some(style));
let wrapped_spans =
simple_string.wrapped_spans(width, Some(style), None);
wrapped_spans
.into_iter()
.map(Line::from)
Expand Down Expand Up @@ -637,6 +637,7 @@ impl NewProfileCreator {
let wrapped_spans = ready_message.wrapped_spans(
area.width as usize - 4,
Some(Style::default().fg(Self::COLOR_SECONDARY)),
None,
);
for spans in wrapped_spans {
items.push(ListItem::new(Line::from(spans)));
Expand Down Expand Up @@ -706,8 +707,11 @@ impl NewProfileCreator {
// Wrap the value part using SimpleString
let value_width = available_width.saturating_sub(key_width);
let simple_string = SimpleString::from(input_content);
let wrapped_value =
simple_string.wrapped_spans(value_width, Some(input_style));
let wrapped_value = simple_string.wrapped_spans(
value_width,
Some(input_style),
None,
);

// Render the key-value pair
for (i, value_spans) in wrapped_value.into_iter().enumerate() {
Expand Down Expand Up @@ -1462,8 +1466,11 @@ impl NewProfileCreator {
let value_width = area.width as usize - 4 - key_width;
let simple_string =
SimpleString::from(format!("{}{}", display_value, status));
let wrapped_value =
simple_string.wrapped_spans(value_width, Some(value_style));
let wrapped_value = simple_string.wrapped_spans(
value_width,
Some(value_style),
None,
);

// Render the key-value pair
for (i, value_spans) in wrapped_value.into_iter().enumerate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a> TextDisplay<'a> {
line.segments().map(|s| s.text.as_str()).collect::<String>();
let trailing_spaces =
text_str.len() - text_str.trim_end_matches(' ').len();
let wrapped_lines = text_wrapper.wrap_text_styled(line, None);
let wrapped_lines = text_wrapper.wrap_text_styled(line, None, None);
if wrapped_lines.is_empty() {
self.handle_empty_line(trailing_spaces, line.get_background());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl SimpleString {
&self,
width: usize,
style: Option<Style>,
delimiter: Option<&str>,
) -> Vec<Vec<Span<'static>>> {
let wrapper = TextWrapper::new(width);
let text_line = TextLine {
Expand All @@ -103,7 +104,7 @@ impl SimpleString {
length: self.len(),
background: None,
};
let wrapped_lines = wrapper.wrap_text_styled(&text_line, None);
let wrapped_lines = wrapper.wrap_text_styled(&text_line, None, None);

wrapped_lines
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ impl TextWrapper {
pub fn new(display_width: usize) -> Self {
Self { display_width }
}
// TODO: wrap_text_styled_with_delim

pub fn wrap_text_styled(
&self,
line: &TextLine,
first_line_max_width: Option<usize>,
delimiter: Option<&str>,
) -> Vec<TextLine> {
let max_display_width = self.display_width.saturating_sub(2);
let max_first_line_width =
Expand Down Expand Up @@ -57,6 +57,13 @@ impl TextWrapper {

if word.len() > remaining_width {
if !current_line.is_empty() {
if let Some(delim) = delimiter {
self.add_delimiter(
&mut current_line,
delim,
segment.style.as_ref(),
);
}
wrapped_lines.push(std::mem::replace(
&mut current_line,
TextLine::new(),
Expand All @@ -70,6 +77,7 @@ impl TextWrapper {
&mut wrapped_lines,
&mut remaining_width,
max_display_width,
delimiter,
);
} else {
current_line.add_segment(
Expand All @@ -82,27 +90,24 @@ impl TextWrapper {
}

if !current_line.is_empty() {
if let Some(delim) = delimiter {
self.add_delimiter(&mut current_line, delim, None);
}
wrapped_lines.push(current_line);
}

wrapped_lines
}

fn handle_code_block(
fn add_delimiter(
&self,
current_line: &mut TextLine,
wrapped_lines: &mut Vec<TextLine>,
line: &mut TextLine,
delimiter: &str,
style: Option<&Style>,
remaining_width: &mut usize,
max_display_width: usize,
) {
if !current_line.is_empty() {
wrapped_lines
.push(std::mem::replace(current_line, TextLine::new()));
if line.get_length() + delimiter.len() <= self.display_width {
line.add_segment(SimpleString::from_str(delimiter), style.cloned());
}
current_line.add_segment(SimpleString::from("```"), style.cloned());
wrapped_lines.push(std::mem::replace(current_line, TextLine::new()));
*remaining_width = max_display_width;
}

fn handle_long_word(
Expand All @@ -113,6 +118,7 @@ impl TextWrapper {
wrapped_lines: &mut Vec<TextLine>,
remaining_width: &mut usize,
max_display_width: usize,
delimiter: Option<&str>,
) {
let mut start = 0;
while start < word.len() {
Expand All @@ -123,6 +129,9 @@ impl TextWrapper {
*remaining_width -= slice.len();

if *remaining_width == 0 && end < word.len() {
if let Some(delim) = delimiter {
self.add_delimiter(current_line, delim, style);
}
wrapped_lines
.push(std::mem::replace(current_line, TextLine::new()));
*remaining_width = max_display_width;
Expand All @@ -131,6 +140,23 @@ impl TextWrapper {
start = end;
}
}

fn handle_code_block(
&self,
current_line: &mut TextLine,
wrapped_lines: &mut Vec<TextLine>,
style: Option<&Style>,
remaining_width: &mut usize,
max_display_width: usize,
) {
if !current_line.is_empty() {
wrapped_lines
.push(std::mem::replace(current_line, TextLine::new()));
}
current_line.add_segment(SimpleString::from("```"), style.cloned());
wrapped_lines.push(std::mem::replace(current_line, TextLine::new()));
*remaining_width = max_display_width;
}
}

struct WordIterator<'a> {
Expand Down

0 comments on commit a4d7c0f

Please sign in to comment.