1 | use std::fmt::Debug; |
2 | use std::io; |
3 | |
4 | use console::Term; |
5 | |
6 | /// A trait for minimal terminal-like behavior. |
7 | /// |
8 | /// Anything that implements this trait can be used a draw target via [`ProgressDrawTarget::term_like`]. |
9 | /// |
10 | /// [`ProgressDrawTarget::term_like`]: crate::ProgressDrawTarget::term_like |
11 | pub trait TermLike: Debug + Send + Sync { |
12 | /// Return the terminal width |
13 | fn width(&self) -> u16; |
14 | /// Return the terminal height |
15 | fn height(&self) -> u16 { |
16 | // FIXME: remove this default impl in the next major version bump |
17 | 20 // sensible default |
18 | } |
19 | |
20 | /// Move the cursor up by `n` lines |
21 | fn move_cursor_up(&self, n: usize) -> io::Result<()>; |
22 | /// Move the cursor down by `n` lines |
23 | fn move_cursor_down(&self, n: usize) -> io::Result<()>; |
24 | /// Move the cursor right by `n` chars |
25 | fn move_cursor_right(&self, n: usize) -> io::Result<()>; |
26 | /// Move the cursor left by `n` chars |
27 | fn move_cursor_left(&self, n: usize) -> io::Result<()>; |
28 | |
29 | /// Write a string and add a newline. |
30 | fn write_line(&self, s: &str) -> io::Result<()>; |
31 | /// Write a string |
32 | fn write_str(&self, s: &str) -> io::Result<()>; |
33 | /// Clear the current line and reset the cursor to beginning of the line |
34 | fn clear_line(&self) -> io::Result<()>; |
35 | |
36 | fn flush(&self) -> io::Result<()>; |
37 | } |
38 | |
39 | impl TermLike for Term { |
40 | fn width(&self) -> u16 { |
41 | self.size().1 |
42 | } |
43 | |
44 | fn height(&self) -> u16 { |
45 | self.size().0 |
46 | } |
47 | |
48 | fn move_cursor_up(&self, n: usize) -> io::Result<()> { |
49 | self.move_cursor_up(n) |
50 | } |
51 | |
52 | fn move_cursor_down(&self, n: usize) -> io::Result<()> { |
53 | self.move_cursor_down(n) |
54 | } |
55 | |
56 | fn move_cursor_right(&self, n: usize) -> io::Result<()> { |
57 | self.move_cursor_right(n) |
58 | } |
59 | |
60 | fn move_cursor_left(&self, n: usize) -> io::Result<()> { |
61 | self.move_cursor_left(n) |
62 | } |
63 | |
64 | fn write_line(&self, s: &str) -> io::Result<()> { |
65 | self.write_line(s) |
66 | } |
67 | |
68 | fn write_str(&self, s: &str) -> io::Result<()> { |
69 | self.write_str(s) |
70 | } |
71 | |
72 | fn clear_line(&self) -> io::Result<()> { |
73 | self.clear_line() |
74 | } |
75 | |
76 | fn flush(&self) -> io::Result<()> { |
77 | self.flush() |
78 | } |
79 | } |
80 | |