2024-07-09 12:12:03 +02:00
|
|
|
use chrono::SubsecRound;
|
2024-07-09 11:58:12 +02:00
|
|
|
use ratatui::{
|
|
|
|
backend::CrosstermBackend,
|
2024-07-09 12:12:03 +02:00
|
|
|
crossterm::event::poll,
|
|
|
|
layout::{Constraint, Direction, Layout, Rect},
|
2024-07-09 11:58:12 +02:00
|
|
|
widgets::{Block, Borders},
|
|
|
|
Terminal,
|
|
|
|
};
|
2024-07-09 12:12:03 +02:00
|
|
|
use ratatui::{
|
|
|
|
crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
},
|
|
|
|
style::Stylize,
|
|
|
|
};
|
|
|
|
use std::{io, time::Duration};
|
2024-07-09 11:58:12 +02:00
|
|
|
|
|
|
|
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 {
|
2024-07-09 12:12:03 +02:00
|
|
|
let time = chrono::Utc::now().round_subsecs(0);
|
2024-07-09 11:58:12 +02:00
|
|
|
terminal.draw(|f| {
|
2024-07-09 12:12:03 +02:00
|
|
|
let w = ratatui::widgets::Paragraph::new(format!(
|
|
|
|
"{}\n\n{}",
|
|
|
|
time.time(),
|
|
|
|
time.date_naive()
|
|
|
|
))
|
|
|
|
.centered()
|
|
|
|
.red();
|
|
|
|
f.render_widget(w, centered_rect(f.size(), 40, 20));
|
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
|
|
|
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let rect = centered_rect(f.size(), 50, 50);
|
|
|
|
/// ```
|
|
|
|
fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
|
|
|
|
let popup_layout = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([
|
|
|
|
Constraint::Percentage((100 - percent_y) / 2),
|
|
|
|
Constraint::Percentage(percent_y),
|
|
|
|
Constraint::Percentage((100 - percent_y) / 2),
|
|
|
|
])
|
|
|
|
.split(r);
|
|
|
|
|
|
|
|
Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([
|
|
|
|
Constraint::Percentage((100 - percent_x) / 2),
|
|
|
|
Constraint::Percentage(percent_x),
|
|
|
|
Constraint::Percentage((100 - percent_x) / 2),
|
|
|
|
])
|
|
|
|
.split(popup_layout[1])[1]
|
|
|
|
}
|