Skip to content

Commit

Permalink
add edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Apr 19, 2024
1 parent 3a74b53 commit 8558251
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tracker"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -12,6 +12,6 @@ clap = { version = "4", features = ["derive"] }
dirs = "5"
edit = "0.1.5"
figment = { version = "0.10", features = ["json", "yaml", "toml", "env"] }
inquire = "0.7.4"
inquire = { version = "0.7.4", features = ["editor"] }
serde = { version = "1", features = ["derive"] }
yansi = "1.0.1"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ A simple tool to track time.
$ tracker --help
Simple tool to do time tracking
Usage: tracker.exe [OPTIONS] <COMMAND>
Usage: tracker [OPTIONS] <COMMAND>
Commands:
add Add a track entry
view Display tracking list entries
delete Remove entries from a tracking list
edit Edit an entry from a tracking list
help Print this message or the help of the given subcommand(s)
Options:
Expand Down
96 changes: 96 additions & 0 deletions src/commands/edit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use super::Command;
use crate::{
store::{Entry, Store},
util::parse_date,
};
use anyhow::Result;
use chrono::{Local, NaiveDateTime, NaiveTime};
use clap::Args;
use inquire::{CustomType, Editor, Select, Text};

/// Edit an entry from a tracking list
#[derive(Args)]
pub struct Edit {
/// Date of the list
date: Option<String>,

/// Edit the latest added entry
#[arg(short, long)]
last: bool,
}

impl Command for Edit {
fn run(&self, store: &Store) -> Result<()> {
let date = match self.date {
Some(ref date_str) => parse_date(date_str)?,
None => Local::now().date_naive(),
};

let mut entries = store.list(date)?;
if entries.is_empty() {
anyhow::bail!("There are no entreis for the given date.")
}

let selected = match self.last {
true => entries
.last()
.ok_or_else(|| anyhow::anyhow!("no entries found"))?
.clone(),
false => {
entries.sort_by_key(|e| e.timestamp);
Select::new("Select entry to edit", entries.clone()).prompt()?
}
};

let time: NaiveTime = CustomType::new("Time")
.with_parser(&parse_time)
.with_formatter(&format_time)
.with_default_value_formatter(&format_time)
.with_default(selected.timestamp.time())
.with_error_message("Invalid value. Must be time i nformat %H:%M")
.prompt()?;

let timestamp = NaiveDateTime::new(selected.timestamp.date(), time);

let message = Text::new("Message")
.with_default(&selected.message)
.prompt()?;

let long = Editor::new("Long")
.with_predefined_text(&selected.long.clone().unwrap_or_default())
.prompt()?;

let long = long.trim();
let long = match long.is_empty() {
true => None,
false => Some(long.to_string()),
};

let new: Entry = Entry {
timestamp,
message,
long,
};

let new = entries
.iter()
.map(|e| {
if e == &selected {
new.clone()
} else {
e.clone()
}
})
.collect();

store.set(selected.timestamp.date(), new)
}
}

fn parse_time(s: &str) -> std::result::Result<NaiveTime, ()> {
NaiveTime::parse_from_str(s, "%H:%M").map_err(|_| ())
}

fn format_time(s: NaiveTime) -> String {
s.format("%H:%M").to_string()
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ re_export! {
add
view
delete
edit
}

pub trait Command {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ register_commands! {
Add
View
Delete
Edit
}

fn main() -> Result<()> {
Expand Down

0 comments on commit 8558251

Please sign in to comment.