-
Notifications
You must be signed in to change notification settings - Fork 479
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Issue 1703 timewarrior block #1878
Open
Mice7R
wants to merge
15
commits into
greshake:master
Choose a base branch
from
Mice7R:issue_1703_timewarrior
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6b1313e
timewarrior block
Mice7R fbba1f8
Add doc
Mice7R 29326bd
Format code
Mice7R 2641bc7
Fix example to use new formatter arguments
Mice7R 7aeac25
Add missing Action documentation
Mice7R feceb0c
Add timew word in cspell.yml
Mice7R d277971
Add deny_unknown_fields
Mice7R 6812cc1
Simplify get_current_timew_task
Mice7R 62bc9ce
Check timew stop/continue exit code
Mice7R 1cec907
Fix small issues in doc
Mice7R d0b70d0
refactor some code to make it more rusty
Mice7R 1a32ebb
Update block to 0.32.3
Mice7R 5a4e842
Remove deprecated DateTime::from_utc
Mice7R 3402a53
Fix non-zero exit when continue
Mice7R cfcd9c5
Fix typo
Mice7R File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,7 @@ words: | |
- sysfs | ||
- tebi | ||
- tera | ||
- timew | ||
- tzname | ||
- tzset | ||
- udev | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,6 +193,7 @@ define_blocks!( | |
taskwarrior, | ||
temperature, | ||
time, | ||
timewarrior, | ||
tea_timer, | ||
toggle, | ||
uptime, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
//! Time and information of the current timewarrior task | ||
//! | ||
//! Clicking left mouse stops or resumes the task | ||
//! | ||
//! # Configuration | ||
//! | ||
//! Key | Values | Default | ||
//! ----|--------|-------- | ||
//! `interval` | Update interval in seconds | `30` | ||
//! `format` | A string to customise the output of the block. See placeholders. | <code>" $icon {$elapsed |}"</code> | ||
//! `info` | The threshold of minutes the task turns into a info state | - | ||
//! `good` | The threshold of minutes the task turns into a good state | - | ||
//! `warning` | The threshold of minutes the task turns into a warning state | - | ||
//! `critical` | The threshold of minutes the task turns into a critical state | - | ||
//! | ||
//! Placeholder | Value | Type | Unit | ||
//! ------------|-------|------|------ | ||
//! `icon` | A static icon | Icon | - | ||
//! `elapsed`| Elapsed time in format H:MM (Only present if task is active) | Text | - | ||
//! `tags` | Tags of the active task separated by space (Only present if task is active) | Text | - | ||
//! `annotation` | Annotation of the active task (Only present if task is active) | Text | - | ||
//! | ||
//! Action | Default button | ||
//! ----------------|---------------- | ||
//! `stop_continue` | Left | ||
//! | ||
//! # Example | ||
//! ```toml | ||
//! [[block]] | ||
//! block = "timewarrior" | ||
//! format = " $icon {$tags.str(w:8,rot_interval:4) $elapsed|}" | ||
//! ``` | ||
//! | ||
//! # Icons Used | ||
//! - `tasks` | ||
|
||
use super::prelude::*; | ||
use chrono::DateTime; | ||
use tokio::process::Command; | ||
|
||
#[derive(Deserialize, Debug, SmartDefault)] | ||
#[serde(deny_unknown_fields, default)] | ||
pub struct Config { | ||
#[default(30.into())] | ||
interval: Seconds, | ||
format: FormatConfig, | ||
|
||
info: Option<u64>, | ||
good: Option<u64>, | ||
warning: Option<u64>, | ||
critical: Option<u64>, | ||
} | ||
|
||
pub async fn run(config: &Config, api: &CommonApi) -> Result<()> { | ||
let mut actions = api.get_actions()?; | ||
api.set_default_actions(&[(MouseButton::Left, None, "stop_continue")])?; | ||
|
||
let widget = Widget::new().with_format(config.format.with_default(" $icon {$elapsed|}")?); | ||
|
||
loop { | ||
let mut values = map! { | ||
"icon" => Value::icon("tasks"), | ||
}; | ||
let mut state = State::Idle; | ||
let mut widget = widget.clone(); | ||
|
||
let data = get_current_timewarrior_task().await?; | ||
if let Some(tw) = data { | ||
if tw.end.is_none() { | ||
// only show active tasks | ||
let elapsed = chrono::Utc::now() - tw.start; | ||
|
||
// calculate state | ||
for (level, st) in [ | ||
(&config.critical, State::Critical), | ||
(&config.warning, State::Warning), | ||
(&config.good, State::Good), | ||
(&config.info, State::Info), | ||
] { | ||
if let Some(value) = level { | ||
if (elapsed.num_minutes() as u64) >= *value { | ||
state = st; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of keeping a |
||
break; | ||
} | ||
} | ||
} | ||
|
||
values.insert("tags".into(), Value::text(tw.tags.join(" "))); | ||
|
||
let elapsedstr = | ||
format!("{}:{:0>2}", elapsed.num_hours(), elapsed.num_minutes() % 60); | ||
values.insert("elapsed".into(), Value::text(elapsedstr)); | ||
|
||
if let Some(annotation) = tw.annotation { | ||
values.insert("annotation".into(), Value::text(annotation)); | ||
} | ||
} | ||
} | ||
|
||
widget.state = state; | ||
widget.set_values(values); | ||
api.set_widget(widget)?; | ||
|
||
select! { | ||
_ = sleep(config.interval.0) => (), | ||
_ = api.wait_for_update_request() => (), | ||
Some(action) = actions.recv() => match action.as_ref() { | ||
"stop_continue" => { stop_continue().await?; } | ||
_ => (), | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Raw output from timew | ||
#[derive(Deserialize, Debug)] | ||
struct TimewarriorRAW { | ||
pub id: u32, | ||
pub start: String, | ||
pub tags: Vec<String>, | ||
pub annotation: Option<String>, | ||
pub end: Option<String>, | ||
} | ||
|
||
/// TimeWarrior entry | ||
#[derive(Debug, PartialEq, Deserialize)] | ||
#[serde(from = "TimewarriorRAW")] | ||
struct TimewarriorData { | ||
Mice7R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub id: u32, | ||
pub start: DateTime<chrono::offset::Utc>, | ||
pub tags: Vec<String>, | ||
pub annotation: Option<String>, | ||
pub end: Option<DateTime<chrono::offset::Utc>>, | ||
} | ||
|
||
impl From<TimewarriorRAW> for TimewarriorData { | ||
fn from(item: TimewarriorRAW) -> Self { | ||
Self { | ||
id: item.id, | ||
tags: item.tags, | ||
annotation: item.annotation, | ||
start: chrono::TimeZone::from_utc_datetime( | ||
&chrono::Utc, | ||
&chrono::NaiveDateTime::parse_from_str(&item.start, "%Y%m%dT%H%M%SZ").unwrap() | ||
), | ||
end: item.end.map(|v| { | ||
chrono::TimeZone::from_utc_datetime( | ||
&chrono::Utc, | ||
&chrono::NaiveDateTime::parse_from_str(&v, "%Y%m%dT%H%M%SZ").unwrap() | ||
) | ||
}), | ||
} | ||
} | ||
} | ||
|
||
/// Format a DateTime given a format string | ||
#[allow(dead_code)] | ||
fn format_datetime(date: &DateTime<chrono::Utc>, format: &str) -> String { | ||
date.format(format).to_string() | ||
} | ||
|
||
/// Execute "timew export now" and return the current task (if any) | ||
async fn get_current_timewarrior_task() -> Result<Option<TimewarriorData>> { | ||
let out = Command::new("timew") | ||
.args(["export", "now"]) | ||
.output() | ||
.await | ||
.error("failed to run timewarrior")? | ||
.stdout; | ||
Ok(serde_json::from_slice::<Vec<TimewarriorData>>(&out) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.next()) | ||
} | ||
|
||
/// Stop or continue a task | ||
async fn stop_continue() -> Result<()> { | ||
let is_stopped = get_current_timewarrior_task() | ||
.await? | ||
.map_or(true, |tw| tw.end.is_some()); | ||
let args = if is_stopped { "continue" } else { "stop" }; | ||
Command::new("timew") | ||
.args([args]) | ||
.stdout(std::process::Stdio::null()) | ||
.spawn() | ||
.error("Error spawning timew")? | ||
.wait() | ||
.await | ||
.error("Error executing stop/continue")? | ||
.success() | ||
.then_some(()) | ||
.error("timew exited with non-zero value when attempting to stop/continue") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make these
i64
to avoid casting(elapsed.num_minutes() as u64)
?