generated from PlexSheep/rs-base
feat: center and split
This commit is contained in:
parent
da29f6796c
commit
7f81f884e4
73
src/main.rs
73
src/main.rs
|
@ -1,14 +1,20 @@
|
||||||
use ratatui::crossterm::{
|
use chrono::SubsecRound;
|
||||||
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
|
|
||||||
execute,
|
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
||||||
};
|
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
backend::CrosstermBackend,
|
backend::CrosstermBackend,
|
||||||
|
crossterm::event::poll,
|
||||||
|
layout::{Constraint, Direction, Layout, Rect},
|
||||||
widgets::{Block, Borders},
|
widgets::{Block, Borders},
|
||||||
Terminal,
|
Terminal,
|
||||||
};
|
};
|
||||||
use std::io;
|
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};
|
||||||
|
|
||||||
const TITLE: &str = "Crock";
|
const TITLE: &str = "Crock";
|
||||||
|
|
||||||
|
@ -21,20 +27,26 @@ fn main() -> Result<(), io::Error> {
|
||||||
let mut terminal = Terminal::new(backend)?;
|
let mut terminal = Terminal::new(backend)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
let time = chrono::Utc::now().round_subsecs(0);
|
||||||
terminal.draw(|f| {
|
terminal.draw(|f| {
|
||||||
let size = f.size();
|
let w = ratatui::widgets::Paragraph::new(format!(
|
||||||
let block = Block::default().title(TITLE).borders(Borders::ALL);
|
"{}\n\n{}",
|
||||||
let inner = block.inner(size);
|
time.time(),
|
||||||
f.render_widget(block, size);
|
time.date_naive()
|
||||||
let w = ratatui::widgets::Paragraph::new(format!("{}", chrono::Utc::now()));
|
))
|
||||||
f.render_widget(w, inner);
|
.centered()
|
||||||
|
.red();
|
||||||
|
f.render_widget(w, centered_rect(f.size(), 40, 20));
|
||||||
})?;
|
})?;
|
||||||
if let Event::Key(key) = event::read()? {
|
if poll(Duration::from_millis(100))? {
|
||||||
if key.code == KeyCode::Char('q')
|
if let Event::Key(key) = event::read()? {
|
||||||
|| key.code == KeyCode::Esc
|
if key.code == KeyCode::Char('q')
|
||||||
|| (key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c'))
|
|| key.code == KeyCode::Esc
|
||||||
{
|
|| (key.modifiers.contains(KeyModifiers::CONTROL)
|
||||||
break;
|
&& key.code == KeyCode::Char('c'))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,3 +62,28 @@ fn main() -> Result<(), io::Error> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # 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]
|
||||||
|
}
|
||||||
|
|
Reference in New Issue