Skip to content

Commit

Permalink
update: remove leading space from rendered output (#57)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
yk-amarly-20 and yk-amarly-20 authored May 23, 2024
1 parent 9542af2 commit 7f18d36
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
16 changes: 8 additions & 8 deletions src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> Confirm<'a> {
fn render_success(&self) -> io::Result<String> {
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)?;
Expand Down Expand Up @@ -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())
);
Expand Down
16 changes: 8 additions & 8 deletions src/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'a> Dialog<'a> {
fn render_success(&self) -> io::Result<String> {
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,
Expand Down Expand Up @@ -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())
);
Expand Down
36 changes: 18 additions & 18 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,23 @@ 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)?,
}
}

out.set_color(&self.theme.input_prompt)?;
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()?;
Expand All @@ -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()?;
}

Expand Down Expand Up @@ -384,7 +384,7 @@ impl<'a> Input<'a> {
fn render_success(&mut self) -> io::Result<String> {
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()?;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
14 changes: 7 additions & 7 deletions src/multiselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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() {
Expand Down Expand Up @@ -436,7 +436,7 @@ impl<'a, T> MultiSelect<'a, T> {
fn render_success(&self, selected: &[String]) -> io::Result<String> {
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()?;
Expand Down Expand Up @@ -471,8 +471,8 @@ mod tests {

assert_eq!(
indoc! {
" Toppings
Select your toppings
"Toppings
Select your toppings
>[•] Lettuce
[•] Tomatoes
[ ] Charm Sauce
Expand Down Expand Up @@ -524,8 +524,8 @@ mod tests {
);
assert_eq!(
indoc! {
" things
pick a thing
"things
pick a thing
>[ ] First
[•] 2
[•] 3
Expand Down
40 changes: 20 additions & 20 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<'a, T> Select<'a, T> {
fn render_success(&self, selected: &str) -> io::Result<String> {
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()?;
Expand Down Expand Up @@ -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())
);
Expand Down Expand Up @@ -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())
);
Expand Down

0 comments on commit 7f18d36

Please sign in to comment.