ViewPort: Add initial implementation

This commit is contained in:
Tomas Babej 2015-03-16 00:39:44 +01:00
parent 4c78eed983
commit 3e5564f596
4 changed files with 106 additions and 0 deletions

View file

@ -2,6 +2,7 @@ import copy
import vim import vim
from task import VimwikiTask from task import VimwikiTask
from viewport import ViewPort
class TaskCache(object): class TaskCache(object):
@ -103,3 +104,12 @@ class TaskCache(object):
for task in tasks: for task in tasks:
self.task_cache[task['uuid']] = task self.task_cache[task['uuid']] = task
def evaluate_viewports(self):
for i in range(len(vim.current.buffer)):
port = ViewPort.from_line(i, self)
if port is None:
continue
port.load_tasks()
port.sync_with_taskwarrior()

View file

@ -43,3 +43,10 @@ PROJECT_DEFINITION = re.compile(r'Project: (?P<project>.*)(?<!\s)')
DATETIME_FORMAT = "(%Y-%m-%d %H:%M)" DATETIME_FORMAT = "(%Y-%m-%d %H:%M)"
DATE_FORMAT = "(%Y-%m-%d)" DATE_FORMAT = "(%Y-%m-%d)"
GENERIC_VIEWPORT = re.compile(
'[=]+' # Heading begging
'(?P<name>[^=\|]*)' # Name of the viewport, all before the | sign
'\|' # Colon
'(?P<filter>[^=\|]*)' # Filter
'[=]+' # Header ending
)

View file

@ -33,6 +33,7 @@ def update_from_tw():
cache.load_buffer() cache.load_buffer()
cache.update_tasks() cache.update_tasks()
cache.update_buffer() cache.update_buffer()
cache.evaluate_viewports()
def update_to_tw(): def update_to_tw():

88
taskwiki/viewport.py Normal file
View file

@ -0,0 +1,88 @@
import vim
from task import VimwikiTask
from regexp import *
class ViewPort(object):
"""
Represents viewport with a given filter.
A ViewPort is a vimwiki heading which contains (albeit usually hidden
by the vim's concealing feature) the definition of TaskWarrior filter.
ViewPort then displays all the tasks that match the given filter below it.
=== Work related tasks | pro:Work ===
* [ ] Talk with the boss
* [ ] Publish a new blogpost
* [ ] Pick a topic
* [ ] Make sure the hosting is working
"""
def __init__(self, line_number, cache, taskfilter):
"""
Constructs a ViewPort out of given line.
"""
self.cache = cache
self.tw = cache.tw
self.line_number = line_number
self.taskfilter = taskfilter
self.tasks = set()
@classmethod
def from_line(cls, number, cache):
match = re.search(GENERIC_VIEWPORT, vim.current.buffer[number])
if not match:
return None
self = cls(number, cache, match.group('filter').strip())
return self
def load_tasks(self):
# Load all tasks below the viewport
for i in range(self.line_number + 1, len(vim.current.buffer)):
line = vim.current.buffer[i]
match = re.search(GENERIC_TASK, line)
if match:
self.tasks.add(self.cache[i].task)
else:
# If we didn't found a valid task, terminate the viewport
break
def sync_with_taskwarrior(self):
# This is called at the point where all the tasks in the vim
# are already synced. This should load the tasks from TW matching
# the filter, and add the tasks that are new. Optionally remove the
# tasks that are not longer belonging there.
# Use uuid filter hack to pass raw filter
matching_tasks = set(
task for task in self.tw.tasks.filter(uuid=self.taskfilter)
)
to_add = matching_tasks - self.tasks
#to_del = self.tasks - matching_tasks
added_tasks = 0
for task in to_add:
added_tasks += 1
added_at = self.line_number + len(self.tasks) + added_tasks
# Add the task object to cache
self.cache[task['uuid']] = task
# Create the VimwikiTask
vimwikitask = VimwikiTask.from_task(self.cache, task)
vimwikitask['line_number'] = added_at
# Save it to cache
self.cache[added_at] = vimwikitask
# Update the buffer
vim.current.buffer.append(str(vimwikitask), added_at)