-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
243 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use chrono::Local; | ||
use iocraft::prelude::*; | ||
use std::time::Duration; | ||
|
||
#[component] | ||
fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> { | ||
let (width, height) = hooks.use_terminal_size(); | ||
let mut system = hooks.use_context_mut::<SystemContext>(); | ||
let time = hooks.use_state(|| Local::now()); | ||
let should_exit = hooks.use_state(|| false); | ||
|
||
hooks.use_future(async move { | ||
loop { | ||
smol::Timer::after(Duration::from_secs(1)).await; | ||
time.set(Local::now()); | ||
} | ||
}); | ||
|
||
hooks.use_terminal_events({ | ||
move |event| match event { | ||
TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => { | ||
match code { | ||
KeyCode::Char('q') => should_exit.set(true), | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
}); | ||
|
||
if should_exit.get() { | ||
system.exit(); | ||
} | ||
|
||
element! { | ||
Box( | ||
// subtract one in case there's a scrollbar | ||
width: width - 1, | ||
height, | ||
background_color: Color::DarkGrey, | ||
border_style: BorderStyle::Double, | ||
border_color: Color::Blue, | ||
flex_direction: FlexDirection::Column, | ||
align_items: AlignItems::Center, | ||
justify_content: JustifyContent::Center, | ||
) { | ||
Box( | ||
border_style: BorderStyle::Round, | ||
border_color: Color::Blue, | ||
margin_bottom: 2, | ||
padding_top: 2, | ||
padding_bottom: 2, | ||
padding_left: 8, | ||
padding_right: 8, | ||
) { | ||
Text(content: format!("Current Time: {}", time.get().format("%r"))) | ||
} | ||
Text(content: "Press \"q\" to quit.") | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
smol::block_on(element!(Example).fullscreen()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::{ | ||
hooks::{UseState, UseTerminalEvents}, | ||
Hooks, TerminalEvent, | ||
}; | ||
use crossterm::terminal; | ||
|
||
/// `UseTerminalSize` is a hook that returns the current terminal size. | ||
pub trait UseTerminalSize { | ||
/// Returns the current terminal size as a tuple of `(width, height)`. | ||
fn use_terminal_size(&mut self) -> (u16, u16); | ||
} | ||
|
||
impl UseTerminalSize for Hooks<'_, '_> { | ||
fn use_terminal_size(&mut self) -> (u16, u16) { | ||
let size = self.use_state(|| terminal::size().unwrap_or((0, 0))); | ||
self.use_terminal_events(move |event| { | ||
if let TerminalEvent::Resize(width, height) = event { | ||
size.set((width, height)); | ||
} | ||
}); | ||
size.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.