diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64bf0eb --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/Cargo.toml b/Cargo.toml index bb7e3c6..194bca3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ keywords = ["time", "clock", "tui"] [dependencies] - +chrono = "0.4.38" +ratatui = "0.27.0" diff --git a/src/main.rs b/src/main.rs index e7a11a9..0193899 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }