Content-Length: 601092 | pFad | http://github.com/gaborbata/todo/commit/65cf0951a9e7a9f89deffe22ef94ac54ebde3768

66 Add possibility to delete specific note entries · gaborbata/todo@65cf095 · GitHub
Skip to content

Commit

Permalink
Add possibility to delete specific note entries
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbata committed Mar 24, 2021
1 parent 45cf789 commit 65cf095
Show file tree
Hide file tree
Showing 10 changed files with 1,018 additions and 987 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Commands:
* rename <tasknumber> <text> rename task
* del <tasknumber> delete task
* note <tasknumber> <text> add note to task
* delnote <tasknumber> delete all notes from task
* delnote <tasknumber> [number] delete a specific or all notes from task
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
Expand Down
34 changes: 15 additions & 19 deletions bin/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def execute(arguments)
raise action + ' command requires at least two parameters' if args.length < 2
add_note(args.first.to_i, args[1..-1].join(' '))
when 'delnote'
raise action + ' command requires exactly one parameter' if args.length != 1
delete_note(args.first.to_i)
raise action + ' command requires one or two parameters' if args.length < 1 || args.length > 2
delete_note(args.first.to_i, args[1])
when 'list'
list(nil, args)
when 'show'
Expand Down Expand Up @@ -151,7 +151,7 @@ def usage
* rename <tasknumber> <text> rename task
* del <tasknumber> delete task
* note <tasknumber> <text> add note to task
* delnote <tasknumber> delete all notes from task
* delnote <tasknumber> [number] delete a specific or all notes from task
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
Expand Down Expand Up @@ -212,9 +212,7 @@ def load_tasks(item_to_check = nil)

def write_tasks(tasks)
File.open(TODO_FILE, 'w:UTF-8') do |file|
tasks.keys.sort.each do |key|
file.write(JSON.generate(tasks[key]) + "\n")
end
tasks.keys.sort.each { |key| file.write(JSON.generate(tasks[key]) + "\n") }
end
end

Expand All @@ -228,15 +226,9 @@ def postprocess_tags(task)
end

def add(text)
task = {
state: 'new',
title: text,
modified: @today.strftime(DATE_FORMAT)
}
task = { state: 'new', title: text, modified: @today.strftime(DATE_FORMAT) }
postprocess_tags(task)
File.open(TODO_FILE, 'a:UTF-8') do |file|
file.write(JSON.generate(task) + "\n")
end
File.open(TODO_FILE, 'a:UTF-8') { |file| file.write(JSON.generate(task) + "\n") }
list
end

Expand Down Expand Up @@ -344,17 +336,21 @@ def add_note(item, text)
end)
end

def delete_note(item)
def delete_note(item, num = nil)
update_task(item, :show, lambda do |task|
task.delete(:note)
return task.delete(:note) if num.to_s.empty?
raise "#{num.to_i}: Note does not exist" if num.to_i <= 0 || task[:note].to_a.size < num.to_i
task[:note].delete_at(num.to_i - 1)
task.delete(:note) if task[:note].empty?
end)
end

