diff options
Diffstat (limited to 'src/ui.rs')
| -rw-r--r-- | src/ui.rs | 121 |
1 files changed, 100 insertions, 21 deletions
| @@ -33,53 +33,79 @@ fn paint(code: &str, text: &str) -> String { | |||
| 33 | 33 | ||
| 34 | /// Streams to stdout. Colour is used only when stdout is a terminal, so piping | 34 | /// Streams to stdout. Colour is used only when stdout is a terminal, so piping |
| 35 | /// the output somewhere gives clean text. | 35 | /// the output somewhere gives clean text. |
| 36 | pub struct Renderer { | 36 | /// |
| 37 | /// Generic over the writer so the line-break rules can be tested without a | ||
| 38 | /// terminal: a reasoning stream does not always end with a newline, and the | ||
| 39 | /// answer must not be appended to the same line as the thinking. | ||
| 40 | pub struct Renderer<W: Write> { | ||
| 41 | out: W, | ||
| 42 | color: bool, | ||
| 37 | dimmed: bool, | 43 | dimmed: bool, |
| 38 | wrote_text: bool, | 44 | line_dirty: bool, |
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | impl Renderer { | 47 | impl Renderer<std::io::Stdout> { |
| 42 | pub fn new() -> Self { | 48 | pub fn new() -> Self { |
| 43 | Self { | 49 | Self { |
| 50 | out: std::io::stdout(), | ||
| 51 | color: color(), | ||
| 44 | dimmed: false, | 52 | dimmed: false, |
| 45 | wrote_text: false, | 53 | line_dirty: false, |
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | impl<W: Write> Renderer<W> { | ||
| 59 | pub fn with_color(out: W, color: bool) -> Self { | ||
| 60 | Self { | ||
| 61 | out, | ||
| 62 | color, | ||
| 63 | dimmed: false, | ||
| 64 | line_dirty: false, | ||
| 46 | } | 65 | } |
| 47 | } | 66 | } |
| 48 | 67 | ||
| 49 | fn undim(&mut self) { | 68 | fn close_reasoning(&mut self) { |
| 50 | if self.dimmed { | 69 | if !self.dimmed { |
| 51 | print!("\x1b[0m"); | 70 | return; |
| 52 | self.dimmed = false; | 71 | } |
| 72 | let _ = write!(self.out, "\x1b[0m"); | ||
| 73 | if self.line_dirty { | ||
| 74 | let _ = writeln!(self.out); | ||
| 75 | self.line_dirty = false; | ||
| 53 | } | 76 | } |
| 77 | self.dimmed = false; | ||
| 54 | } | 78 | } |
| 55 | } | 79 | } |
| 56 | 80 | ||
| 57 | impl Sink for Renderer { | 81 | impl<W: Write> Sink for Renderer<W> { |
| 58 | fn text(&mut self, s: &str) { | 82 | fn text(&mut self, s: &str) { |
| 59 | self.undim(); | 83 | self.close_reasoning(); |
| 60 | print!("{s}"); | 84 | let _ = write!(self.out, "{s}"); |
| 61 | let _ = std::io::stdout().flush(); | 85 | let _ = self.out.flush(); |
| 62 | self.wrote_text = true; | 86 | self.line_dirty = !s.ends_with('\n'); |
| 63 | } | 87 | } |
| 64 | 88 | ||
| 65 | fn reasoning(&mut self, s: &str) { | 89 | fn reasoning(&mut self, s: &str) { |
| 66 | if !color() { | 90 | if !self.color { |
| 67 | return; | 91 | return; |
| 68 | } | 92 | } |
| 69 | if !self.dimmed { | 93 | if !self.dimmed { |
| 70 | print!("\x1b[2m"); | 94 | let _ = write!(self.out, "\x1b[2m"); |
| 71 | self.dimmed = true; | 95 | self.dimmed = true; |
| 72 | } | 96 | } |
| 73 | print!("{s}"); | 97 | let _ = write!(self.out, "{s}"); |
| 74 | let _ = std::io::stdout().flush(); | 98 | let _ = self.out.flush(); |
| 99 | self.line_dirty = !s.ends_with('\n'); | ||
| 75 | } | 100 | } |
| 76 | 101 | ||
| 77 | fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { | 102 | fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { |
| 78 | self.undim(); | 103 | self.close_reasoning(); |
| 79 | if self.wrote_text { | 104 | if self.line_dirty { |
| 80 | println!(); | 105 | let _ = writeln!(self.out); |
| 81 | self.wrote_text = false; | 106 | self.line_dirty = false; |
| 82 | } | 107 | } |
| 108 | let _ = self.out.flush(); | ||
| 83 | } | 109 | } |
| 84 | } | 110 | } |
| 85 | 111 | ||
| @@ -234,3 +260,56 @@ impl Approver { | |||
| 234 | == Answer::Yes | 260 | == Answer::Yes |
| 235 | } | 261 | } |
| 236 | } | 262 | } |
| 263 | |||
| 264 | #[cfg(test)] | ||
| 265 | mod tests { | ||
| 266 | use super::*; | ||
| 267 | |||
| 268 | fn rendered(color: bool, run: impl FnOnce(&mut Renderer<Vec<u8>>)) -> String { | ||
| 269 | let mut renderer = Renderer::with_color(Vec::new(), color); | ||
| 270 | run(&mut renderer); | ||
| 271 | String::from_utf8(renderer.out).unwrap() | ||
| 272 | } | ||
| 273 | |||
| 274 | #[test] | ||
| 275 | fn the_answer_starts_on_its_own_line_after_reasoning() { | ||
| 276 | let text = rendered(true, |renderer| { | ||
| 277 | renderer.reasoning("thinking without a newline"); | ||
| 278 | renderer.text("the answer"); | ||
| 279 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 280 | }); | ||
| 281 | assert_eq!( | ||
| 282 | text, | ||
| 283 | "\u{1b}[2mthinking without a newline\u{1b}[0m\nthe answer\n" | ||
| 284 | ); | ||
| 285 | } | ||
| 286 | |||
| 287 | #[test] | ||
| 288 | fn reasoning_that_already_ended_with_a_newline_gets_no_blank_line() { | ||
| 289 | let text = rendered(true, |renderer| { | ||
| 290 | renderer.reasoning("thinking\n"); | ||
| 291 | renderer.text("answer"); | ||
| 292 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 293 | }); | ||
| 294 | assert_eq!(text, "\u{1b}[2mthinking\n\u{1b}[0manswer\n"); | ||
| 295 | } | ||
| 296 | |||
| 297 | #[test] | ||
| 298 | fn reasoning_alone_still_ends_the_line() { | ||
| 299 | let text = rendered(true, |renderer| { | ||
| 300 | renderer.reasoning("only thinking"); | ||
| 301 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 302 | }); | ||
| 303 | assert_eq!(text, "\u{1b}[2monly thinking\u{1b}[0m\n"); | ||
| 304 | } | ||
| 305 | |||
| 306 | #[test] | ||
| 307 | fn reasoning_is_dropped_when_colour_is_off() { | ||
| 308 | let text = rendered(false, |renderer| { | ||
| 309 | renderer.reasoning("invisible"); | ||
| 310 | renderer.text("answer"); | ||
| 311 | renderer.done(&Outcome::default(), Duration::ZERO); | ||
| 312 | }); | ||
| 313 | assert_eq!(text, "answer\n"); | ||
| 314 | } | ||
| 315 | } | ||
