generated from PlexSheep/rs-base
Compare commits
3 commits
4c4127f1dc
...
40d391a266
Author | SHA1 | Date | |
---|---|---|---|
40d391a266 | |||
7f81f884e4 | |||
da29f6796c |
3 changed files with 121 additions and 3 deletions
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal 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/
|
|
@ -13,4 +13,6 @@ keywords = ["time", "clock", "tui"]
|
|||
|
||||
|
||||
[dependencies]
|
||||
|
||||
chrono = "0.4.38"
|
||||
ratatui = "0.27.0"
|
||||
tui-big-text = "0.4.5"
|
||||
|
|
99
src/main.rs
99
src/main.rs
|
@ -1,3 +1,98 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use chrono::SubsecRound;
|
||||
use ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
crossterm::event::poll,
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::Style,
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
Terminal,
|
||||
};
|
||||
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";
|
||||
|
||||
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 {
|
||||
let raw_time = chrono::Utc::now().round_subsecs(0);
|
||||
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(|f| {
|
||||
let timew = tui_big_text::BigText::builder()
|
||||
.style(Style::new().red())
|
||||
.lines(vec![ftime.into()])
|
||||
.build()
|
||||
.expect("could not render time widget");
|
||||
let datew = Paragraph::new(fdate).blue();
|
||||
f.render_widget(datew, centered_rect(f.size(), 45, 80));
|
||||
f.render_widget(timew, centered_rect(f.size(), 45, 60));
|
||||
})?;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// restore terminal
|
||||
disable_raw_mode()?;
|
||||
execute!(
|
||||
terminal.backend_mut(),
|
||||
LeaveAlternateScreen,
|
||||
DisableMouseCapture
|
||||
)?;
|
||||
terminal.show_cursor()?;
|
||||
|
||||
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 a new issue