yacll/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(missing_docs)]
3
4/*!
5A library designed to provide a curses alternative.
6
7Explaining things is hard, so here's an example:
8```
9use yacll::Yogurt;
10
11fn main() -> yacll::Result<()> {
12    let mut y = Yogurt::new();
13    // does not add newline
14    y.print("Hello, world!\n")?;
15    // flush the buffer (print just queues things for execution)
16    y.flush()?;
17    Ok(())
18}
19// try running `$ cargo run --example=quickstart` if you have the repository cloned!
20```
21For serializing/deserializing of some structs, the `serde` feature can be enabled.
22*/
23
24pub use crossterm::terminal::ClearType as ClrT;
25use std::io::{stdout, Stdout};
26use thiserror::Error;
27mod output;
28pub mod stylize;
29pub use output::*;
30pub mod input;
31
32/// **Y**et An**o**ther Data Stora**g**e **U** St**r**uc**t**. Main struct for methods.
33///
34/// Unless a method specifies it flushes the buffer, calling the [`flush`](Yogurt::flush) method will be required for the changes to take effect.
35#[derive(Debug)]
36pub struct Yogurt {
37    stdout: Stdout,
38    /// Terminal size before entering alternate mode
39    size: Option<Point>,
40}
41
42impl Yogurt {
43    /// Create a new yogurt.
44    pub fn new() -> Self {
45        Yogurt {
46            stdout: stdout(),
47            size: None,
48        }
49    }
50}
51
52impl Default for Yogurt {
53    fn default() -> Self {
54        Yogurt::new()
55    }
56}
57
58/// A point on the screen.
59#[derive(Debug, Copy, Clone)]
60pub struct Point {
61    /// Which column this point is on.
62    pub x: u16,
63    /// Which row this point is on.
64    pub y: u16,
65}
66
67impl From<(u16, u16)> for Point {
68    /// Convert a tuple to a point. The first tuple value is interpreted as `x`, while the second is interpreted as `y`.
69    fn from(t: (u16, u16)) -> Self {
70        Point { x: t.0, y: t.1 }
71    }
72}
73
74impl From<Point> for (u16, u16) {
75    fn from(point: Point) -> Self {
76        (point.x, point.y)
77    }
78}
79
80/// **Y**et **A**nother E**rr**or enum.
81#[non_exhaustive]
82#[derive(Error, Debug)]
83pub enum Yarr {
84    /// An IO error.
85    #[error(transparent)]
86    IO(#[from] std::io::Error),
87    /// Tried to use something that depended on alternate mode, but wasn't in it already.
88    #[error("tried to use something that depended on alternate mode, but wasn't in it already")]
89    NotInAltMode,
90    /// An error converting crossterm's KeyCode type to yacll's KeyCode type. This error will never
91    /// appear in public interfaces (because it should be impossible), so you do not have to worry about it.
92    #[error("tried to convert a crossterm keycode to a yacll keycode but failed")]
93    FromCrossKeyCodeError,
94}
95
96/// Result with the given type and an instance of `Yarr`.
97pub type Result<T> = core::result::Result<T, Yarr>;