Skip to content

Commit

Permalink
TxtTask: add due and threshold fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JMoerman committed Nov 25, 2021
1 parent b7c39dc commit 629cfc7
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 27 deletions.
111 changes: 96 additions & 15 deletions src/Todo.txt/TxtTask.vala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ class GOFI.TXT.TxtTask : TodoTask {
default = null;
}

public GOFI.Date? due_date {
public get;
public set;
default = null;
}

public GOFI.Date? threshold_date {
public get;
public set;
default = null;
}

public uint8 priority {
public get;
public set;
Expand Down Expand Up @@ -236,6 +248,18 @@ class GOFI.TXT.TxtTask : TodoTask {
continue;
}
break;
case "due":
if (is_date (t.content)) {
due_date = string_to_date (t.content);
continue;
}
break;
case "t":
if (is_date (t.content)) {
threshold_date = string_to_date (t.content);
continue;
}
break;
}
}
parsed_parts += (owned) t;
Expand All @@ -256,6 +280,8 @@ class GOFI.TXT.TxtTask : TodoTask {
parse_priority (parts, ref index);

duration = 0;
due_date = null;
threshold_date = null;
set_descr_parts (parse_description (parts, index));
}

Expand Down Expand Up @@ -292,6 +318,7 @@ class GOFI.TXT.TxtTask : TodoTask {

descr_builder.append (p.content);
}

return descr_builder.str;
}

Expand Down Expand Up @@ -328,6 +355,15 @@ class GOFI.TXT.TxtTask : TodoTask {
str_builder.append (" duration:");
append_duration (this.duration, str_builder);
}
if (threshold_date != null) {
str_builder.append (" t:");
str_builder.append (dt_to_string (threshold_date.dt));
}

if (due_date != null) {
str_builder.append (" due:");
str_builder.append (dt_to_string (due_date.dt));
}

return str_builder.str;
}
Expand Down Expand Up @@ -370,6 +406,16 @@ class GOFI.TXT.TxtTask : TodoTask {
append_duration (duration, str_builder);
}

if (threshold_date != null) {
str_builder.append (" t:");
str_builder.append (dt_to_string (threshold_date.dt));
}

if (due_date != null) {
str_builder.append (" due:");
str_builder.append (dt_to_string (due_date.dt));
}

return str_builder.str;
}

