generated from PlexSheep/rs-base
Compare commits
No commits in common. "23a417a9f9bab400e1d724483af786ffaa453eb3" and "80b466efdf275688d04dbcd114046eafc8d0b366" have entirely different histories.
23a417a9f9
...
80b466efdf
5 changed files with 10 additions and 55 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "crock"
|
||||
version = "0.3.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
publish = true
|
||||
authors = ["Christoph J. Scherr <software@cscherr.de>"]
|
||||
|
|
14
src/clock.rs
14
src/clock.rs
|
@ -6,7 +6,7 @@
|
|||
#![allow(clippy::cast_possible_wrap)]
|
||||
#![allow(clippy::cast_sign_loss)] // it should only be positive anyway
|
||||
|
||||
use chrono::{Date, DateTime, Local, SubsecRound, Timelike};
|
||||
use chrono::{DateTime, Local, SubsecRound, Timelike};
|
||||
use clap::Parser;
|
||||
use libpt::cli::args::HELP_TEMPLATE;
|
||||
use libpt::cli::clap::ArgGroup;
|
||||
|
@ -30,7 +30,7 @@ use ui::Data;
|
|||
/// Make your terminal into a big clock
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[command(help_template = HELP_TEMPLATE, author, version)]
|
||||
#[clap(group( ArgGroup::new("timebarlen") .args(&["minute","day", "hour", "custom", "countdown", "timer"]),))]
|
||||
#[clap(group( ArgGroup::new("timebarlen") .args(&["minute","day", "hour", "custom", "countdown"]),))]
|
||||
#[allow(clippy::struct_excessive_bools)] // the struct is for cli parsing and we already use an
|
||||
// ArgGroup
|
||||
pub struct Clock {
|
||||
|
@ -70,8 +70,6 @@ pub struct Clock {
|
|||
pub(crate) last_reset: Option<DateTime<Local>>,
|
||||
#[clap(skip)]
|
||||
pub(crate) did_notify: bool,
|
||||
#[clap(skip)]
|
||||
pub(crate) started_at: DateTime<Local>,
|
||||
}
|
||||
|
||||
impl Clock {
|
||||
|
@ -84,8 +82,6 @@ impl Clock {
|
|||
Some(TimeBarLength::Day)
|
||||
} else if self.hour {
|
||||
Some(TimeBarLength::Hour)
|
||||
} else if self.timer {
|
||||
Some(TimeBarLength::Timer)
|
||||
} else if self.countdown.is_some() {
|
||||
Some(TimeBarLength::Countup(
|
||||
self.countdown.unwrap().as_secs() as i64
|
||||
|
@ -115,7 +111,7 @@ impl Clock {
|
|||
if let Some(len) = self.timebar_len() {
|
||||
let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap());
|
||||
match len {
|
||||
TimeBarLength::Countup(_) | TimeBarLength::Timer => {
|
||||
TimeBarLength::Countup(_) => {
|
||||
// the count up should not reset. If the time is over, just keep it at 100%
|
||||
}
|
||||
TimeBarLength::Custom(_) => {
|
||||
|
@ -151,7 +147,7 @@ impl Clock {
|
|||
if let Some(len) = self.timebar_len() {
|
||||
trace!("Local Time: {}", Local::now());
|
||||
match len {
|
||||
TimeBarLength::Custom(_) | TimeBarLength::Countup(_) | TimeBarLength::Timer => {
|
||||
TimeBarLength::Custom(_) | TimeBarLength::Countup(_) => {
|
||||
self.last_reset = Some(Local::now());
|
||||
}
|
||||
TimeBarLength::Minute => {
|
||||
|
@ -208,7 +204,7 @@ impl Clock {
|
|||
) -> anyhow::Result<()> {
|
||||
let tick_rate = std::time::Duration::from_millis(100);
|
||||
let mut last_tick = Instant::now();
|
||||
let mut uidata: Data = Data::new(self.timebar_len().unwrap());
|
||||
let mut uidata: Data = Data::default();
|
||||
self.setup()?;
|
||||
loop {
|
||||
let raw_time = chrono::Local::now().round_subsecs(0);
|
||||
|
|
|
@ -4,7 +4,6 @@ use chrono::Duration;
|
|||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TimeBarLength {
|
||||
Timer,
|
||||
Minute,
|
||||
Hour,
|
||||
Custom(i64),
|
||||
|
@ -20,7 +19,6 @@ impl TimeBarLength {
|
|||
Self::Minute => 60,
|
||||
Self::Day => 24 * 60 * 60,
|
||||
Self::Hour => 60 * 60,
|
||||
Self::Timer => 1,
|
||||
Self::Custom(secs) | Self::Countup(secs) => secs,
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +38,6 @@ impl Default for TimeBarLength {
|
|||
|
||||
impl Display for TimeBarLength {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if *self == Self::Timer {
|
||||
return write!(f, "");
|
||||
}
|
||||
let buf = match self {
|
||||
Self::Minute => humantime::Duration::from(
|
||||
Duration::minutes(1)
|
||||
|
@ -64,7 +59,6 @@ impl Display for TimeBarLength {
|
|||
.to_std()
|
||||
.expect("could not convert chrono time to std time"),
|
||||
),
|
||||
Self::Timer => unreachable!(),
|
||||
};
|
||||
write!(f, "{buf}")
|
||||
}
|
||||
|
|
|
@ -8,37 +8,18 @@ use crate::clock::timebar::TimeBarLength;
|
|||
|
||||
use super::Clock;
|
||||
|
||||
pub const TIME_FORMAT: &str = "%H:%M:%S";
|
||||
|
||||
// TODO: make this a ringbuffer with a custom struct inside?
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Data {
|
||||
now: [DateTime<Local>; 2],
|
||||
fdate: [String; 2],
|
||||
ftime: [String; 2],
|
||||
timebar_ratio: [Option<f64>; 2],
|
||||
|
||||
timebar_type: TimeBarLength,
|
||||
started_at: DateTime<Local>,
|
||||
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn new(timebar_type: TimeBarLength) -> Self {
|
||||
let mut this = Self {
|
||||
now: [Default::default(); 2],
|
||||
fdate: ["".to_string(), "".to_string()],
|
||||
ftime: ["".to_string(), "".to_string()],
|
||||
timebar_ratio: [Default::default(); 2],
|
||||
started_at: Local::now(),
|
||||
idx: Default::default(),
|
||||
|
||||
timebar_type,
|
||||
};
|
||||
this.started_at = this.started_at.round_subsecs(0);
|
||||
this
|
||||
}
|
||||
pub fn update(
|
||||
&mut self,
|
||||
now: DateTime<Local>,
|
||||
|
@ -90,9 +71,6 @@ impl Data {
|
|||
#[inline]
|
||||
#[allow(clippy::missing_const_for_fn)] // no it's not const
|
||||
pub fn timebar_ratio(&self) -> Option<f64> {
|
||||
if self.timebar_type == TimeBarLength::Timer {
|
||||
return Some(0.0);
|
||||
}
|
||||
self.timebar_ratio[self.idx]
|
||||
}
|
||||
}
|
||||
|
@ -193,21 +171,8 @@ pub fn timebarw_label<'a>(
|
|||
// example with `-o` #17
|
||||
.checked_add_signed(len.into())
|
||||
.expect("could not calculate when the countdown finishes")
|
||||
.format(TIME_FORMAT);
|
||||
|
||||
let text: String = match clock.timebar_len().unwrap() {
|
||||
TimeBarLength::Timer => format!("{} + {time_now}", data.started_at.format(TIME_FORMAT)),
|
||||
TimeBarLength::Countup(_) | TimeBarLength::Custom(_) => format!(
|
||||
"{time_now} / {len} | {} -> {until}",
|
||||
last_reset.format(TIME_FORMAT)
|
||||
),
|
||||
_ => format!(
|
||||
"{time_now} / {len} | {} -> {until}",
|
||||
last_reset.with_second(0).unwrap().format(TIME_FORMAT)
|
||||
),
|
||||
};
|
||||
|
||||
Paragraph::new(text)
|
||||
.format("%H:%M:%S");
|
||||
Paragraph::new(format!("{time_now} / {len} ({until})"))
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default().padding(Padding::right(if inner_rect.width > 80 {
|
||||
|
|
|
@ -90,7 +90,7 @@ fn mock_tests() {
|
|||
info!("0s=0.0");
|
||||
}
|
||||
{
|
||||
let mut data = Data::new(clock::timebar::TimeBarLength::Day);
|
||||
let mut data = Data::default();
|
||||
let now = Local::now();
|
||||
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
|
||||
assert_eq!(data.timebar_ratio(), Some(0.1));
|
||||
|
|
Reference in a new issue