1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
//! Utilities for the terminal UI of toipe.
use std::{
fmt::Display,
io::{stdout, Stdout, Write},
};
use termion::{
clear,
color::{self, Color},
cursor::{self, DetectCursorPos},
raw::{IntoRawMode, RawTerminal},
style, terminal_size,
};
use crate::ToipeError;
use anyhow::Result;
const MIN_LINE_WIDTH: usize = 50;
/// Describes something that has a printable length.
///
/// For example, a string containing color characters has a different
/// length when printed than the number of bytes or chars in it.
pub trait HasLength {
/// number of char widths taken when printed on the terminal
fn length(&self) -> usize;
}
/// Holds some text that is to be printed on the terminal.
///
/// This provides an abstraction for
/// - retrieving the number of actual characters when printed on the
/// terminal through the [`HasLength`] trait.
/// - for formatting the text through the various `with_*` methods.
///
/// Usually, this is used in the slice form as `&[Text]` since a
/// single [`Text`] only holds one string with all of it formatted in
/// the same way. For example, you cannot format one part of a [`Text`]
/// with green color while the rest is in red. You should instead use a
/// slice of [`Text`]s with each formatted in a different way.
#[derive(Debug, Clone)]
pub struct Text {
/// the raw text
// TODO: make this private
raw_text: String,
/// text without formatting
text: String,
/// actual number of char width taken when printed on the terminal
length: usize,
}
impl Text {
/// Constructs a new Text from a raw string
///
/// NOTE: ensure that this string does not itself have formatting
/// characters, zero-width characters or multi-width characters.
pub fn new(text: String) -> Self {
let length = text.len();
Self {
raw_text: text.clone(),
text,
length,
}
}
/// the raw text with all formatting
// TODO: remove this
pub fn raw_text(&self) -> &String {
&self.raw_text
}
/// the actual printed text without formatting
pub fn text(&self) -> &String {
&self.text
}
/// adds faint style to the text
pub fn with_faint(mut self) -> Self {
self.raw_text = format!("{}{}{}", style::Faint, self.raw_text, style::NoFaint);
self
}
/// adds underline to the text
pub fn with_underline(mut self) -> Self {
self.raw_text = format!("{}{}{}", style::Underline, self.raw_text, style::Reset);
self
}
/// adds given color to the text
pub fn with_color<C>(mut self, color: C) -> Self
where
C: Color,
{
self.raw_text = format!(
"{}{}{}",
color::Fg(color),
self.raw_text,
color::Fg(color::Reset)
);
self
}
}
impl HasLength for Text {
fn length(&self) -> usize {
self.length
}
}
/// NOTE: note to be confused with `.len()` which provides the number
/// of elements in the slice.
impl HasLength for [Text] {
fn length(&self) -> usize {
self.iter().map(|t| t.length()).sum()
}
}
impl From<String> for Text {
/// Constructs a new Text from a raw string
///
/// NOTE: ensure that this string does not itself have formatting
/// characters, zero-width characters or multi-width characters.
fn from(text: String) -> Self {
Self::new(text)
}
}
impl From<&str> for Text {
/// Constructs a new Text from a raw string
///
/// NOTE: ensure that this string does not itself have formatting
/// characters, zero-width characters or multi-width characters.
fn from(text: &str) -> Self {
Self::new(text.to_string())
}
}
impl From<char> for Text {
/// Constructs a new Text from a character
///
/// NOTE: ensure that this character is itself not a formatting
/// character, zero-width character or a multi-width character.
fn from(c: char) -> Self {
Self::new(c.to_string())
}
}
/// Displays the raw string as-is. No surprises here.
impl Display for Text {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.raw_text)
}
}
/// the position of a line of words
#[derive(Clone, Copy)]
struct LinePos {
/// y-position of line in the terminal window
pub y: u16,
/// x-position of the first char in the line
pub x: u16,
/// length (number of chars) in this line
pub length: u16,
}
/// TODO: document this
struct CursorPos {
pub lines: Vec<LinePos>,
pub cur_line: usize,
pub cur_char_in_line: u16,
}
impl CursorPos {
pub fn new() -> Self {
Self {
lines: Vec::new(),
cur_line: 0,
cur_char_in_line: 0,
}
}
pub fn next(&mut self) -> (u16, u16) {
let line = self.lines[self.cur_line];
let max_chars_index = line.length - 1;
if self.cur_char_in_line < max_chars_index {
// more chars in line
self.cur_char_in_line += 1;
} else {
// reached the end of line
if self.cur_line + 1 < self.lines.len() {
// more lines available
self.cur_line += 1;
self.cur_char_in_line = 0;
}
}
self.cur_pos()
}
pub fn prev(&mut self) -> (u16, u16) {
if self.cur_char_in_line > 0 {
// more chars behind in line
self.cur_char_in_line -= 1;
} else {
// reached the start of line
if self.cur_line > 0 {
// more lines available
self.cur_line -= 1;
self.cur_char_in_line = self.lines[self.cur_line].length - 1;
}
}
self.cur_pos()
}
pub fn cur_pos(&self) -> (u16, u16) {
let line = self.lines[self.cur_line];
(line.x + self.cur_char_in_line, line.y)
}
}
/// terminal UI of toipe
pub struct ToipeTui {
stdout: RawTerminal<Stdout>,
cursor_pos: CursorPos,
track_lines: bool,
bottom_lines_len: usize,
}
type MaybeError<T = ()> = Result<T>;
impl ToipeTui {
/// Initializes stdout in raw mode for the TUI.
///
/// NOTE: does not clear the screen when initialized.
pub fn new() -> Self {
Self {
stdout: stdout().into_raw_mode().unwrap(),
cursor_pos: CursorPos::new(),
track_lines: false,
bottom_lines_len: 0,
}
}
pub fn reset(&mut self) {
self.cursor_pos = CursorPos::new();
}
// TODO: make this private
/// Flushes stdout
pub fn flush(&mut self) -> MaybeError {
self.stdout.flush()?;
Ok(())
}
/// Resets the TUI.
///
/// Clears screen, moves cursor to the center and changes cursor to
/// a blinking bar.
pub fn reset_screen(&mut self) -> MaybeError {
let (sizex, sizey) = terminal_size()?;
write!(
self.stdout,
"{}{}{}",
clear::All,
cursor::Goto(sizex / 2, sizey / 2),
cursor::BlinkingBar
)?;
self.flush()?;
Ok(())
}
/// Displays a single line of text.
///
/// - A line of text is described by a slice of [`Text`]s, they are
/// concatenated and displayed on the same line.
///
/// - The line is displayed on whatever Y-position the cursor is
/// currently on.
///
/// - The line is centered horizontally.
pub fn display_a_line(&mut self, text: &[Text]) -> MaybeError {
self.display_a_line_raw(text)?;
self.flush()?;
Ok(())
}
/// Same as [`display_a_line`] but without the flush.
fn display_a_line_raw<T, U>(&mut self, text: U) -> MaybeError
where
U: AsRef<[T]>,
[T]: HasLength,
T: Display,
{
let len = text.as_ref().length() as u16;
write!(self.stdout, "{}", cursor::Left(len / 2),)?;
// TODO: find a better way to enable this only in certain contexts
if self.track_lines {
let (x, y) = self.stdout.cursor_pos()?;
self.cursor_pos.lines.push(LinePos { x, y, length: len });
}
for t in text.as_ref() {
self.display_raw_text(t)?;
}
write!(self.stdout, "{}", cursor::Left(len),)?;
Ok(())
}
/// Displays multiple lines of text.
///
/// - A line of text is described by a slice of [`Text`]s, they are
/// concatenated and displayed on the same line.
///
/// - The lines are centered vertically and each line itself is
/// centered horizontally.
// Ref for this generic thingy: https://stackoverflow.com/a/50056925/11199009
// TODO: document the generic stuff
pub fn display_lines<T, U>(&mut self, lines: &[T]) -> MaybeError
where
T: AsRef<[U]>,
[U]: HasLength,
U: Display,
{
let (sizex, sizey) = terminal_size()?;
let line_offset = lines.len() as u16 / 2;
for (line_no, line) in lines.iter().enumerate() {
write!(
self.stdout,
"{}",
cursor::Goto(sizex / 2, sizey / 2 + (line_no as u16) - line_offset)
)?;
self.display_a_line_raw(line.as_ref())?;
}
self.flush()?;
Ok(())
}
/// Displays multiple lines of text at the bottom of the screen.
///
/// See [`display_lines`] for more information.
pub fn display_lines_bottom<T, U>(&mut self, lines: &[T]) -> MaybeError
where
T: AsRef<[U]>,
[U]: HasLength,
U: Display,
{
let (sizex, sizey) = terminal_size()?;
let line_offset = lines.len() as u16;
self.bottom_lines_len = lines.len();
for (line_no, line) in lines.iter().enumerate() {
write!(
self.stdout,
"{}",
cursor::Goto(sizex / 2, sizey - 1 + (line_no as u16) - line_offset)
)?;
self.display_a_line_raw(line.as_ref())?;
}
self.flush()?;
Ok(())
}
// TODO: document this
pub fn display_words(&mut self, words: &[String]) -> MaybeError<Vec<Text>> {
self.reset();
let mut current_len = 0;
let mut max_word_len = 0;
let mut line = Vec::new();
let mut lines = Vec::new();
let (terminal_width, terminal_height) = terminal_size()?;
// 40% of terminal width
let max_width = terminal_width * 2 / 5;
const MAX_WORDS_PER_LINE: usize = 10;
// eprintln!("max width is {}", max_width);
for word in words {
max_word_len = std::cmp::max(max_word_len, word.len() + 1);
let new_len = current_len + word.len() as u16 + 1;
if line.len() < MAX_WORDS_PER_LINE && new_len <= max_width {
// add to line
line.push(word.clone());
current_len += word.len() as u16 + 1
} else {
// add an extra space at the end of each line because
// user will instinctively type a space after every word
// (at least I did)
lines.push(Text::from(line.join(" ") + " ").with_faint());
// clear line
line = vec![word.clone()];
current_len = word.len() as u16 + 1;
}
}
// last line wasn't added in loop
// last line doesn't have an extra space at the end
// - the typing test stops as soon as the user types last char
// - won't hang there waiting for user to type space
lines.push(Text::from(line.join(" ")).with_faint());
max_word_len = std::cmp::max(max_word_len + 1, MIN_LINE_WIDTH);
if lines.len() + self.bottom_lines_len + 2 > terminal_height as usize {
return Err(ToipeError::from(format!(
"Terminal height is too short! Toipe requires at least {} lines, got {} lines",
lines.len() + self.bottom_lines_len + 2,
terminal_height,
))
.into());
} else if max_word_len > terminal_width as usize {
return Err(ToipeError::from(format!(
"Terminal width is too low! Toipe requires at least {} columns, got {} columns",
max_word_len, terminal_width,
))
.into());
}
self.track_lines = true;
self.display_lines(
lines
.iter()
.cloned()
.map(|line| [line])
.collect::<Vec<[Text; 1]>>()
.as_slice(),
)?;
self.track_lines = false;
self.move_to_cur_pos()?;
self.flush()?;
Ok(lines)
}
/// Displays a [`Text`].
pub fn display_raw_text<T>(&mut self, text: &T) -> MaybeError
where
T: Display,
{
write!(self.stdout, "{}", text)?;
Ok(())
}
/// Hides the cursor.
pub fn hide_cursor(&mut self) -> MaybeError {
write!(self.stdout, "{}", cursor::Hide)?;
self.flush()?;
Ok(())
}
/// Shows the cursor.
pub fn show_cursor(&mut self) -> MaybeError {
write!(self.stdout, "{}", cursor::Show)?;
self.flush()?;
Ok(())
}
/// Replaces the text previous to the cursor with given text.
///
/// NOTE: only call this with [`Text`]s containing one character.
///
/// Last character is replaced with given text.
///
/// The text is described by a slice of [`Text`].
// TODO: enforce single character constrainst in compile time
pub fn replace_text<T>(&mut self, text: T) -> MaybeError
where
T: Display,
{
self.move_to_prev_char()?;
self.display_raw_text(&text)?;
self.move_to_cur_pos()?;
Ok(())
}
/// Moves the cursor to the next char
pub fn move_to_next_char(&mut self) -> MaybeError {
let (x, y) = self.cursor_pos.next();
write!(self.stdout, "{}", cursor::Goto(x, y))?;
Ok(())
}
/// Moves the cursor to the previous char
pub fn move_to_prev_char(&mut self) -> MaybeError {
let (x, y) = self.cursor_pos.prev();
write!(self.stdout, "{}", cursor::Goto(x, y))?;
Ok(())
}
/// Moves the cursor to just before the character to be typed next
pub fn move_to_cur_pos(&mut self) -> MaybeError {
let (x, y) = self.cursor_pos.cur_pos();
write!(self.stdout, "{}", cursor::Goto(x, y))?;
Ok(())
}
/// Returns the current line the cursor is on
pub fn current_line(&self) -> usize {
self.cursor_pos.cur_line
}
}
impl Default for ToipeTui {
fn default() -> Self {
Self::new()
}
}
impl Drop for ToipeTui {
/// Resets terminal.
///
/// Clears screen and sets the cursor to a non-blinking block.
///
/// TODO: print error message when terminal height/width is too small.
/// Take a look at https://github.com/Samyak2/toipe/pull/28#discussion_r851784291 for more info.
fn drop(&mut self) {
write!(
self.stdout,
"{}{}{}",
clear::All,
cursor::SteadyBlock,
cursor::Goto(1, 1)
)
.expect("Could not reset terminal while exiting");
self.flush().expect("Could not flush stdout while exiting");
}
}