Compare commits

..

No commits in common. "master" and "v0.2.1" have entirely different histories.

9 changed files with 27 additions and 157 deletions

View File

@ -1,19 +1,12 @@
name: Rust CI name: cargo devel CI
on: on:
pull_request:
branches:
- '**'
push: push:
branches: branches:
- '**' - '**'
# - '!master' # - '!master'
env:
CARGO_TERM_COLOR: always
jobs: jobs:
CI: format:
runs-on: ubuntu-latest
name: cargo CI name: cargo CI
permissions: permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the # Give the default GITHUB_TOKEN write permission to commit and push the
@ -23,11 +16,15 @@ jobs:
- name: get repo - name: get repo
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: install rust - name: install rust
uses: dtolnay/rust-toolchain@stable uses: https://github.com/dtolnay/rust-toolchain@stable
- name: install additional rust things - name: install additional rust things
run: | run: |
rustup component add rustfmt rustup component add rustfmt
rustup component add clippy rustup component add clippy
- name: install additional system dependencies
run: |
apt update
apt install libdbus-1-dev libasound2-dev -y
- name: config custom registry - name: config custom registry
run: | run: |
mkdir -p ~/.cargo/ mkdir -p ~/.cargo/
@ -37,19 +34,15 @@ jobs:
echo '[registries.cscherr]' >> ~/.cargo/config.toml echo '[registries.cscherr]' >> ~/.cargo/config.toml
echo 'index = "https://git.cscherr.de/PlexSheep/_cargo-index.git"' >> ~/.cargo/config.toml echo 'index = "https://git.cscherr.de/PlexSheep/_cargo-index.git"' >> ~/.cargo/config.toml
cat ~/.cargo/config.toml cat ~/.cargo/config.toml
- name: install additional system dependencies
run: sudo apt-get install -y libasound2-dev libdbus-1-dev
- name: cargo clippy check - name: cargo clippy check
run: cargo clippy --all-features --all-targets --workspace run: cargo clippy --all-features --all-targets --workspace
- name: cargo clippy fix - name: cargo clippy fix
run: cargo clippy --fix --all-features --all-targets --workspace run: cargo clippy --fix --all-features --all-targets --workspace
- name: cargo fmt - name: cargo fmt
run: cargo fmt --all run: cargo fmt --all
- name: cargo test
run: cargo test --all-features --all-targets --workspace
- name: commit back to repository - name: commit back to repository
uses: stefanzweifel/git-auto-commit-action@v5 uses: https://github.com/stefanzweifel/git-auto-commit-action@v5
with: with:
# Optional. Commit message for the created commit. # Optional. Commit message for the created commit.
# Defaults to "Apply automatic changes" # Defaults to "Apply automatic changes"
commit_message: "ci: automatic Rust CI changes" commit_message: automatic cargo CI changes

View File

@ -1,7 +0,0 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
# Check for updates every Monday
schedule:
interval: "weekly"

View File

@ -1,56 +0,0 @@
name: Release-plz
permissions:
pull-requests: write
contents: write
on:
push:
branches:
- master
- main
jobs:
# Release unpublished packages.
release-plz-release:
name: Release-plz release
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: install additional system dependencies
run: sudo apt-get install -y libasound2-dev libdbus-1-dev
- name: Run release-plz
uses: MarcoIeni/release-plz-action@v0.5
with:
command: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
# Create a PR with the new versions and changelog, preparing the next release.
release-plz-pr:
name: Release-plz PR
runs-on: ubuntu-latest
concurrency:
group: release-plz-${{ github.ref }}
cancel-in-progress: false
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run release-plz
uses: MarcoIeni/release-plz-action@v0.5
with:
command: release-pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

View File

@ -1,6 +1,6 @@
[package] [package]
name = "crock" name = "crock"
version = "0.3.1" version = "0.2.1"
edition = "2021" edition = "2021"
publish = true publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"] authors = ["Christoph J. Scherr <software@cscherr.de>"]

View File

