generated from PlexSheep/rs-base
refactor(uidata): change name of the struct and the idx
cargo devel CI / cargo CI (push) Successful in 1m47s
Details
cargo devel CI / cargo CI (push) Successful in 1m47s
Details
This commit is contained in:
parent
edd1751a98
commit
0eac501b4a
|
@ -20,7 +20,7 @@ use std::time::Instant;
|
||||||
pub mod timebar;
|
pub mod timebar;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
use timebar::TimeBarLength;
|
use timebar::TimeBarLength;
|
||||||
use ui::UiData;
|
use ui::Data;
|
||||||
|
|
||||||
/// Make your terminal into a big clock
|
/// Make your terminal into a big clock
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
|
@ -190,7 +190,7 @@ impl Clock {
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let tick_rate = std::time::Duration::from_millis(100);
|
let tick_rate = std::time::Duration::from_millis(100);
|
||||||
let mut last_tick = Instant::now();
|
let mut last_tick = Instant::now();
|
||||||
let mut uidata: UiData = UiData::default();
|
let mut uidata: Data = Data::default();
|
||||||
self.setup()?;
|
self.setup()?;
|
||||||
loop {
|
loop {
|
||||||
let raw_time = chrono::Local::now().round_subsecs(0);
|
let raw_time = chrono::Local::now().round_subsecs(0);
|
||||||
|
@ -251,7 +251,7 @@ impl Clock {
|
||||||
fn ui(
|
fn ui(
|
||||||
&mut self,
|
&mut self,
|
||||||
terminal: &mut Terminal<CrosstermBackend<Stdout>>,
|
terminal: &mut Terminal<CrosstermBackend<Stdout>>,
|
||||||
data: &UiData,
|
data: &Data,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
terminal.draw(|frame| {
|
terminal.draw(|frame| {
|
||||||
debug!("rendering the ui");
|
debug!("rendering the ui");
|
||||||
|
|
|
@ -9,16 +9,16 @@ use crate::clock::timebar::TimeBarLength;
|
||||||
use super::Clock;
|
use super::Clock;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Default)]
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
pub struct UiData {
|
pub struct Data {
|
||||||
now: [DateTime<Local>; 2],
|
now: [DateTime<Local>; 2],
|
||||||
fdate: [String; 2],
|
fdate: [String; 2],
|
||||||
ftime: [String; 2],
|
ftime: [String; 2],
|
||||||
timebar_ratio: [Option<f64>; 2],
|
timebar_ratio: [Option<f64>; 2],
|
||||||
|
|
||||||
data_idx: usize,
|
idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiData {
|
impl Data {
|
||||||
pub fn update(
|
pub fn update(
|
||||||
&mut self,
|
&mut self,
|
||||||
now: DateTime<Local>,
|
now: DateTime<Local>,
|
||||||
|
@ -26,11 +26,11 @@ impl UiData {
|
||||||
ftime: String,
|
ftime: String,
|
||||||
timebar_ratio: Option<f64>,
|
timebar_ratio: Option<f64>,
|
||||||
) {
|
) {
|
||||||
self.data_idx ^= 1;
|
self.idx ^= 1;
|
||||||
self.now[self.data_idx] = now;
|
self.now[self.idx] = now;
|
||||||
self.fdate[self.data_idx] = fdate;
|
self.fdate[self.idx] = fdate;
|
||||||
self.ftime[self.data_idx] = ftime;
|
self.ftime[self.idx] = ftime;
|
||||||
self.timebar_ratio[self.data_idx] = timebar_ratio;
|
self.timebar_ratio[self.idx] = timebar_ratio;
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
if self.changed() {
|
if self.changed() {
|
||||||
trace!("update with change: {:#?}", self);
|
trace!("update with change: {:#?}", self);
|
||||||
|
@ -49,32 +49,32 @@ impl UiData {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn fdate(&self) -> &str {
|
pub fn fdate(&self) -> &str {
|
||||||
&self.fdate[self.data_idx]
|
&self.fdate[self.idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn ftime(&self) -> &str {
|
pub fn ftime(&self) -> &str {
|
||||||
&self.ftime[self.data_idx]
|
&self.ftime[self.idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn now(&self) -> &DateTime<Local> {
|
pub fn now(&self) -> &DateTime<Local> {
|
||||||
&self.now[self.data_idx]
|
&self.now[self.idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(clippy::missing_const_for_fn)] // no it's not const
|
#[allow(clippy::missing_const_for_fn)] // no it's not const
|
||||||
pub fn timebar_ratio(&self) -> Option<f64> {
|
pub fn timebar_ratio(&self) -> Option<f64> {
|
||||||
self.timebar_ratio[self.data_idx]
|
self.timebar_ratio[self.idx]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn timebarw<'a>(
|
pub fn timebarw<'a>(
|
||||||
clock: &mut Clock,
|
clock: &mut Clock,
|
||||||
data: &UiData,
|
data: &Data,
|
||||||
timebarw_padding: &[u16],
|
timebarw_padding: &[u16],
|
||||||
inner_rect: Rect,
|
inner_rect: Rect,
|
||||||
) -> Option<LineGauge<'a>> {
|
) -> Option<LineGauge<'a>> {
|
||||||
|
@ -122,7 +122,7 @@ pub fn timebarw<'a>(
|
||||||
|
|
||||||
pub fn timebarw_label<'a>(
|
pub fn timebarw_label<'a>(
|
||||||
clock: &Clock,
|
clock: &Clock,
|
||||||
data: &UiData,
|
data: &Data,
|
||||||
timebarw_padding: &[u16],
|
timebarw_padding: &[u16],
|
||||||
inner_rect: Rect,
|
inner_rect: Rect,
|
||||||
) -> Option<Paragraph<'a>> {
|
) -> Option<Paragraph<'a>> {
|
||||||
|
|
|
@ -63,7 +63,7 @@ fn mock_tests() {
|
||||||
use chrono::{Local, Timelike};
|
use chrono::{Local, Timelike};
|
||||||
use libpt::log::info;
|
use libpt::log::info;
|
||||||
|
|
||||||
use crate::clock::ui::UiData;
|
use crate::clock::ui::Data;
|
||||||
info!("doing the mock tests");
|
info!("doing the mock tests");
|
||||||
{
|
{
|
||||||
let mut c = Clock::parse_from(["some exec", "-mvvv"]);
|
let mut c = Clock::parse_from(["some exec", "-mvvv"]);
|
||||||
|
@ -81,7 +81,7 @@ fn mock_tests() {
|
||||||
info!("0s=0.0");
|
info!("0s=0.0");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let mut data = UiData::default();
|
let mut data = Data::default();
|
||||||
let now = Local::now();
|
let now = Local::now();
|
||||||
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
|
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
|
||||||
assert_eq!(data.timebar_ratio(), Some(0.1));
|
assert_eq!(data.timebar_ratio(), Some(0.1));
|
||||||
|
|
Reference in New Issue