use std::time::Duration;
use yacll::{
input::{Event, KeyCode, MouseEventKind, MouseButton},
stylize::{Styler, Style, Color, reset, Attr},
Point,
ClrT,
Result, Yogurt,
};
fn main() -> Result<()> {
let mut y = Yogurt::new();
let mut color = 1;
let mut size = 0;
y.enter_alt()?;
y.cursor_on(yacll::stylize::CursorAttr::Hidden)?;
y.enable_mouse_capture()?;
loop {
y.style(styler(color))?;
y.mv_print((1, 0), "██████████")?;
y.style(reset())?;
y.print(' ')?;
for i in 1..=8 {
y.style(styler(i))?;
y.style(invert())?;
y.print(format!(" {i} "))?;
y.style(reset())?;
}
y.mv_print((36, 0), " ")?;
y.style(invert())?;
if size < 10 {
y.mv_print((37, 0), format!(" 0{size} "))?;
} else {
y.mv_print((37, 0), format!(" {size} "))?;
}
y.style(reset())?;
if let Some(some) = y.read_event(Duration::from_millis(10))? {
if let Event::Key(ke) = some {
match ke.code {
KeyCode::Char('q') => break,
KeyCode::Char('c') => y.clear(ClrT::All)?,
KeyCode::Char('1') => color = 1,
KeyCode::Char('2') => color = 2,
KeyCode::Char('3') => color = 3,
KeyCode::Char('4') => color = 4,
KeyCode::Char('5') => color = 5,
KeyCode::Char('6') => color = 6,
KeyCode::Char('7') => color = 7,
KeyCode::Char('8') => color = 8,
_ => (),
}
} else if let Event::Mouse(me) = some {
if me.kind == MouseEventKind::Down(MouseButton::Left) || me.kind == MouseEventKind::Drag(MouseButton::Left) {
y.style(styler(color))?;
for point in square(Point::from((me.column, me.row)), size) {
y.mv_print(point, '█')?;
}
y.style(reset())?;
} else if me.kind == MouseEventKind::Down(MouseButton::Right) || me.kind == MouseEventKind::Drag(MouseButton::Right) {
for point in square(Point::from((me.column, me.row)), size) {
y.mv_print(point, ' ')?;
}
} else if me.kind == MouseEventKind::ScrollDown && size > 0 {
size -= 1;
} else if me.kind == MouseEventKind::ScrollUp && size < 99 {
size += 1;
}
} else if let Event::Resize(cols, rows) = some {
y.resize(cols, rows)?;
}
}
y.flush()?;
}
y.disable_mouse_capture()?;
y.cursor_off(yacll::stylize::CursorAttr::Hidden)?;
y.leave_alt()?;
Ok(())
}
fn square(point: Point, size: u16) -> Vec<Point> {
let mut v = Vec::new();
let up_left = Point::from((point.x.saturating_sub(size), point.y.saturating_sub(size)));
let down_right = Point::from((point.x + size, point.y + size));
for x in up_left.x..=down_right.x {
for y in up_left.y..=down_right.y {
v.push(Point::from((x, y)));
}
}
v
}
fn white() -> Style {
Styler::new()
.foreground(Color::White)
.attr(Attr::Bold)
.style()
}
fn black() -> Style {
Styler::new()
.foreground(Color::Black)
.background(Color::White)
.attr(Attr::Bold)
.style()
}
fn red() -> Style {
Styler::new()
.foreground(Color::Red)
.attr(Attr::Bold)
.style()
}
fn green() -> Style {
Styler::new()
.foreground(Color::Green)
.attr(Attr::Bold)
.style()
}
fn yellow() -> Style {
Styler::new()
.foreground(Color::Yellow)
.attr(Attr::Bold)
.style()
}
fn blue() -> Style {
Styler::new()
.foreground(Color::Blue)
.attr(Attr::Bold)
.style()
}
fn magenta() -> Style {
Styler::new()
.foreground(Color::Magenta)
.attr(Attr::Bold)
.style()
}
fn cyan() -> Style {
Styler::new()
.foreground(Color::Cyan)
.attr(Attr::Bold)
.style()
}
fn styler(style: u8) -> Style {
match style {
1 => white(),
2 => black(),
3 => red(),
4 => green(),
5 => yellow(),
6 => blue(),
7 => magenta(),
8 => cyan(),
_ => white(),
}
}
fn invert() -> Style {
Styler::new()
.attr(Attr::Inverse)
.style()
}