@ -2,15 +2,15 @@
![Project badge](https://img.shields.io/badge/language-Rust-blue.svg) ![Project badge](https://img.shields.io/badge/language-Rust-blue.svg)
![Crates.io License](https://img.shields.io/crates/l/crock) ![Crates.io License](https://img.shields.io/crates/l/crock)
![GitHub Release](https://img.shields.io/github/v/release/PlexSheep/crock) ![Gitea Release](https://img.shields.io/gitea/v/release/PlexSheep/crock?gitea_url=https%3A%2F%2Fgit.cscherr.de)
![GitHub language count](https://img.shields.io/github/languages/count/PlexSheep/crock) ![Gitea language count](https://img.shields.io/gitea/languages/count/PlexSheep/crock?gitea_url=https%3A%2F%2Fgit.cscherr.de)
A little clock for your terminal, written in rust. A little clock for your terminal, written in rust.
![screenshot](data/media/screenshot.png) ![screenshot](data/media/screenshot.png)
* [GitHub](https://github.com/PlexSheep/crock) * [Original Repository](https://git.cscherr.de/PlexSheep/crock)
* [GitHub Mirror](https://github.com/PlexSheep/crock)
* [crates.io](https://crates.io/crates/crock) * [crates.io](https://crates.io/crates/crock)
## Compilation ## Compilation

View File

@ -30,7 +30,7 @@ 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)]
#[command(help_template = HELP_TEMPLATE, author, version)] #[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 #[allow(clippy::struct_excessive_bools)] // the struct is for cli parsing and we already use an
// ArgGroup // ArgGroup
pub struct Clock { pub struct Clock {
@ -82,8 +82,6 @@ impl Clock {
Some(TimeBarLength::Day) Some(TimeBarLength::Day)
} else if self.hour { } else if self.hour {
Some(TimeBarLength::Hour) Some(TimeBarLength::Hour)
} else if self.timer {
Some(TimeBarLength::Timer)
} else if self.countdown.is_some() { } else if self.countdown.is_some() {
Some(TimeBarLength::Countup( Some(TimeBarLength::Countup(
self.countdown.unwrap().as_secs() as i64 self.countdown.unwrap().as_secs() as i64
@ -113,12 +111,11 @@ impl Clock {
if let Some(len) = self.timebar_len() { if let Some(len) = self.timebar_len() {
let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap()); let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap());
match len { match len {
TimeBarLength::Countup(_) | TimeBarLength::Timer => { TimeBarLength::Countup(_) => {
// the count up should not reset. If the time is over, just keep it at 100% // the count up should not reset. If the time is over, just keep it at 100%
} }
TimeBarLength::Custom(_) => { TimeBarLength::Custom(_) => {
// BUG: this is not consistent, sometimes leads to wrong seconds if since_last_reset.num_seconds() >= 1
if since_last_reset.num_milliseconds() >= 100
&& since_last_reset.num_seconds() >= len.as_secs() && since_last_reset.num_seconds() >= len.as_secs()
{ {
self.last_reset = Some(Local::now().round_subsecs(0)); self.last_reset = Some(Local::now().round_subsecs(0));
@ -126,34 +123,19 @@ impl Clock {
} }
TimeBarLength::Minute => { TimeBarLength::Minute => {
if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 { if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 {
self.last_reset = Some( self.last_reset = Some(Local::now().round_subsecs(0));
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (minute)"); debug!("reset the time of the time bar (minute)");
} }
} }
TimeBarLength::Hour => { TimeBarLength::Hour => {
if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 { if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 {
self.last_reset = Some( self.last_reset = Some(Local::now().round_subsecs(0));
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (hour)"); debug!("reset the time of the time bar (hour)");
} }
} }
TimeBarLength::Day => { TimeBarLength::Day => {
if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 { if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 {
self.last_reset = Some( self.last_reset = Some(Local::now().round_subsecs(0));
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (day)"); debug!("reset the time of the time bar (day)");
} }
} }
@ -165,7 +147,7 @@ impl Clock {
if let Some(len) = self.timebar_len() { if let Some(len) = self.timebar_len() {
trace!("Local Time: {}", Local::now()); trace!("Local Time: {}", Local::now());
match len { match len {
TimeBarLength::Custom(_) | TimeBarLength::Countup(_) | TimeBarLength::Timer => { TimeBarLength::Custom(_) | TimeBarLength::Countup(_) => {
self.last_reset = Some(Local::now()); self.last_reset = Some(Local::now());
} }
TimeBarLength::Minute => { TimeBarLength::Minute => {
@ -222,7 +204,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: Data = Data::new(self.timebar_len()); 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);
@ -422,7 +404,7 @@ impl Clock {
.split(r); .split(r);
#[allow(clippy::cast_sign_loss)] #[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
let hlen_date: u16 = (f32::from(part[1].width) * 0.32) as u16; let hlen_date: u16 = (f32::from(part[1].width) * 0.35) as u16;
let subparts = Layout::default() let subparts = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([ .constraints([

View File

@ -4,7 +4,6 @@ use chrono::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeBarLength { pub enum TimeBarLength {
Timer,
Minute, Minute,
Hour, Hour,
Custom(i64), Custom(i64),
@ -20,7 +19,6 @@ impl TimeBarLength {
Self::Minute => 60, Self::Minute => 60,
Self::Day => 24 * 60 * 60, Self::Day => 24 * 60 * 60,
Self::Hour => 60 * 60, Self::Hour => 60 * 60,
Self::Timer => 1,
Self::Custom(secs) | Self::Countup(secs) => secs, Self::Custom(secs) | Self::Countup(secs) => secs,
} }
} }
@ -40,9 +38,6 @@ impl Default for TimeBarLength {
impl Display for TimeBarLength { impl Display for TimeBarLength {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if *self == Self::Timer {
return write!(f, "");
}
let buf = match self { let buf = match self {
Self::Minute => humantime::Duration::from( Self::Minute => humantime::Duration::from(
Duration::minutes(1) Duration::minutes(1)
@ -64,7 +59,6 @@ impl Display for TimeBarLength {
.to_std() .to_std()
.expect("could not convert chrono time to std time"), .expect("could not convert chrono time to std time"),
), ),
Self::Timer => unreachable!(),
}; };
write!(f, "{buf}") write!(f, "{buf}")
} }

View File

@ -8,38 +8,18 @@ use crate::clock::timebar::TimeBarLength;
use super::Clock; use super::Clock;
pub const TIME_FORMAT: &str = "%H:%M:%S";
pub const TIME_FORMAT_SHORT: &str = "%H:%M";
// TODO: make this a ringbuffer with a custom struct inside? // TODO: make this a ringbuffer with a custom struct inside?
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct Data { 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],
timebar_type: Option<TimeBarLength>,
started_at: DateTime<Local>,
idx: usize, idx: usize,
} }
impl Data { impl Data {
pub fn new(timebar_type: Option<TimeBarLength>) -> Self {
let mut this = Self {
now: [DateTime::default(); 2],
fdate: [String::new(), String::new()],
ftime: [String::new(), String::new()],
timebar_ratio: [Option::default(); 2],
started_at: Local::now(),
idx: usize::default(),
timebar_type,
};
this.started_at = this.started_at.round_subsecs(0);
this
}
pub fn update( pub fn update(
&mut self, &mut self,
now: DateTime<Local>, now: DateTime<Local>,
@ -91,9 +71,6 @@ impl Data {
#[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> {
if self.timebar_type.is_some() && self.timebar_type.unwrap() == TimeBarLength::Timer {
return Some(0.0);
}
self.timebar_ratio[self.idx] self.timebar_ratio[self.idx]
} }
} }
@ -194,21 +171,8 @@ pub fn timebarw_label<'a>(
// example with `-o` #17 // example with `-o` #17
.checked_add_signed(len.into()) .checked_add_signed(len.into())
.expect("could not calculate when the countdown finishes") .expect("could not calculate when the countdown finishes")
.format(TIME_FORMAT_SHORT); .format("%H:%M:%S");
Paragraph::new(format!("{time_now} / {len} ({until})"))
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_SHORT)
),
_ => format!(
"{time_now} / {len} | {} -> {until}",
last_reset.with_second(0).unwrap().format(TIME_FORMAT_SHORT)
),
};
Paragraph::new(text)
.alignment(Alignment::Center) .alignment(Alignment::Center)
.block( .block(
Block::default().padding(Padding::right(if inner_rect.width > 80 { Block::default().padding(Padding::right(if inner_rect.width > 80 {

View File

@ -90,7 +90,7 @@ fn mock_tests() {
info!("0s=0.0"); info!("0s=0.0");
} }
{ {
let mut data = Data::new(None); 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));