Skip to content

Commit

Permalink
let set and unset operate on id ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Sep 22, 2024
1 parent 9de0f8a commit d9d7270
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl Task {
&self.props
}

pub fn set_property(&mut self, prop: String, value: String) {
self.props.insert(prop, value);
pub fn set_property(&mut self, prop: &str, value: &str) {
self.props.insert(prop.to_string(), value.to_string());
}

pub fn has_property(&self, prop: &str) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ enum Command {
},
/// Set a property
Set {
/// task ID
id: String,
/// space separated task IDs
ids: Vec<String>,
/// property name
prop_name: String,
/// property value
Expand Down Expand Up @@ -474,7 +474,7 @@ fn main() -> ExitCode {
Some(Command::Create { name, description, no_desc, push, remote }) => task_create(name, description, no_desc, push, &remote),
Some(Command::Status { ids, status, push, remote, no_color }) => task_status(ids, status, push, &remote, no_color),
Some(Command::Get { id, prop_name }) => task_get(id, prop_name),
Some(Command::Set { id, prop_name, value, push, remote, no_color }) => task_set(&id, prop_name, value, push, &remote, no_color),
Some(Command::Set { ids, prop_name, value, push, remote, no_color }) => task_set(ids, prop_name, value, push, &remote, no_color),
Some(Command::Unset { ids, prop_name }) => task_unset(ids, prop_name),
Some(Command::Edit { id, prop_name }) => task_edit(id, prop_name),
Some(Command::Comment { subcommand }) => task_comment(subcommand),
Expand Down
84 changes: 44 additions & 40 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ pub(crate) fn task_status(ids: Vec<String>, status: String, push: bool, remote:
let status_manager = StatusManager::new();
let status = status_manager.get_full_status_name(&status);
let ids = ids.into_iter().expand_range().collect::<Vec<_>>();
for id in &ids {
task_set(id, "status".to_string(), status.clone(), push, remote, no_color);
}

if push {
task_push(ids, remote, false, no_color);
}
task_set(ids, "status".to_string(), status.clone(), push, remote, no_color);

true
}
Expand All @@ -82,49 +77,58 @@ pub(crate) fn task_get(id: String, prop_name: String) -> bool {
}
}

pub(crate) fn task_set(id: &String, prop_name: String, value: String, push: bool, remote: &Option<String>, no_color: bool) -> bool {
pub(crate) fn task_set(ids: Vec<String>, prop_name: String, value: String, push: bool, remote: &Option<String>, no_color: bool) -> bool {
let ids = ids.into_iter().expand_range().collect::<Vec<_>>();
match prop_name.as_str() {
"id" => {
match gittask::update_task_id(&id, &value) {
Ok(_) => {
println!("Task ID {id} -> {value} updated");
if let Err(e) = gittask::delete_tasks(&[&id]) {
eprintln!("ERROR: {e}");
}
if push {
task_push(vec![id.to_string()], remote, false, no_color);
}
true
},
Err(e) => error_message(format!("ERROR: {e}")),
if ids.len() > 1 {
return error_message("Can't change IDs of several tasks to the same value".to_string());
}

for id in ids {
match gittask::update_task_id(&id, &value) {
Ok(_) => {
println!("Task ID {id} -> {value} updated");
if let Err(e) = gittask::delete_tasks(&[&id]) {
eprintln!("ERROR: {e}");
}
if push {
task_push(vec![id.to_string()], remote, false, no_color);
}
},
Err(e) => eprintln!("ERROR: {e}"),
}
}
},
_ => {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
task.set_property(prop_name, value);

match gittask::update_task(task) {
Ok(_) => {
println!("Task ID {id} updated");
for id in ids {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
task.set_property(&prop_name, &value);

if push {
task_push(vec![id.to_string()], remote, false, no_color);
}
match gittask::update_task(task) {
Ok(_) => {
println!("Task ID {id} updated");

true
},
Err(e) => error_message(format!("ERROR: {e}")),
}
},
Ok(None) => error_message(format!("Task ID {id} not found")),
Err(e) => error_message(format!("ERROR: {e}")),
if push {
task_push(vec![id.to_string()], remote, false, no_color);
}
},
Err(e) => eprintln!("ERROR: {e}"),
}
},
Ok(None) => eprintln!("Task ID {id} not found"),
Err(e) => eprintln!("ERROR: {e}"),
}
}
}
}

true
}

pub(crate) fn task_unset(ids: Vec<String>, prop_name: String) -> bool {
let ids = ids.into_iter().expand_range().collect::<Vec<_>>();
for id in ids {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
Expand Down Expand Up @@ -172,7 +176,7 @@ pub(crate) fn task_edit(id: String, prop_name: String) -> bool {
Some(value) => {
match get_text_from_editor(Some(value)) {
Some(text) => {
task.set_property(prop_name, text);
task.set_property(&prop_name, &text);
match gittask::update_task(task) {
Ok(_) => success_message(format!("Task ID {id} updated")),
Err(e) => error_message(format!("ERROR: {e}")),
Expand Down Expand Up @@ -436,9 +440,9 @@ fn import_remote_task(remote_task: Task, no_comments: bool) -> Result<Option<Str
&& (no_comments || comments_are_equal(local_task.get_comments(), remote_task.get_comments())) {
Ok(None)
} else {
local_task.set_property("name".to_string(), remote_task.get_property("name").unwrap().to_string());
local_task.set_property("description".to_string(), remote_task.get_property("description").unwrap().to_string());
local_task.set_property("status".to_string(), remote_task.get_property("status").unwrap().to_string());
local_task.set_property("name", remote_task.get_property("name").unwrap());
local_task.set_property("description", remote_task.get_property("description").unwrap());
local_task.set_property("status", remote_task.get_property("status").unwrap());
if !no_comments {
if let Some(comments) = remote_task.get_comments() {
local_task.set_comments(comments.to_vec());
Expand Down
6 changes: 3 additions & 3 deletions src/operations/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) fn task_config_status_set(name: String, param: String, value: String)
Ok(tasks) => {
for mut task in tasks {
if task.get_property("status").unwrap() == prev_status.as_str() {
task.set_property("status".to_string(), value.clone());
task.set_property("status", &value);
if let Err(e) = gittask::update_task(task) {
eprintln!("ERROR: {e}");
}
Expand Down Expand Up @@ -212,8 +212,8 @@ pub(crate) fn task_config_properties_set(name: String, param: String, value: Str
Ok(tasks) => {
for mut task in tasks {
if task.has_property(&name) {
let task_prop_value = task.get_property(&name).unwrap();
task.set_property(value.clone(), task_prop_value.to_string());
let task_prop_value = task.get_property(&name).unwrap().clone();
task.set_property(&value, &task_prop_value);
task.delete_property(&name);
if let Err(e) = gittask::update_task(task) {
eprintln!("ERROR: {e}");
Expand Down

0 comments on commit d9d7270

Please sign in to comment.