Expand All @@ -380,6 +426,17 @@ class GOFI.TXT.TxtTask : TodoTask {
if (this.priority == other.priority) {
int cmp_tmp;

// Sort by due date
if (this.due_date != null) {
if (other.due_date != null) {
cmp_tmp = this.due_date.compare (other.due_date);
} else {
return -1;
}
} else if (other.due_date != null) {
return 1;
}

// Sort by description, case insensitive
cmp_tmp = this.description.ascii_casecmp (other.description);
if (cmp_tmp != 0) {
Expand All @@ -392,6 +449,17 @@ class GOFI.TXT.TxtTask : TodoTask {
return cmp_tmp;
}

// Sort by threshold date
if (this.threshold_date != null) {
if (other.threshold_date != null) {
cmp_tmp = this.threshold_date.compare (other.threshold_date);
} else {
return -1;
}
} else if (other.threshold_date != null) {
return 1;
}

// Sort by creation date
if (this.creation_date != null) {
if (other.creation_date != null) {
Expand Down Expand Up @@ -420,45 +488,58 @@ class GOFI.TXT.TxtTask : TodoTask {

internal string? assert_equal (TxtTask other) {
if (this.priority != other.priority) {
return "\"%s\" != \"%s\": Priorities don't match".printf (
this.to_txt (true), other.to_txt (true)
return "Priorities don't match: %u != %u".printf (
(uint) this.priority, (uint) other.priority
);
}
if (this.done != other.done) {
return "\"%s\" != \"%s\": Completion status doesn't match".printf (
this.to_txt (true), other.to_txt (true)
);
return "Completion status doesn't match";
}
bool a, b;
a = this.creation_date != null;
b = other.creation_date != null;
if (a != b || (a && b && this.creation_date.compare (other.creation_date) != 0)) {
return "\"%s\" != \"%s\": Creation dates don't match".printf (
this.to_txt (true), other.to_txt (true)
return "Creation dates don't match: %s != %s".printf (
a ? dt_to_string (this.creation_date.dt) : "null",
b ? dt_to_string (other.creation_date.dt) : "null"
);
}
a = this.completion_date != null;
b = other.completion_date != null;
if (a != b || (a && b && this.completion_date.compare (other.completion_date) != 0)) {
return "\"%s\" != \"%s\": Completion dates don't match".printf (
this.to_txt (true), other.to_txt (true)
return "Completion dates don't match: %s != %s".printf (
a ? dt_to_string (this.completion_date.dt) : "null",
b ? dt_to_string (other.completion_date.dt) : "null"
);
}
a = this.threshold_date != null;
b = other.threshold_date != null;
if (a != b || (a && b && this.threshold_date.compare (other.threshold_date) != 0)) {
return "Threshold dates don't match: %s != %s".printf (
a ? dt_to_string (this.threshold_date.dt) : "null",
b ? dt_to_string (other.threshold_date.dt) : "null"
);
}
a = this.due_date != null;
b = other.due_date != null;
if (a != b || (a && b && this.due_date.compare (other.due_date) != 0)) {
return "Due dates don't match: %s != %s".printf (
a ? dt_to_string (this.due_date.dt) : "null",
b ? dt_to_string (other.due_date.dt) : "null"
);
}
if (this.description != other.description) {
return "\"%s\" != \"%s\": Descriptions don't match: \"%s\" != \"%s\"".printf (
this.to_txt (true), other.to_txt (true),
return "Descriptions don't match: \"%s\" != \"%s\"".printf (
this.description, other.description
);
}
if (this.timer_value != other.timer_value) {
return "\"%s\" != \"%s\": Timer values don't match: \"%u\" != \"%u\"".printf (
this.to_txt (true), other.to_txt (true),
return "Timer values don't match: \"%u\" != \"%u\"".printf (
this.timer_value, other.timer_value
);
}
if (this.timer_value != other.timer_value) {
return "\"%s\" != \"%s\": Duration values values don't match: \"%u\" != \"%u\"".printf (
this.to_txt (true), other.to_txt (true),
return "Duration values values don't match: \"%u\" != \"%u\"".printf (
this.duration, other.duration
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Todo.txt/Widgets/TaskRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ class GOFI.TXT.TaskRow: DragListRow {
);
}
}

//TODO: remove this once due and threshold dates are fully supported
if (task.threshold_date != null) {
markup_string += " t:" + TxtUtils.dt_to_string (task.threshold_date.dt);
}
if (task.due_date != null) {
markup_string += " due:" + TxtUtils.dt_to_string (task.due_date.dt);
}

if (done) {
markup_string = "<s>" + markup_string + "</s>";
}
Expand Down
49 changes: 37 additions & 12 deletions tests/todo.txt/TodoTaskTest.vala
Original file line number Diff line number Diff line change
Expand Up @@ -196,36 +196,61 @@ class TodoTaskTest : TestCase {
}
private const string TEST_TASK3_TXT = "(A) 2021-11-15 2020-10-13 @context +project Test task";

private static TxtTask build_test_task4 () {
TxtTask task = new TxtTask ("Test task", false);
task.threshold_date = new GOFI.Date.from_ymd (2021, 11, 16);
task.due_date = new GOFI.Date.from_ymd (2021, 11, 17);
return task;
}
private const string TEST_TASK4_TXT = "Test task t:2021-11-16 due:2021-11-17";

private void print_comp_error (TxtTask task1, TxtTask task2, string error_str) {
stdout.printf ("%s != %s: %s\n", task1.to_txt (true), task2.to_txt (true), error_str);
}

private void test_from_txt () {
string? error_str = build_test_task1 ().assert_equal (new TxtTask.from_todo_txt (TEST_TASK1_TXT, false));
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
error_str = build_test_task2 ().assert_equal (new TxtTask.from_todo_txt (TEST_TASK2_TXT, false));
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
error_str = build_test_task2 ().assert_equal (new TxtTask.from_todo_txt (TEST_TASK2_TXT.offset (2), true));
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
error_str = build_test_task3 ().assert_equal (new TxtTask.from_todo_txt (TEST_TASK3_TXT, false));
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
TxtTask[] reference_tasks = {};
TxtTask[] txt_tasks = {};
reference_tasks += build_test_task1 (); txt_tasks += new TxtTask.from_todo_txt (TEST_TASK1_TXT, false);
reference_tasks += build_test_task2 (); txt_tasks += new TxtTask.from_todo_txt (TEST_TASK2_TXT, false);
reference_tasks += build_test_task2 (); txt_tasks += new TxtTask.from_todo_txt (TEST_TASK2_TXT.offset (2), true);
reference_tasks += build_test_task3 (); txt_tasks += new TxtTask.from_todo_txt (TEST_TASK3_TXT, false);
reference_tasks += build_test_task4 (); txt_tasks += new TxtTask.from_todo_txt (TEST_TASK4_TXT, false);

assert_cmpuint (reference_tasks.length, CompareOperator.EQ, txt_tasks.length);

string error_str = null;
for (uint i = 0; i < reference_tasks.length; i++) {
var reference_taks = reference_tasks[i];
var txt_task = txt_tasks[i];

if ((error_str = reference_taks.assert_equal (txt_task)) != null) {
stdout.printf ("Task pair %u doesn't match\n", i);
print_comp_error (reference_taks, txt_task, error_str);
assert_not_reached ();
}
}
}

private void test_to_txt () {
var initial_task = build_test_task1 ();
var final_task = new TxtTask.from_todo_txt (initial_task.to_txt (true), false);
string? error_str = initial_task.assert_equal (final_task);
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
if (error_str != null) { print_comp_error (initial_task, final_task, error_str); assert_not_reached (); }

initial_task = build_test_task2 ();
final_task = new TxtTask.from_todo_txt (initial_task.to_txt (true), false);
error_str = initial_task.assert_equal (final_task);
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
if (error_str != null) { print_comp_error (initial_task, final_task, error_str); assert_not_reached (); }
final_task = new TxtTask.from_todo_txt (initial_task.to_txt (false), false);
assert_cmpuint (final_task.timer_value, CompareOperator.EQ, 0);
final_task.timer_value = initial_task.timer_value;
error_str = initial_task.assert_equal (final_task);
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
if (error_str != null) { print_comp_error (initial_task, final_task, error_str); assert_not_reached (); }

initial_task = build_test_task3 ();
final_task = new TxtTask.from_todo_txt (initial_task.to_txt (true), false);
error_str = initial_task.assert_equal (final_task);
if (error_str != null) { stdout.printf ("%s\n", error_str); assert_not_reached (); }
if (error_str != null) { print_comp_error (initial_task, final_task, error_str); assert_not_reached (); }
}
}

0 comments on commit 629cfc7

Please sign in to comment.