feat: minimal clock

This commit is contained in:
Christoph J. Scherr 2024-07-09 11:58:12 +02:00 committed by PlexSheep
parent 8fbb3ca7e0
commit da29f6796c
3 changed files with 74 additions and 3 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

View File

@ -13,4 +13,5 @@ keywords = ["time", "clock", "tui"]
[dependencies]
chrono = "0.4.38"
ratatui = "0.27.0"

View File

@ -1,3 +1,52 @@
fn main() {
println!("Hello, world!");
use ratatui::crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
backend::CrosstermBackend,
widgets::{Block, Borders},
Terminal,
};
use std::io;
const TITLE: &str = "Crock";
fn main() -> Result<(), io::Error> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
loop {
terminal.draw(|f| {
let size = f.size();
let block = Block::default().title(TITLE).borders(Borders::ALL);
let inner = block.inner(size);
f.render_widget(block, size);
let w = ratatui::widgets::Paragraph::new(format!("{}", chrono::Utc::now()));
f.render_widget(w, inner);
})?;
if let Event::Key(key) = event::read()? {
if key.code == KeyCode::Char('q')
|| key.code == KeyCode::Esc
|| (key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c'))
{
break;
}
}
}
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
Ok(())
}