Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bdac6ff65e | |||
| 060faf594d | |||
| 511046678d | |||
| 7e896d04bd |
@ -1,5 +1,5 @@
|
||||
[registries.lugit]
|
||||
index = "sparse+http://lugit.local/api/packages/lucodear/cargo/"
|
||||
index = "sparse+https://git.lucode.dev/api/packages/lucas/cargo/"
|
||||
|
||||
[registry]
|
||||
global-credential-providers = ["cargo:token"]
|
||||
|
||||
122
Cargo.lock
generated
122
Cargo.lock
generated
@ -1,6 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
@ -82,9 +82,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.6.0"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
@ -133,16 +133,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.38"
|
||||
version = "0.4.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
||||
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -167,9 +167,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
|
||||
|
||||
[[package]]
|
||||
name = "croner"
|
||||
version = "2.0.5"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eba3aaaafb3c313b352ff02626adb2dfaf9663159d339a878f6e5b2f6259a97c"
|
||||
checksum = "38fd53511eaf0b00a185613875fee58b208dfce016577d0ad4bb548e1c4fb3ee"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
]
|
||||
@ -202,9 +202,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "downcast-rs"
|
||||
version = "1.2.1"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
|
||||
checksum = "ea8a8b81cacc08888170eef4d13b775126db426d0b348bee9d18c2c1eaf123cf"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
@ -240,9 +240,9 @@ checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1"
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
||||
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
@ -255,9 +255,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
||||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
@ -265,15 +265,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
||||
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
@ -282,15 +282,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
|
||||
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
||||
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -299,21 +299,21 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
|
||||
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
|
||||
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.30"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
@ -390,6 +390,12 @@ version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "2.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
|
||||
|
||||
[[package]]
|
||||
name = "instability"
|
||||
version = "0.3.2"
|
||||
@ -448,13 +454,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
version = "0.4.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||
|
||||
[[package]]
|
||||
name = "lool"
|
||||
version = "0.8.2"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"chrono",
|
||||
@ -468,7 +474,7 @@ dependencies = [
|
||||
"num-traits",
|
||||
"palette",
|
||||
"ratatui",
|
||||
"strum",
|
||||
"strum 0.27.1",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"unicode-width 0.2.0",
|
||||
@ -677,23 +683,23 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
|
||||
[[package]]
|
||||
name = "ratatui"
|
||||
version = "0.28.1"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fdef7f9be5c0122f890d58bdf4d964349ba6a6161f705907526d891efabba57d"
|
||||
checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cassowary",
|
||||
"compact_str",
|
||||
"crossterm",
|
||||
"indoc",
|
||||
"instability",
|
||||
"itertools",
|
||||
"lru",
|
||||
"paste",
|
||||
"strum",
|
||||
"strum_macros",
|
||||
"strum 0.26.3",
|
||||
"unicode-segmentation",
|
||||
"unicode-truncate",
|
||||
"unicode-width 0.1.13",
|
||||
"unicode-width 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -805,7 +811,16 @@ version = "0.26.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
||||
dependencies = [
|
||||
"strum_macros",
|
||||
"strum_macros 0.26.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.27.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
|
||||
dependencies = [
|
||||
"strum_macros 0.27.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -821,6 +836,19 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.27.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.58"
|
||||
@ -834,9 +862,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.40.0"
|
||||
version = "1.44.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
|
||||
checksum = "f382da615b842244d4b8738c82ed1275e6c5dd90c459a30941cd07080b06c91a"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"pin-project-lite",
|
||||
@ -845,9 +873,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.4.0"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
||||
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -856,9 +884,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.12"
|
||||
version = "0.7.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a"
|
||||
checksum = "6b9590b93e6fcc1739458317cccd391ad3955e2bde8913edf6f95f9e65a8f034"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
@ -999,6 +1027,12 @@ dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
|
||||
27
Cargo.toml
27
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "lool"
|
||||
version = "0.8.2"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
description = "🧳 lool » lucode.ar rust common utilities"
|
||||
authors = ["Lucas Colombo <lucas@lucode.ar>"]
|
||||
@ -75,20 +75,20 @@ path = "lib/lib.rs"
|
||||
eyre = { version = "0.6.12", default-features = false }
|
||||
|
||||
# optional
|
||||
bitflags = { version = "2.6.0", optional = true }
|
||||
chrono = { version = "0.4.37", optional = true }
|
||||
log = { version = "0.4.22", optional = true }
|
||||
tokio = { version = "1.40.0", optional = true }
|
||||
croner = { version = "2.0.5", optional = true }
|
||||
bitflags = { version = "2.9.0", optional = true }
|
||||
chrono = { version = "0.4.40", optional = true }
|
||||
log = { version = "0.4.27", optional = true }
|
||||
tokio = { version = "1.44.1", optional = true }
|
||||
croner = { version = "2.1.0", optional = true }
|
||||
num-traits = { version = "0.2.19", optional = true }
|
||||
glob-match = { version = "0.2.1", optional = true }
|
||||
tokio-util = { version = "0.7.12", optional = true }
|
||||
ratatui = { version = "0.28.1", optional = true }
|
||||
tokio-util = { version = "0.7.14", optional = true }
|
||||
ratatui = { version = "0.29.0", optional = true }
|
||||
palette = { version = "0.7.6", optional = true }
|
||||
crossterm = { version="0.28.1", optional = true}
|
||||
strum = { version="0.26.1", optional = true }
|
||||
downcast-rs = { version="1.2.1", optional = true}
|
||||
futures = { version = "0.3.30", optional = true }
|
||||
strum = { version="0.27.1", optional = true }
|
||||
downcast-rs = { version="2.0.1", optional = true}
|
||||
futures = { version = "0.3.31", optional = true }
|
||||
unicode-width = { version = "0.2.0", optional = true }
|
||||
|
||||
[[example]]
|
||||
@ -110,3 +110,8 @@ required-features = ["cli.tui.widgets", "tokio.rt"]
|
||||
name = "widget_grid_selector"
|
||||
path = "examples/widget_grid_selector.rs"
|
||||
required-features = ["cli.tui.widgets", "tokio.rt"]
|
||||
|
||||
[[example]]
|
||||
name = "widget_switch"
|
||||
path = "examples/widget_switch.rs"
|
||||
required-features = ["cli.tui.widgets", "tokio.rt"]
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<p align="center"><b>𝐥𝐨𝐨𝐥</b> is a tool-box library with common utilities for <b>𝚕𝚞𝚌𝚘𝚍𝚎.𝚊𝚛</b> projects.
|
||||
<p align="center"><b>𝐥𝐨𝐨𝐥</b> is a tool-box library with common utilities for <b>𝚕𝚞𝚌𝚘𝚍𝚎.dev</b> projects.
|
||||
</p>
|
||||
|
||||
<br>
|
||||
|
||||
@ -34,6 +34,11 @@ tasks:
|
||||
cmds:
|
||||
- cargo watch --features=cli.tui.widgets,tokio.rt -c -x "run --example widget_grid_selector"
|
||||
|
||||
example:switch:
|
||||
desc: 🚀 run lool «example widget_switch»
|
||||
cmds:
|
||||
- cargo watch --features=cli.tui.widgets,tokio.rt -c -x "run --example widget_switch"
|
||||
|
||||
fmt:
|
||||
desc: 🎨 format lool
|
||||
cmds:
|
||||
|
||||
80
examples/widget_switch.rs
Normal file
80
examples/widget_switch.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use {
|
||||
lool::tui::{
|
||||
ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
crossterm::{
|
||||
event::{self},
|
||||
execute,
|
||||
terminal::{
|
||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||
},
|
||||
},
|
||||
layout::{Constraint, Layout},
|
||||
Terminal,
|
||||
},
|
||||
widgets::{
|
||||
switch::Switch,
|
||||
textarea::{Input, Key},
|
||||
},
|
||||
},
|
||||
ratatui::layout::Flex,
|
||||
std::io,
|
||||
};
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
||||
enable_raw_mode()?;
|
||||
execute!(stdout, EnterAlternateScreen)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut term = Terminal::new(backend)?;
|
||||
let mut switch_state = false;
|
||||
|
||||
loop {
|
||||
term.draw(|f| {
|
||||
// Render the textarea
|
||||
let switch =
|
||||
Switch::with_status(switch_state).with_color_on(ratatui::style::Color::Blue);
|
||||
|
||||
let [horiz] = Layout::horizontal([Constraint::Percentage(100)])
|
||||
.flex(Flex::Center)
|
||||
.areas(f.area());
|
||||
|
||||
let [verti] = Layout::vertical([Constraint::Length(2)]).flex(Flex::Center).areas(horiz);
|
||||
|
||||
let [centered] =
|
||||
Layout::horizontal([Constraint::Length(14)]).flex(Flex::Center).areas(verti);
|
||||
|
||||
f.render_widget(switch, centered);
|
||||
})?;
|
||||
match event::read()?.into() {
|
||||
Input { key: Key::Esc, .. }
|
||||
| Input {
|
||||
key: Key::Char('c'),
|
||||
shift: false,
|
||||
ctrl: true,
|
||||
alt: false,
|
||||
} => break,
|
||||
Input {
|
||||
key: Key::Enter,
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
}
|
||||
| Input {
|
||||
key: Key::Char(' '),
|
||||
..
|
||||
} => {
|
||||
switch_state = !switch_state;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
disable_raw_mode()?;
|
||||
execute!(term.backend_mut(), LeaveAlternateScreen)?;
|
||||
term.show_cursor()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -244,3 +244,12 @@ As a part of this library, the `Into<GridItem>` trait is implemented for `String
|
||||
|
||||
The example at [`widget_grid_selector.rs`](/examples/widget_grid_selector.rs) demonstrates how to
|
||||
implement the `Into<GridItem>` trait for a custom type.
|
||||
|
||||
## `Switch` Widget
|
||||
|
||||
A simple stateless switch widget that can be used to show visual feedback of a boolean state.
|
||||
|
||||
### Example
|
||||
|
||||
See the [`widget_switch.rs`](/examples/widget_switch.rs) example for a full demonstration of how to
|
||||
use the `Switch` widget.
|
||||
@ -40,6 +40,11 @@ pub mod widgets {
|
||||
}
|
||||
|
||||
pub mod textarea;
|
||||
|
||||
pub mod switch {
|
||||
mod widget;
|
||||
pub use widget::*;
|
||||
}
|
||||
}
|
||||
|
||||
// ratatui prelude
|
||||
|
||||
@ -29,9 +29,9 @@ impl AsRef<str> for GridItem {
|
||||
}
|
||||
|
||||
// convert Label into String
|
||||
impl Into<String> for GridItem {
|
||||
fn into(self) -> String {
|
||||
self.0.clone()
|
||||
impl From<GridItem> for String {
|
||||
fn from(val: GridItem) -> Self {
|
||||
val.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
88
lib/cli/tui/widgets/switch/widget.rs
Normal file
88
lib/cli/tui/widgets/switch/widget.rs
Normal file
@ -0,0 +1,88 @@
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
style::{Color, Style},
|
||||
widgets::{Block, Widget},
|
||||
};
|
||||
|
||||
/// A switch widget
|
||||
///
|
||||
/// This widget is used to show visual confirmation of a boolean state
|
||||
pub struct Switch {
|
||||
/// The state of the switch
|
||||
state: bool,
|
||||
/// The color of the "on" state (`Green` by default)
|
||||
color_on: Color,
|
||||
/// The color of the "off" state (`DarkGray` by default)
|
||||
color_off: Color,
|
||||
/// The color of the switch itself (`White` by default)
|
||||
color_switch: Color,
|
||||
}
|
||||
|
||||
impl Switch {
|
||||
pub fn with_status(state: bool) -> Self {
|
||||
Switch {
|
||||
state,
|
||||
color_on: Color::Green,
|
||||
color_off: Color::DarkGray,
|
||||
color_switch: Color::White,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color_on(mut self, color: Color) -> Self {
|
||||
self.color_on = color;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_color_off(mut self, color: Color) -> Self {
|
||||
self.color_off = color;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_color_switch(mut self, color: Color) -> Self {
|
||||
self.color_switch = color;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn get_layout(&self, area: Rect) -> (Rect, Rect) {
|
||||
let main = Layout::default()
|
||||
.direction(ratatui::layout::Direction::Vertical)
|
||||
.constraints([Constraint::Length(2)])
|
||||
.split(area);
|
||||
|
||||
let layout = Layout::default()
|
||||
.direction(ratatui::layout::Direction::Horizontal)
|
||||
.constraints([Constraint::Length(7), Constraint::Length(7)])
|
||||
.split(main[0]);
|
||||
|
||||
(layout[0], layout[1])
|
||||
}
|
||||
|
||||
fn get_left_color(&self) -> Color {
|
||||
if self.state {
|
||||
self.color_on
|
||||
} else {
|
||||
self.color_switch
|
||||
}
|
||||
}
|
||||
|
||||
fn get_right_color(&self) -> Color {
|
||||
if self.state {
|
||||
self.color_switch
|
||||
} else {
|
||||
self.color_off
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Switch {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
let (left, right) = self.get_layout(area);
|
||||
|
||||
let left_block = Block::default().style(Style::default().bg(self.get_left_color()));
|
||||
let right_block = Block::default().style(Style::default().bg(self.get_right_color()));
|
||||
|
||||
left_block.render(left, buf);
|
||||
right_block.render(right, buf);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user