def show(item, tasks = nil)
tasks ||= load_tasks(item)
tasks[item].each do |key, value|
val = value.kind_of?(Array) ? "\n" + value.join("\n") : value
puts "#{colorize(key.to_s.rjust(10, ' ') + ':', :cyan)} #{val}"
tasks[item].each do |k, v|
v = "\n" + v.each_with_index.map { |n, i| "#{(i + 1).to_s.rjust(v.size.to_s.size, ' ')}: #{n}" }.
join("\n") if v.is_a?(Array)
puts "#{colorize(k.to_s.rjust(10, ' ') + ':', :cyan)} #{v}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Commands:
* rename <tasknumber> <text> rename task
* del <tasknumber> delete task
* note <tasknumber> <text> add note to task
* delnote <tasknumber> delete all notes from task
* delnote <tasknumber> [number] delete a specific or all notes from task
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
Expand Down
5 changes: 3 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "todo-jsonl",
"version": "1.0.3",
"version": "1.0.4",
"description": "todo list manager on the command-line inspired by todo.txt using the jsonl format",
"main": "todo.js",
"bin": {
Expand All @@ -27,7 +27,8 @@
"todotxt",
"todoapp",
"jsonlines",
"jsonl"
"jsonl",
"productivity"
],
"author": "Gabor Bata",
"license": "MIT",
Expand Down
1,151 changes: 576 additions & 575 deletions node/todo.js

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions node/todo.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def execute(arguments)
raise action + ' command requires at least two parameters' if args.length < 2
add_note(args.first.to_i, args[1..-1].join(' '))
when 'delnote'
raise action + ' command requires exactly one parameter' if args.length != 1
delete_note(args.first.to_i)
raise action + ' command requires one or two parameters' if args.length < 1 || args.length > 2
delete_note(args.first.to_i, args[1])
when 'list'
list(nil, args)
when 'show'
Expand Down Expand Up @@ -152,7 +152,7 @@ def usage
* rename <tasknumber> <text> rename task
* del <tasknumber> delete task
* note <tasknumber> <text> add note to task
* delnote <tasknumber> delete all notes from task
* delnote <tasknumber> [number] delete a specific or all notes from task
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
Expand Down Expand Up @@ -225,11 +225,7 @@ def postprocess_tags(task)
end

def add(text)
task = {
state: 'new',
title: text,
modified: @today.strftime(DATE_FORMAT)
}
task = { state: 'new', title: text, modified: @today.strftime(DATE_FORMAT) }
postprocess_tags(task)
`require('fs').appendFileSync(#{TODO_FILE}, #{JSON.generate(task) + "\n"}, 'utf8')`
list
Expand Down Expand Up @@ -339,17 +335,21 @@ def add_note(item, text)
end)
end

def delete_note(item)
def delete_note(item, num = nil)
update_task(item, :show, lambda do |task|
task.delete(:note)
return task.delete(:note) if num.to_s.empty?
raise "#{num.to_i}: Note does not exist" if num.to_i <= 0 || task[:note].to_a.size < num.to_i
task[:note].delete_at(num.to_i - 1)
task.delete(:note) if task[:note].empty?
end)
end

def show(item, tasks = nil)
tasks ||= load_tasks(item)
tasks[item].each do |key, value|
val = value.kind_of?(Array) ? "\n" + value.join("\n") : value
puts "#{colorize(key.to_s.rjust(10, ' ') + ':', :cyan)} #{val}"
tasks[item].each do |k, v|
v = "\n" + v.each_with_index.map { |n, i| "#{(i + 1).to_s.rjust(v.size.to_s.size, ' ')}: #{n}" }.
join("\n") if v.is_a?(Array)
puts "#{colorize(k.to_s.rjust(10, ' ') + ':', :cyan)} #{v}"
end
end

Expand Down
46 changes: 39 additions & 7 deletions test/test_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,21 @@ def test_delete_todo_should_reorganize_numbers
end

