commands erweitert
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
[package]
|
||||
name = "timetracker"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
authors = ["Damian Wessels"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
|
||||
35
src/commands/edit.rs
Normal file
35
src/commands/edit.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use anyhow::Result;
|
||||
use crate::database::{get_entry_by_id, running_entry};
|
||||
use crate::{Entry, State};
|
||||
use crate::style::{style_string, Styles};
|
||||
|
||||
pub fn edit_task(
|
||||
id: &Option<usize>,
|
||||
start: &Option<String>,
|
||||
end: &Option<String>,
|
||||
move_to: &Option<String>,
|
||||
notes: &Option<String>,
|
||||
state: &mut State,
|
||||
) -> Result<()> {
|
||||
let running_entry = running_entry(&state.database,&state.current_sheet)?;
|
||||
|
||||
let entry = if let Some(id) = id {
|
||||
get_entry_by_id(id,&state.database)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if running_entry.is_none() && entry.is_none() {
|
||||
println!(
|
||||
"{}",
|
||||
style_string("The task was not found. Either the given id is invalid or there is no task running", Styles::Message)
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut entry = entry.unwrap_or_else(|| running_entry.unwrap());
|
||||
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -2,8 +2,10 @@ mod current;
|
||||
mod in_cmd;
|
||||
mod out;
|
||||
mod lists;
|
||||
mod edit;
|
||||
|
||||
pub use current::current_task;
|
||||
pub use in_cmd::start_task;
|
||||
pub use out::stop_task;
|
||||
pub use lists::list_sheets;
|
||||
pub use lists::list_sheets;
|
||||
pub use edit::edit_task;
|
||||
@@ -195,4 +195,27 @@ pub fn get_all_sheets(db: &Connection) -> Result<Vec<String>> {
|
||||
sheets.push(sheet?);
|
||||
}
|
||||
Ok(sheets)
|
||||
}
|
||||
|
||||
pub fn get_entry_by_id(id: &usize, db: &Connection) -> Result<Option<Entry>> {
|
||||
let query = "SELECT id, note, start, end, sheet FROM entries WHERE id = ?;";
|
||||
let mut stmt = db.prepare(query)?;
|
||||
let mut entries = stmt.query_map([id], |row| {
|
||||
let end = row
|
||||
.get::<usize, Option<String>>(3)?
|
||||
.map(|t| str_to_datetime(&t).unwrap());
|
||||
|
||||
let start = str_to_datetime(&row.get::<usize, String>(2)?).unwrap();
|
||||
|
||||
Ok(Entry {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
start,
|
||||
end,
|
||||
sheet: row.get(4)?,
|
||||
})
|
||||
})?;
|
||||
|
||||
let entry = entries.next();
|
||||
entry.transpose().context(format!("Failed to read entry"))
|
||||
}
|
||||
34
src/main.rs
34
src/main.rs
@@ -60,8 +60,31 @@ enum SubCommands {
|
||||
/// The timesehet to display, or the current one
|
||||
sheet: Option<String>,
|
||||
},
|
||||
/// Change timesheet
|
||||
Sheet {
|
||||
name: String,
|
||||
#[arg(short, long)]
|
||||
rename: Option<String>
|
||||
},
|
||||
/// List available timesheet
|
||||
List,
|
||||
/// Edit a task
|
||||
Edit {
|
||||
/// The ID of the Task to edit
|
||||
#[arg(short, long)]
|
||||
id: Option<usize>,
|
||||
/// Set a new start date and time for this task
|
||||
#[arg(short, long)]
|
||||
start: Option<String>,
|
||||
/// Set a new end date and time for this task
|
||||
#[arg(short, long)]
|
||||
end: Option<String>,
|
||||
/// Move this task to a different timesheet
|
||||
#[arg(short, long)]
|
||||
move_to: Option<String>,
|
||||
/// The new task description
|
||||
notes: Option<String>,
|
||||
},
|
||||
/// Shows the active task for the current sheet
|
||||
Current,
|
||||
}
|
||||
@@ -103,12 +126,23 @@ fn cli() -> Result<()> {
|
||||
filter_by_date,
|
||||
sheet,
|
||||
} => {}
|
||||
SubCommands::Sheet {name, rename} => {}
|
||||
SubCommands::List => {
|
||||
list_sheets(&state).context("Could not list sheets")?;
|
||||
}
|
||||
SubCommands::Current => {
|
||||
current_task(&state).context("could not get current task")?;
|
||||
}
|
||||
SubCommands::Edit {
|
||||
id,
|
||||
start,
|
||||
end,
|
||||
move_to,
|
||||
notes
|
||||
} => {
|
||||
edit_task(id,start,end,move_to,notes, &mut state).context("Could not edit task")?;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user