From 7f18d3600978cd0eb44704ad06958ce759d236cc Mon Sep 17 00:00:00 2001 From: YutoKojima Date: Fri, 24 May 2024 03:36:22 +0900 Subject: [PATCH] update: remove leading space from rendered output (#57) * update: remove confirm heading whitespace * update: remove dialog heading whitespace * update: remove list heading whitespace * update: remove multiselect heading whitespace * update: remove select heading whitespace * update: remove input heading whitespace * update: reflect heading whitespace to cursor offset and update test --------- Co-authored-by: yk-amarly-20 --- src/confirm.rs | 16 ++++++++-------- src/dialog.rs | 16 ++++++++-------- src/input.rs | 36 ++++++++++++++++++------------------ src/list.rs | 2 +- src/multiselect.rs | 14 +++++++------- src/select.rs | 40 ++++++++++++++++++++-------------------- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/confirm.rs b/src/confirm.rs index 620cd59..60bd700 100644 --- a/src/confirm.rs +++ b/src/confirm.rs @@ -136,11 +136,11 @@ impl<'a> Confirm<'a> { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - writeln!(out, " {}", self.title)?; + writeln!(out, "{}", self.title)?; if !self.description.is_empty() { out.set_color(&self.theme.description)?; - write!(out, " {}", self.description)?; + write!(out, "{}", self.description)?; } writeln!(out, "\n")?; @@ -186,7 +186,7 @@ impl<'a> Confirm<'a> { fn render_success(&self) -> io::Result { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; out.set_color(&self.theme.selected_option)?; if self.selected { writeln!(out, " {}", self.affirmative)?; @@ -219,13 +219,13 @@ mod tests { assert_eq!( indoc! { - " Are you sure? - This will do a thing. + "Are you sure? + This will do a thing. - Yes! No. + Yes! No. - ←/→ toggle • y/n/enter submit - " + ←/→ toggle • y/n/enter submit + " }, without_ansi(confirm.render().unwrap().as_str()) ); diff --git a/src/dialog.rs b/src/dialog.rs index e1d7c06..d1ef267 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -167,11 +167,11 @@ impl<'a> Dialog<'a> { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - writeln!(out, " {}", self.title)?; + writeln!(out, "{}", self.title)?; if !self.description.is_empty() { out.set_color(&self.theme.description)?; - write!(out, " {}", self.description)?; + write!(out, "{}", self.description)?; } writeln!(out, "\n")?; @@ -220,7 +220,7 @@ impl<'a> Dialog<'a> { fn render_success(&self) -> io::Result { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; out.set_color(&self.theme.selected_option)?; writeln!( out, @@ -261,13 +261,13 @@ mod tests { assert_eq!( indoc! { - " Are you sure? - This will do a thing. + "Are you sure? + This will do a thing. - Ok Not sure Cancel + Ok Not sure Cancel - ←/→ toggle • o/n/c/enter submit - " + ←/→ toggle • o/n/c/enter submit + " }, without_ansi(dialog.render().unwrap().as_str()) ); diff --git a/src/input.rs b/src/input.rs index 0884c00..acbf993 100644 --- a/src/input.rs +++ b/src/input.rs @@ -278,15 +278,15 @@ impl<'a> Input<'a> { out.set_color(&self.theme.title)?; match self.inline { - true => write!(out, " {}", self.title)?, - false => writeln!(out, " {}", self.title)?, + true => write!(out, "{}", self.title)?, + false => writeln!(out, "{}", self.title)?, } out.set_color(&self.theme.description)?; if !self.description.is_empty() { match self.inline { - true => write!(out, "{}", self.description)?, - false => writeln!(out, " {}", self.description)?, + true => write!(out, " {}", self.description)?, + false => writeln!(out, "{}", self.description)?, } } @@ -294,7 +294,7 @@ impl<'a> Input<'a> { if !self.prompt.is_empty() { match self.inline { true => write!(out, "{}", self.prompt)?, - false => write!(out, " {}", self.prompt)?, + false => write!(out, "{}", self.prompt)?, } } out.reset()?; @@ -305,7 +305,7 @@ impl<'a> Input<'a> { out.set_color(&self.theme.error_indicator)?; writeln!(out)?; writeln!(out)?; - write!(out, " * {}", self.err.as_ref().unwrap())?; + write!(out, "* {}", self.err.as_ref().unwrap())?; out.reset()?; } @@ -384,7 +384,7 @@ impl<'a> Input<'a> { fn render_success(&mut self) -> io::Result { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; out.set_color(&self.theme.selected_option)?; writeln!(out, " {}", self.input)?; out.reset()?; @@ -444,7 +444,7 @@ impl<'a> Input<'a> { let err_count = self.err.as_ref().unwrap().chars().count(); self.term.move_cursor_left(err_count + 2)?; // 2 for the error prefix self.term.move_cursor_up(ERR_MSG_HEIGHT)?; - let mut offset = 1; // 1 for the column before the title/prompt + let mut offset = 0; if self.inline { offset += self.title.chars().count(); offset += self.description.chars().count(); @@ -496,7 +496,7 @@ mod tests { .placeholder("Placeholder"); assert_eq!( - " Title\n Description\n $ Placeholder\n", + "Title\nDescription\n$ Placeholder\n", without_ansi(input.render().unwrap().as_str()) ); } @@ -506,7 +506,7 @@ mod tests { let mut input = Input::new("Title"); assert_eq!( - " Title\n > \n", + "Title\n> \n", without_ansi(input.render().unwrap().as_str()) ); } @@ -516,7 +516,7 @@ mod tests { let mut input = Input::new("Title").description("Description"); assert_eq!( - " Title\n Description\n > \n", + "Title\nDescription\n> \n", without_ansi(input.render().unwrap().as_str()) ); } @@ -526,7 +526,7 @@ mod tests { let mut input = Input::new("Title").prompt("$ "); assert_eq!( - " Title\n $ \n", + "Title\n$ \n", without_ansi(input.render().unwrap().as_str()) ); } @@ -536,7 +536,7 @@ mod tests { let mut input = Input::new("Title").placeholder("Placeholder"); assert_eq!( - " Title\n > Placeholder\n", + "Title\n> Placeholder\n", without_ansi(input.render().unwrap().as_str()) ); } @@ -550,7 +550,7 @@ mod tests { .inline(true); assert_eq!( - " Title?Description.Prompt:Placeholder\n", + "Title? Description.Prompt:Placeholder\n", without_ansi(input.render().unwrap().as_str()) ); } @@ -564,14 +564,14 @@ mod tests { input.input = "".to_string(); input.validate().unwrap(); assert_eq!( - " Title\n Description\n > \n\n * Name cannot be empty\n", + "Title\nDescription\n> \n\n* Name cannot be empty\n", without_ansi(input.render().unwrap().as_str()) ); input.input = "non empty".to_string(); input.validate().unwrap(); assert_eq!( - " Title\n Description\n > non empty\n", + "Title\nDescription\n> non empty\n", without_ansi(input.render().unwrap().as_str()) ); } @@ -586,14 +586,14 @@ mod tests { input.input = "".to_string(); input.validate().unwrap(); assert_eq!( - " Title?Description.> \n\n * Name cannot be empty\n", + "Title? Description.> \n\n* Name cannot be empty\n", without_ansi(input.render().unwrap().as_str()) ); input.input = "non empty".to_string(); input.validate().unwrap(); assert_eq!( - " Title?Description.> non empty\n", + "Title? Description.> non empty\n", without_ansi(input.render().unwrap().as_str()) ); } diff --git a/src/list.rs b/src/list.rs index 539441e..773a402 100644 --- a/src/list.rs +++ b/src/list.rs @@ -284,7 +284,7 @@ impl<'a> List<'a> { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; for entry in self.items.iter().take(self.success_items) { out.set_color(&self.theme.unselected_option)?; diff --git a/src/multiselect.rs b/src/multiselect.rs index f1bf68a..9b2cd38 100644 --- a/src/multiselect.rs +++ b/src/multiselect.rs @@ -345,7 +345,7 @@ impl<'a, T> MultiSelect<'a, T> { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; if self.err.is_some() { out.set_color(&self.theme.error_indicator)?; @@ -355,7 +355,7 @@ impl<'a, T> MultiSelect<'a, T> { } if !self.description.is_empty() || self.pages > 1 { out.set_color(&self.theme.description)?; - write!(out, " {}", self.description)?; + write!(out, "{}", self.description)?; writeln!(out)?; } for (i, option) in self.visible_options().iter().enumerate() { @@ -436,7 +436,7 @@ impl<'a, T> MultiSelect<'a, T> { fn render_success(&self, selected: &[String]) -> io::Result { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; out.set_color(&self.theme.selected_option)?; writeln!(out, " {}", selected.join(", "))?; out.reset()?; @@ -471,8 +471,8 @@ mod tests { assert_eq!( indoc! { - " Toppings - Select your toppings + "Toppings + Select your toppings >[•] Lettuce [•] Tomatoes [ ] Charm Sauce @@ -524,8 +524,8 @@ mod tests { ); assert_eq!( indoc! { - " things - pick a thing + "things + pick a thing >[ ] First [•] 2 [•] 3 diff --git a/src/select.rs b/src/select.rs index 99ea843..23afda8 100644 --- a/src/select.rs +++ b/src/select.rs @@ -249,20 +249,20 @@ impl<'a, T> Select<'a, T> { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; writeln!(out)?; if !self.description.is_empty() || self.pages > 1 { out.set_color(&self.theme.description)?; - write!(out, " {}", self.description)?; + write!(out, "{}", self.description)?; writeln!(out)?; } for (i, option) in self.visible_options().iter().enumerate() { if self.cursor == i { out.set_color(&self.theme.cursor)?; - write!(out, " >")?; + write!(out, ">")?; } else { - write!(out, " ")?; + write!(out, " ")?; } out.set_color(&self.theme.unselected_option)?; writeln!(out, " {}", option.label)?; @@ -321,7 +321,7 @@ impl<'a, T> Select<'a, T> { fn render_success(&self, selected: &str) -> io::Result { let mut out = Buffer::ansi(); out.set_color(&self.theme.title)?; - write!(out, " {}", self.title)?; + write!(out, "{}", self.title)?; out.set_color(&self.theme.selected_option)?; writeln!(out, " {}", selected)?; out.reset()?; @@ -354,15 +354,15 @@ mod tests { assert_eq!( indoc! { - " Country - Select your Country - > United States - Germany - Brazil - Canada - Mexico - ↑/↓/k/j up/down • enter confirm - " + "Country + Select your Country + > United States + Germany + Brazil + Canada + Mexico + ↑/↓/k/j up/down • enter confirm + " }, without_ansi(select.render().unwrap().as_str()) ); @@ -399,12 +399,12 @@ mod tests { ); assert_eq!( indoc! { - " things - pick a thing - > First - 2 - ↑/↓/k/j up/down • enter confirm - " + "things + pick a thing + > First + 2 + ↑/↓/k/j up/down • enter confirm + " }, without_ansi(select.render().unwrap().as_str()) );