def test_add_note
$stdout = StringIO.new
@todo.execute ['note', '1', 'test']
assert_match(
/{"state":"new","title":"Buy Milk","modified":"\d{4}-\d{2}-\d{2}","note":\["test"\]}\r?\n/,
File.read(@todo_file)
)
assert_match(
/\e\[36m {5}state:\e\[0m new\n\e\[36m {5}title:\e\[0m Buy Milk\n\e\[36m modified:\e\[0m \d{4}-\d{2}-\d{2}\n\e\[36m {6}note:\e\[0m \ntest\n/,
/\e\[36m {5}state:\e\[0m new\n\e\[36m {5}title:\e\[0m Buy Milk\n\e\[36m modified:\e\[0m \d{4}-\d{2}-\d{2}\n\e\[36m {6}note:\e\[0m \n1: test\n/,
$stdout.string
)
end

def test_delete_notes
@todo.execute ['note', '1', 'a note']
$stdout = StringIO.new
@todo.execute ['delnote', '1']
assert_match(
/{"state":"new","title":"Buy Milk","modified":"\d{4}-\d{2}-\d{2}"}\r?\n/,
Expand All @@ -279,6 +282,41 @@ def test_delete_notes
)
end

def test_delete_specific_note
@todo.execute ['note', '1', 'first note']
@todo.execute ['note', '1', 'second note']
$stdout = StringIO.new
@todo.execute ['delnote', '1', '2']
assert_match(
/{"state":"new","title":"Buy Milk","modified":"\d{4}-\d{2}-\d{2}","note":\["first note"\]}\r?\n/,
File.read(@todo_file)
)
assert_match(
/\e\[36m {5}state:\e\[0m new\n\e\[36m {5}title:\e\[0m Buy Milk\n\e\[36m modified:\e\[0m \d{4}-\d{2}-\d{2}\n\e\[36m {6}note:\e\[0m \n1: first note\n/,
$stdout.string
)
end

def test_delete_last_note
@todo.execute ['note', '1', 'first note']
$stdout = StringIO.new
@todo.execute ['delnote', '1', '1']
assert_match(
/{"state":"new","title":"Buy Milk","modified":"\d{4}-\d{2}-\d{2}"}\r?\n/,
File.read(@todo_file)
)
assert_match(
/\e\[36m {5}state:\e\[0m new\n\e\[36m {5}title:\e\[0m Buy Milk\n\e\[36m modified:\e\[0m \d{4}-\d{2}-\d{2}\n/,
$stdout.string
)
end

def test_delete_non_existing_note
$stdout = StringIO.new
@todo.execute ['delnote', '1', '1']
assert_equal("\e[31mERROR:\e[0m 1: Note does not exist\n", $stdout.string)
end

def test_change_state_with_note
@todo.execute ['block', '1', 'note']
assert_match(
Expand Down Expand Up @@ -339,7 +377,6 @@ def test_list_non_matching_multiple_regex
end

def test_list_by_due_date
$stdout = StringIO.new
@todo.execute ['due', '1', Time.now.strftime(Todo::DATE_FORMAT)]
@todo.execute ['add', 'Buy Bread @unplanned']
$stdout = StringIO.new
Expand All @@ -348,7 +385,6 @@ def test_list_by_due_date
end

def test_list_by_next_7_days
$stdout = StringIO.new
@todo.execute ['due', '1', 'tomorrow']
@todo.execute ['add', 'Buy Bread @unplanned']
$stdout = StringIO.new
Expand All @@ -357,15 +393,13 @@ def test_list_by_next_7_days
end

def test_list_overdue_tasks
$stdout = StringIO.new
@todo.execute ['add', "Opean Tutankhamen's burial chamber due:1923-02-16"]
$stdout = StringIO.new
@todo.execute ['list', ':overdue']
assert_match(/ 2: \e\[37m\[ \]\e\[0m Opean Tutankhamen's burial chamber \e\[31m\(\d+d overdue\)\e\[0m\n/, $stdout.string)
end

def test_list_tasks_with_due_dates
$stdout = StringIO.new
@todo.execute ['due', '1', 'tomorrow']
@todo.execute ['add', 'Buy Bread @unplanned']
$stdout = StringIO.new
Expand All @@ -374,7 +408,6 @@ def test_list_tasks_with_due_dates
end

def test_list_tasks_with_notes
$stdout = StringIO.new
@todo.execute ['due', '1', 'tomorrow']
@todo.execute ['add', 'Buy Bread']
@todo.execute ['note', '2', 'A note']
Expand All @@ -384,7 +417,6 @@ def test_list_tasks_with_notes
end

def test_list_priority_tasks
$stdout = StringIO.new
@todo.execute ['add', 'Very important task']
@todo.execute ['prio', '2']
$stdout = StringIO.new
Expand Down
4 changes: 2 additions & 2 deletions todo.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'todo-jsonl'
s.version = '1.0.3'
s.date = '2021-03-22'
s.version = '1.0.4'
s.date = '2021-03-24'
s.summary = 'todo list manager on the command-line inspired by todo.txt using the jsonl format'
s.authors = ['Gabor Bata']
s.homepage = 'https://github.com/gaborbata/todo'
Expand Down
Loading

0 comments on commit 65cf095

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/gaborbata/todo/commit/65cf0951a9e7a9f89deffe22ef94ac54ebde3768

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy