This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
crock/src/main.rs

108 lines
3.4 KiB
Rust
Raw Normal View History

2024-07-09 13:41:25 +02:00
#![warn(clippy::pedantic, clippy::style, clippy::nursery)]
#![allow(clippy::question_mark_used)]
2024-07-09 12:12:03 +02:00
use chrono::SubsecRound;
2024-07-09 13:41:25 +02:00
use ratatui::crossterm::event::{
self, poll, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers,
2024-07-09 11:58:12 +02:00
};
use ratatui::layout::Alignment;
use ratatui::widgets::{Block, Padding};
2024-07-09 12:12:03 +02:00
use ratatui::{
2024-07-09 13:41:25 +02:00
backend::CrosstermBackend,
2024-07-09 12:12:03 +02:00
crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
2024-07-09 13:41:25 +02:00
layout::{Constraint, Direction, Layout, Rect},
style::{Style, Stylize},
widgets::Paragraph,
Terminal,
2024-07-09 12:12:03 +02:00
};
use std::{io, time::Duration};
2024-07-09 11:58:12 +02:00
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 {
2024-07-09 13:46:46 +02:00
let raw_time = chrono::Local::now().round_subsecs(0);
2024-07-09 12:34:15 +02:00
let splits: Vec<String> = raw_time
.naive_local()
.to_string()
.split_whitespace()
.map(str::to_string)
.collect();
let fdate: String = splits[0].clone();
let ftime: String = splits[1].clone();
terminal.draw(|frame| {
let root = frame.size();
let space = Block::bordered()
.padding(Padding::new(
root.width / 8,
root.width / 8,
root.height / 8,
root.height / 8,
))
.title(env!("CARGO_PKG_NAME"))
.title_bottom(env!("CARGO_PKG_VERSION"))
.title_alignment(Alignment::Center)
.title_style(Style::new().bold());
let a = space.inner(root);
let parts = partition(a);
2024-07-09 12:34:15 +02:00
let timew = tui_big_text::BigText::builder()
.style(Style::new().red())
.lines(vec![ftime.into()])
.alignment(Alignment::Center)
2024-07-09 12:34:15 +02:00
.build()
.expect("could not render time widget");
let datew = Paragraph::new(fdate)
.blue()
.alignment(Alignment::Left)
.block(Block::new().padding(Padding::new(
parts.0.left(),
parts.0.right() / 3,
0,
0,
)));
frame.render_widget(space, root);
frame.render_widget(timew, parts.1);
frame.render_widget(datew, parts.0);
2024-07-09 11:58:12 +02:00
})?;
2024-07-09 12:12:03 +02:00
if poll(Duration::from_millis(100))? {
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;
}
2024-07-09 11:58:12 +02:00
}
}
}
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
Ok(())
2024-03-04 15:14:28 +01:00
}
2024-07-09 12:12:03 +02:00
fn partition(r: Rect) -> (Rect, Rect) {
let part = Layout::default()
2024-07-09 12:12:03 +02:00
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(13), Constraint::Min(0)])
2024-07-09 12:12:03 +02:00
.split(r);
(part[0], part[1])
2024-07-09 12:12:03 +02:00
}