Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a4f820a14 | ||
|
|
e53c75097d | ||
|
|
d30da4dfe1 | ||
|
|
def18fed65 | ||
|
|
52eb3cc490 | ||
|
|
a35f420b68 | ||
|
|
65c8bb03d8 | ||
|
|
a168559b75 | ||
|
|
eb90f619ce | ||
|
|
5a9597aed4 |
+5
-1
@@ -6,7 +6,11 @@ license = "Apache-2.0"
|
||||
description = "A TUI library for advanced uis"
|
||||
repository = "https://github.com/osui-rs/engine"
|
||||
homepage = "https://osui.netlify.app"
|
||||
documentation = "https://osui.netlify.app/docs/"
|
||||
documentation = "https://osui.netlify.app/docs/"
|
||||
|
||||
[features]
|
||||
default = ["state"]
|
||||
state = []
|
||||
|
||||
[lib]
|
||||
name = "osmium_engine"
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
use crate::Element;
|
||||
|
||||
impl Element for &str {
|
||||
fn render(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Element for String {
|
||||
fn render(&self) -> String {
|
||||
self.to_owned()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
use crossterm::event::Event;
|
||||
|
||||
pub struct Frame {
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub event: Option<Event>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn new((width, height): (u16, u16)) -> Frame {
|
||||
Frame {
|
||||
width,
|
||||
height,
|
||||
event: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_component(&self, component: &mut crate::Component) {
|
||||
component(self)
|
||||
}
|
||||
|
||||
pub fn draw<E: crate::Element>(&self, mut element: E, props: crate::Props) {
|
||||
if let Some(event) = &self.event {
|
||||
element.event(event.to_owned());
|
||||
}
|
||||
|
||||
let s = element.render((props.width.get(self.width), props.height.get(self.height)));
|
||||
|
||||
let (width, height) = crate::utils::str_size(&s);
|
||||
|
||||
let (width, height) = (
|
||||
props.width.get_after(width, self.width),
|
||||
props.height.get_after(height, self.height),
|
||||
);
|
||||
|
||||
let (x, y) = (
|
||||
props.x.get(width, self.width),
|
||||
props.y.get(height, self.height),
|
||||
);
|
||||
|
||||
for (i, line) in s.lines().enumerate() {
|
||||
println!("\x1b[{};{}H{}\x1b[0m", (y + 1) as usize + i, x + 1, line,);
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
-125
@@ -1,150 +1,54 @@
|
||||
pub mod elements;
|
||||
pub mod style;
|
||||
pub mod frame;
|
||||
mod props;
|
||||
#[cfg(feature = "state")]
|
||||
pub mod state;
|
||||
pub mod utils;
|
||||
|
||||
use crossterm::event::Event;
|
||||
use frame::Frame;
|
||||
pub use props::*;
|
||||
pub use std::io::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::{
|
||||
style::{Color, Style},
|
||||
frame::Frame,
|
||||
props::{Color, Style},
|
||||
utils::init,
|
||||
Props, Result, Ui,
|
||||
Component, Element, Props, Result,
|
||||
};
|
||||
}
|
||||
|
||||
// Traits
|
||||
pub trait Framing {
|
||||
fn draw(&mut self, element: &dyn Element, props: &Props);
|
||||
#[cfg(feature = "state")]
|
||||
pub use crate::state::{default_state, use_state, State};
|
||||
|
||||
pub use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
}
|
||||
|
||||
pub trait Element {
|
||||
fn render(&self) -> String;
|
||||
fn get_colors(&self) -> std::collections::HashMap<String, (style::Color, bool)> {
|
||||
std::collections::HashMap::new()
|
||||
}
|
||||
fn render(&self, size: (u16, u16)) -> String;
|
||||
fn event(&mut self, _event: crossterm::event::Event) {}
|
||||
fn action(&mut self, _action: u16) {}
|
||||
}
|
||||
|
||||
// Types
|
||||
pub type Ui = Arc<dyn Fn(&mut dyn Framing)>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Props {
|
||||
x: style::Position,
|
||||
y: style::Position,
|
||||
width: style::Dimension,
|
||||
height: style::Dimension,
|
||||
style: style::Style,
|
||||
style_state: std::collections::HashMap<String, style::Style>,
|
||||
}
|
||||
pub type Component = Box<dyn FnMut(&Frame)>;
|
||||
|
||||
/// Represents the console state, containing a frame for rendering and a mouse capture flag.
|
||||
pub struct Console {
|
||||
mouse: bool,
|
||||
width: u16,
|
||||
height: u16,
|
||||
pub mouse_position: Option<(u16, u16)>,
|
||||
}
|
||||
|
||||
impl Props {
|
||||
pub fn new(x: u16, y: u16) -> Self {
|
||||
Self {
|
||||
x: style::Position::Num(x),
|
||||
y: style::Position::Num(y),
|
||||
width: style::Dimension::Auto,
|
||||
height: style::Dimension::Auto,
|
||||
style: style::Style::default(),
|
||||
style_state: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center() -> Self {
|
||||
Self {
|
||||
x: style::Position::Center,
|
||||
y: style::Position::Center,
|
||||
width: style::Dimension::Auto,
|
||||
height: style::Dimension::Auto,
|
||||
style: style::Style::default(),
|
||||
style_state: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center_horizontal() -> Self {
|
||||
Self {
|
||||
x: style::Position::Center,
|
||||
y: style::Position::Num(0),
|
||||
width: style::Dimension::Auto,
|
||||
height: style::Dimension::Auto,
|
||||
style: style::Style::default(),
|
||||
style_state: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center_vertical() -> Self {
|
||||
Self {
|
||||
x: style::Position::Num(0),
|
||||
y: style::Position::Center,
|
||||
width: style::Dimension::Auto,
|
||||
height: style::Dimension::Auto,
|
||||
style: style::Style::default(),
|
||||
style_state: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn style(&mut self, style: style::Style) -> Self {
|
||||
self.style = style;
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn state(&mut self, state: &str, style: style::Style) -> Self {
|
||||
self.style_state.insert(state.to_string(), style);
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Framing for Console {
|
||||
fn draw(&mut self, element: &dyn Element, props: &Props) {
|
||||
let written = element.render();
|
||||
|
||||
let (written_width, written_height) = utils::str_size(&written);
|
||||
|
||||
let (width, height) = (
|
||||
props.width.get_root(written_width, self.width),
|
||||
props.height.get_root(written_height, self.height),
|
||||
);
|
||||
|
||||
let (x, y) = (
|
||||
props.x.get(width, self.width),
|
||||
props.y.get(height, self.height),
|
||||
);
|
||||
|
||||
let mut style = props.style;
|
||||
|
||||
if let Some((mouse_x, mouse_y)) = self.mouse_position {
|
||||
if (x..x + width).contains(&mouse_x) && (y..y + height).contains(&mouse_y) {
|
||||
if let Some(hover_style) = props.style_state.get("hover") {
|
||||
style.color = hover_style.color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i, line) in written.lines().enumerate() {
|
||||
println!(
|
||||
"\x1b[{};{}H{}{}",
|
||||
y as usize + i + 1,
|
||||
x + 1,
|
||||
style.color.to_ansi(true),
|
||||
line
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Console {
|
||||
pub fn render(&mut self, ui: Ui) -> Result<()> {
|
||||
pub fn render(&mut self, ui: &mut Component) -> Result<()> {
|
||||
utils::clear()?;
|
||||
(self.width, self.height) = crossterm::terminal::size()?;
|
||||
ui(self);
|
||||
let mut frame = Frame::new(crossterm::terminal::size()?);
|
||||
ui(&mut frame);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn event(&mut self, ui: &mut Component, event: Event) -> Result<()> {
|
||||
utils::clear()?;
|
||||
let mut frame = Frame::new(crossterm::terminal::size()?);
|
||||
frame.event = Some(event);
|
||||
ui(&mut frame);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -157,3 +61,15 @@ impl Console {
|
||||
utils::clear()
|
||||
}
|
||||
}
|
||||
|
||||
impl Element for &str {
|
||||
fn render(&self, _: (u16, u16)) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Element for String {
|
||||
fn render(&self, _: (u16, u16)) -> String {
|
||||
self.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
+41
-20
@@ -1,35 +1,56 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use osmium_engine::prelude::*;
|
||||
use osmium_engine::{prelude::*, utils::clear};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut console = init(true)?;
|
||||
|
||||
let ui = app();
|
||||
let mut ui = app();
|
||||
|
||||
console.render(&mut ui)?;
|
||||
|
||||
loop {
|
||||
console.render(ui.clone())?;
|
||||
if let crossterm::event::Event::Mouse(crossterm::event::MouseEvent {
|
||||
kind: crossterm::event::MouseEventKind::Moved,
|
||||
row,
|
||||
column,
|
||||
..
|
||||
}) = crossterm::event::read()?
|
||||
{
|
||||
console.mouse_position = Some((column, row));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
console.event(&mut ui, read()?)?;
|
||||
}
|
||||
|
||||
console.close()
|
||||
}
|
||||
|
||||
pub fn app() -> Ui {
|
||||
Arc::new(|frame| {
|
||||
pub fn app() -> Component {
|
||||
let mut count = use_state(0);
|
||||
|
||||
Box::new(move |frame| {
|
||||
frame.draw(
|
||||
&"Hello, World",
|
||||
&Props::center().state("hover", Style { color: Color::Blue }),
|
||||
Button {
|
||||
text: format!("The current count: {count}"),
|
||||
on_click: || count += 1,
|
||||
},
|
||||
Props::center(),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Button<F: FnMut()> {
|
||||
text: String,
|
||||
on_click: F,
|
||||
}
|
||||
|
||||
impl<F: FnMut()> Element for Button<F> {
|
||||
fn render(&self, _size: (u16, u16)) -> String {
|
||||
self.text.to_owned()
|
||||
}
|
||||
|
||||
fn event(&mut self, event: crossterm::event::Event) {
|
||||
match event {
|
||||
Event::Key(KeyEvent {
|
||||
code: KeyCode::Enter,
|
||||
..
|
||||
}) => self.action(0),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn action(&mut self, action: u16) {
|
||||
if action == 0 {
|
||||
(self.on_click)()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Color {
|
||||
NoColor,
|
||||
Black,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
BrightBlack,
|
||||
BrightRed,
|
||||
BrightGreen,
|
||||
BrightYellow,
|
||||
BrightBlue,
|
||||
BrightMagenta,
|
||||
BrightCyan,
|
||||
BrightWhite,
|
||||
Rgb(u8, u8, u8), // Custom RGB color
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Converts the enum to its ANSI escape code
|
||||
pub fn ansi_number(&self, fg: bool) -> String {
|
||||
let f = if fg { 30 } else { 40 };
|
||||
|
||||
if *self == Self::NoColor {
|
||||
return String::new();
|
||||
} else if let Self::Rgb(r, g, b) = self {
|
||||
return format!("{};2;{r};{g};{b}", f + 8);
|
||||
}
|
||||
|
||||
(f + match self {
|
||||
Self::Rgb(_, _, _) => 0,
|
||||
Self::NoColor => 0,
|
||||
|
||||
Self::Black => 0,
|
||||
Self::Red => 1,
|
||||
Self::Green => 2,
|
||||
Self::Yellow => 3,
|
||||
Self::Blue => 4,
|
||||
Self::Magenta => 5,
|
||||
Self::Cyan => 6,
|
||||
Self::White => 7,
|
||||
Self::BrightBlack => 60,
|
||||
Self::BrightRed => 61,
|
||||
Self::BrightGreen => 62,
|
||||
Self::BrightYellow => 63,
|
||||
Self::BrightBlue => 64,
|
||||
Self::BrightMagenta => 65,
|
||||
Self::BrightCyan => 66,
|
||||
Self::BrightWhite => 67,
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn to_ansi(&self, foreground: bool) -> String {
|
||||
let f = if foreground { "3" } else { "4" };
|
||||
match self {
|
||||
Self::NoColor => String::new(),
|
||||
Self::Black => format!("\x1b[{f}0m"),
|
||||
Self::Red => format!("\x1b[{f}1m"),
|
||||
Self::Green => format!("\x1b[{f}2m"),
|
||||
Self::Yellow => format!("\x1b[{f}3m"),
|
||||
Self::Blue => format!("\x1b[{f}4m"),
|
||||
Self::Magenta => format!("\x1b[{f}5m"),
|
||||
Self::Cyan => format!("\x1b[{f}6m"),
|
||||
Self::White => format!("\x1b[{f}7m"),
|
||||
Self::BrightBlack => format!("\x1b[{f}90m"),
|
||||
Self::BrightRed => format!("\x1b[{f}91m"),
|
||||
Self::BrightGreen => format!("\x1b[{f}92m"),
|
||||
Self::BrightYellow => format!("\x1b[{f}93m"),
|
||||
Self::BrightBlue => format!("\x1b[{f}94m"),
|
||||
Self::BrightMagenta => format!("\x1b[{f}95m"),
|
||||
Self::BrightCyan => format!("\x1b[{f}96m"),
|
||||
Self::BrightWhite => format!("\x1b[{f}97m"),
|
||||
Self::Rgb(r, g, b) => {
|
||||
let code = if foreground { "38" } else { "48" };
|
||||
format!("\x1b[{code};2;{r};{g};{b}m")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Color {
|
||||
fn default() -> Self {
|
||||
Self::NoColor
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||
pub struct Style {
|
||||
pub color: Color,
|
||||
pub background: Color,
|
||||
pub px: u16,
|
||||
pub py: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Position {
|
||||
Num(u16),
|
||||
Percent(u16),
|
||||
Center,
|
||||
}
|
||||
impl Position {
|
||||
pub fn get(&self, written: u16, frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Center => (frame - written) / 2,
|
||||
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Size {
|
||||
Num(u16),
|
||||
Percent(u16),
|
||||
Auto,
|
||||
}
|
||||
impl Size {
|
||||
pub fn get(&self, frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Auto => frame,
|
||||
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
pub fn get_after(&self, written: u16, frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Auto => written,
|
||||
Self::Percent(percent) => (((*percent as f32) / 100.0) * (frame as f32)) as u16,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Props {
|
||||
pub x: Position,
|
||||
pub y: Position,
|
||||
pub width: Size,
|
||||
pub height: Size,
|
||||
}
|
||||
|
||||
impl Props {
|
||||
pub fn new(x: u16, y: u16) -> Self {
|
||||
Self {
|
||||
x: Position::Num(x),
|
||||
y: Position::Num(y),
|
||||
width: Size::Auto,
|
||||
height: Size::Auto,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn auto() -> Self {
|
||||
Self::new(0, 0)
|
||||
}
|
||||
|
||||
pub fn center() -> Self {
|
||||
Self {
|
||||
x: Position::Center,
|
||||
y: Position::Center,
|
||||
width: Size::Auto,
|
||||
height: Size::Auto,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center_horizontal() -> Self {
|
||||
Self {
|
||||
x: Position::Center,
|
||||
y: Position::Num(0),
|
||||
width: Size::Auto,
|
||||
height: Size::Auto,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn center_vertical() -> Self {
|
||||
Self {
|
||||
x: Position::Num(0),
|
||||
y: Position::Center,
|
||||
width: Size::Auto,
|
||||
height: Size::Auto,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn x_percent(&mut self, p: u16) -> Self {
|
||||
self.x = Position::Percent(p);
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn y_percent(&mut self, p: u16) -> Self {
|
||||
self.y = Position::Percent(p);
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn width(&mut self, w: u16) -> Self {
|
||||
self.width = Size::Num(w);
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn height(&mut self, h: u16) -> Self {
|
||||
self.height = Size::Num(h);
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn width_percent(&mut self, w: u16) -> Self {
|
||||
self.width = Size::Percent(w);
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub fn height_percent(&mut self, h: u16) -> Self {
|
||||
self.height = Size::Percent(h);
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
//! The `state` module provides the `State` type for managing mutable state across UI components
|
||||
//! and operations. It wraps a raw pointer to enable shared mutability and integrates with common
|
||||
//! traits for ease of use.
|
||||
|
||||
/// A wrapper around a raw pointer to a mutable value, allowing shared state management.
|
||||
///
|
||||
/// # Safety
|
||||
/// This type uses unsafe code to dereference raw pointers. Proper care must be taken
|
||||
/// to ensure the validity and lifetime of the referenced value.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// let value = 42;
|
||||
/// let state = State(Box::into_raw(Box::new(value)));
|
||||
/// assert_eq!(*state, 42);
|
||||
/// ```
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct State<T>(pub(crate) *mut T);
|
||||
|
||||
pub fn use_state<T>(v: T) -> State<T> {
|
||||
State(Box::into_raw(Box::new(v)))
|
||||
}
|
||||
|
||||
pub fn default_state<T: Default>() -> State<T> {
|
||||
State(Box::into_raw(Box::new(T::default())))
|
||||
}
|
||||
|
||||
impl<T: std::fmt::Display> std::fmt::Display for State<T> {
|
||||
/// Formats the value of the state for display.
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", unsafe { &*self.0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::fmt::Debug> std::fmt::Debug for State<T> {
|
||||
/// Formats the state for debugging purposes.
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "State({:?})", unsafe { &*self.0 })
|
||||
}
|
||||
}
|
||||
|
||||
//// State Operations ////
|
||||
|
||||
impl<T: std::ops::Add<Output = T> + Clone> std::ops::Add<T> for State<T> {
|
||||
type Output = T;
|
||||
|
||||
/// Adds a value to the state, returning the result.
|
||||
fn add(self, rhs: T) -> Self::Output {
|
||||
unsafe {
|
||||
let lhs = &mut *self.0;
|
||||
lhs.clone() + rhs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::ops::Sub<Output = T> + Clone> std::ops::Sub<T> for State<T> {
|
||||
type Output = T;
|
||||
|
||||
/// Subtracts a value from the state, returning the result.
|
||||
fn sub(self, rhs: T) -> Self::Output {
|
||||
unsafe {
|
||||
let lhs = &mut *self.0;
|
||||
lhs.clone() - rhs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::ops::Div<Output = T> + Clone> std::ops::Div<T> for State<T> {
|
||||
type Output = T;
|
||||
|
||||
/// Divides the state by a value, returning the result.
|
||||
fn div(self, rhs: T) -> Self::Output {
|
||||
unsafe {
|
||||
let lhs = &mut *self.0;
|
||||
lhs.clone() / rhs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::ops::AddAssign + Clone> std::ops::AddAssign<T> for State<T> {
|
||||
/// Adds a value to the state in place.
|
||||
fn add_assign(&mut self, rhs: T) {
|
||||
unsafe { *self.0 += rhs }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::ops::SubAssign + Clone> std::ops::SubAssign<T> for State<T> {
|
||||
/// Subtracts a value from the state in place.
|
||||
fn sub_assign(&mut self, rhs: T) {
|
||||
unsafe { *self.0 -= rhs }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::ops::Deref for State<T> {
|
||||
type Target = T;
|
||||
|
||||
/// Dereferences the state to access the underlying value.
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { &*self.0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::ops::DerefMut for State<T> {
|
||||
/// Dereferences the state to access the underlying mutable value.
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
unsafe { &mut *self.0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> State<T> {
|
||||
/// Returns a copy of the current state.
|
||||
pub fn copy_state(&self) -> Self {
|
||||
Self(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq<T>> PartialEq<T> for State<T> {
|
||||
/// Compares the state with another value for equality.
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
unsafe { &*self.0 == other }
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Color {
|
||||
NoColor,
|
||||
Black,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
BrightBlack,
|
||||
BrightRed,
|
||||
BrightGreen,
|
||||
BrightYellow,
|
||||
BrightBlue,
|
||||
BrightMagenta,
|
||||
BrightCyan,
|
||||
BrightWhite,
|
||||
Rgb(u8, u8, u8), // Custom RGB color
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Converts the enum to its ANSI escape code
|
||||
pub fn to_ansi(&self, foreground: bool) -> String {
|
||||
let f = if foreground { "3" } else { "4" };
|
||||
match self {
|
||||
Self::NoColor => format!("\x1b[0m"),
|
||||
Self::Black => format!("\x1b[{f}0m"),
|
||||
Self::Red => format!("\x1b[{f}1m"),
|
||||
Self::Green => format!("\x1b[{f}2m"),
|
||||
Self::Yellow => format!("\x1b[{f}3m"),
|
||||
Self::Blue => format!("\x1b[{f}4m"),
|
||||
Self::Magenta => format!("\x1b[{f}5m"),
|
||||
Self::Cyan => format!("\x1b[{f}6m"),
|
||||
Self::White => format!("\x1b[{f}7m"),
|
||||
Self::BrightBlack => format!("\x1b[{f}90m"),
|
||||
Self::BrightRed => format!("\x1b[{f}91m"),
|
||||
Self::BrightGreen => format!("\x1b[{f}92m"),
|
||||
Self::BrightYellow => format!("\x1b[{f}93m"),
|
||||
Self::BrightBlue => format!("\x1b[{f}94m"),
|
||||
Self::BrightMagenta => format!("\x1b[{f}95m"),
|
||||
Self::BrightCyan => format!("\x1b[{f}96m"),
|
||||
Self::BrightWhite => format!("\x1b[{f}97m"),
|
||||
Self::Rgb(r, g, b) => {
|
||||
let code = if foreground { "38" } else { "48" };
|
||||
format!("\x1b[{};2;{};{};{}m", code, r, g, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Color {
|
||||
fn default() -> Self {
|
||||
Self::NoColor
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct Style {
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Position {
|
||||
Num(u16),
|
||||
Center,
|
||||
}
|
||||
impl Position {
|
||||
pub fn get(&self, written: u16, frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Center => (frame - written) / 2,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Dimension {
|
||||
Num(u16),
|
||||
Auto,
|
||||
}
|
||||
impl Dimension {
|
||||
pub fn get(&self, _written: u16, frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Auto => frame,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_root(&self, written: u16, _frame: u16) -> u16 {
|
||||
match self {
|
||||
Self::Auto => written,
|
||||
Self::Num(n) => *n,
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-8
@@ -100,18 +100,12 @@ pub fn str_size(s: &str) -> (u16, u16) {
|
||||
///
|
||||
/// # Returns
|
||||
/// A `Console` instance wrapped in a `Result`.
|
||||
pub fn init(mouse: bool) -> crate::Result<crate::Console> {
|
||||
pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console> {
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
clear()?;
|
||||
hide_cursor()?;
|
||||
if mouse {
|
||||
crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture)?;
|
||||
}
|
||||
let (width, height) = crossterm::terminal::size()?;
|
||||
Ok(crate::Console {
|
||||
width,
|
||||
height,
|
||||
mouse,
|
||||
mouse_position: None,
|
||||
})
|
||||
Ok(crate::Console { mouse })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user