From 1b236f187b06c41658b8b8f9f41da710099a1954 Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Fri, 12 Jun 2020 11:00:41 +0200 Subject: [PATCH] Adds TaskWikiOpen command --- Dockerfile | 1 + doc/taskwiki.txt | 15 ++++++ ftplugin/vimwiki/taskwiki.vim | 6 +++ taskwiki/main.py | 33 ++++++++++++ tests/test_selected.py | 94 +++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+) diff --git a/Dockerfile b/Dockerfile index 7783178a1..b00fb43f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -69,6 +69,7 @@ RUN apk add --no-cache \ make \ patchelf \ tzdata \ + xdg-utils \ xvfb-run RUN ln -sf /usr/share/zoneinfo/Etc/UTC /etc/localtime diff --git a/doc/taskwiki.txt b/doc/taskwiki.txt index 790e27641..67dc45a25 100644 --- a/doc/taskwiki.txt +++ b/doc/taskwiki.txt @@ -506,6 +506,8 @@ vn m |:TaskWikiMod| n t |:TaskWikiTags| vn + |:TaskWikiStart| vn - |:TaskWikiStop| + n o |:TaskWikiOpen| + n n |:TaskWikiNote| ============================================================================= 7. COMMANDS *taskwiki-commands* @@ -604,6 +606,15 @@ selected tasks. *:TaskWikiMod* [mods] Opens a prompt for task modification, for selected task(s). +*:TaskWikiOpen* + Opens annotations in the selected task(s) containing links to local files, + urls, etc. + +*:TaskWikiNote* + Adds a note to the task under cursor and opens it to be edit. This command + is compatible with the taskopen utility. See + |taskwiki_taskopen_notes_folder|. + ---------------------------------------------------------------------------- Interactive commands. @@ -733,6 +744,10 @@ constructs. Example: let g:taskwiki_maplocalleader=",t" +*taskwiki_taskopen_notes_folder* + The folder where taskopen has been configured to store notes. Defaults + to $HOME/tasknotes (taskopen default). + ============================================================================= 9. TROUBLESHOOTING *taskwiki-trouble* diff --git a/ftplugin/vimwiki/taskwiki.vim b/ftplugin/vimwiki/taskwiki.vim index ea97996a6..9170bc9cd 100644 --- a/ftplugin/vimwiki/taskwiki.vim +++ b/ftplugin/vimwiki/taskwiki.vim @@ -71,6 +71,8 @@ execute "command! -buffer -nargs=* TaskWikiTags :" . g:taskwiki_py . " Commands that operate on tasks in the buffer execute "command! -buffer -range TaskWikiInfo :," . g:taskwiki_py . "SelectedTasks().info()" +execute "command! -buffer -range TaskWikiOpen :," . g:taskwiki_py . "SelectedTasks().open()" +execute "command! -buffer -range TaskWikiNote :," . g:taskwiki_py . "SelectedTasks().note()" execute "command! -buffer -range TaskWikiEdit :," . g:taskwiki_py . "SelectedTasks().edit()" execute "command! -buffer -range TaskWikiLink :," . g:taskwiki_py . "SelectedTasks().link()" execute "command! -buffer -range TaskWikiGrid :," . g:taskwiki_py . "SelectedTasks().grid()" @@ -125,6 +127,8 @@ if !exists('g:taskwiki_suppress_mappings') nnoremap hm :TaskWikiHistoryMonthly nnoremap ha :TaskWikiHistoryAnnual nnoremap i :TaskWikiInfo + nnoremap o :TaskWikiOpen + nnoremap n :TaskWikiNote nnoremap l :TaskWikiLink nnoremap m :TaskWikiMod nnoremap p :TaskWikiProjects @@ -144,6 +148,8 @@ if !exists('g:taskwiki_suppress_mappings') vnoremap e :TaskWikiEdit vnoremap g :TaskWikiGrid vnoremap i :TaskWikiInfo + vnoremap o :TaskWikiOpen + vnoremap n :TaskWikiNote vnoremap l :TaskWikiLink vnoremap m :TaskWikiMod vnoremap . :TaskWikiRedo diff --git a/taskwiki/main.py b/taskwiki/main.py index fb781ac9f..b47c35857 100644 --- a/taskwiki/main.py +++ b/taskwiki/main.py @@ -6,6 +6,7 @@ import six import sys import vim # pylint: disable=F0401 +import subprocess # Insert the taskwiki on the python path BASE_DIR = vim.eval("s:plugin_path") @@ -83,6 +84,10 @@ def __init__(self): if not self.tasks: print("No tasks selected.") + self.taskopen_notes_folder = ( + util.get_var("taskwiki_taskopen_notes_folder") or "~/tasknotes" + ) + @classmethod def save_action(cls, method, *args): cls.last_action = {'method': method, 'args': args} @@ -123,6 +128,34 @@ def info(self): util.show_in_split(out, name='info', activate_cursorline=True) break # Show only one task + @errors.pretty_exception_handler + def open(self): + compatible_annotation_found = False + for vimwikitask in self.tasks: + for annotation in vimwikitask.task["annotations"]: + annotation = annotation["description"] + proc = subprocess.Popen(["xdg-open", annotation]) + try: + proc.wait(0.3) + except subprocess.TimeoutExpired: + compatible_annotation_found = True + + if not compatible_annotation_found: + print("No compatible annotation found.") + + @errors.pretty_exception_handler + def note(self): + for vimwikitask in self.tasks: + if "Notes" not in [ + a["description"] for a in vimwikitask.task["annotations"] + ]: + self.annotate("Notes") + + note_path = os.path.join(self.taskopen_notes_folder, + vimwikitask.task["uuid"] + ".md") + vim.command("edit " + note_path) + break # Add not to only one task + @errors.pretty_exception_handler def edit(self): for vimwikitask in self.tasks: diff --git a/tests/test_selected.py b/tests/test_selected.py index 7d89ab431..daaab9c8e 100644 --- a/tests/test_selected.py +++ b/tests/test_selected.py @@ -1329,3 +1329,97 @@ def execute(self): self.tasks[0].refresh() assert self.tasks[0]['project'] == "Home" + + +class TestAddTaskNote(IntegrationTest): + + viminput = """ + * [ ] test task 1 #{uuid} + """ + + vimoutput = """ + * [ ] test task 1 #{uuid} + """ + + tasks = [ + dict(description="test task 1"), + ] + + def execute(self): + # Create a note + self.command("let g:taskwiki_taskopen_notes_folder='~'") + self.command( + "TaskWikiNote", + regex='Task "test task 1" annotated', + lines=3 + ) + self.command("w", silent=False) + self.command("bd", silent=False) + + # Re-opening the note should not create a new one + self.command( + "TaskWikiNote", + regex=" 0L, 0C$" + ) + self.command("w", silent=False) + self.command("bd", silent=False) + + # Note should be listed in task annotations + self.command("TaskWikiInfo") + + output = '\n'.join(self.read_buffer()) + + data = r"Annotation of 'Notes' added" + + assert re.search(data, output, re.MULTILINE) + + self.command("bd") + + +class TestTaskOpenWithNoAnnotation(IntegrationTest): + + viminput = """ + * [ ] test task 1 #{uuid} + """ + + vimoutput = """ + * [ ] test task 1 #{uuid} + """ + + tasks = [ + dict(description="test task 1"), + ] + + def execute(self): + self.command( + "TaskWikiOpen", + regex='No compatible annotation found.', + lines=1 + ) + + +class TestTaskOpenWithIncompatibleAnnotation(IntegrationTest): + + viminput = """ + * [ ] test task 1 #{uuid} + """ + + vimoutput = """ + * [ ] test task 1 #{uuid} + """ + + tasks = [ + dict(description="test task 1"), + ] + + def execute(self): + # Create an annotation + self.command( + "TaskWikiAnnotate what is this", + silent=False + ) + self.command( + "TaskWikiOpen", + regex='No compatible annotation found.', + lines=1 + )