-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement basic clocking support #302
Open
maksbotan
wants to merge
1
commit into
jceb:master
Choose a base branch
from
maksbotan:add-clocking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
from orgmode.liborgmode.dom_obj import DomObj, DomObjList | ||
from orgmode.liborgmode.orgdate import OrgDateTime, OrgTimeRange | ||
|
||
from orgmode.py3compat.encode_compatibility import * | ||
from orgmode.py3compat.unicode_compatibility import * | ||
|
||
|
||
class ClockLine(DomObj): | ||
def __init__(self, date, orig_start=None, *args, **kwargs): | ||
super(ClockLine, self).__init__(*args, **kwargs) | ||
self._date = date | ||
# Force ranges to render as []--[] even if both are on the same day | ||
self._date.verbose = True | ||
self._orig_start = orig_start | ||
|
||
self._dirty_clockline = False | ||
|
||
def __unicode__(self): | ||
if isinstance(self.date, OrgDateTime): | ||
return u' ' * self.level + u'CLOCK: %s' % self.date | ||
elif isinstance(self.date, OrgTimeRange): | ||
self._date.verbose = True | ||
return u' ' * self.level + u'CLOCK: %s => %s' % (self.date, self.date.str_duration()) | ||
else: | ||
raise TypeError("self.date is %s instead of OrgDateTime/OrgTimeRange, impossible" % self.date) | ||
|
||
def __str__(self): | ||
return u_encode(self.__unicode__()) | ||
|
||
def set_dirty(self): | ||
self._dirty_clockline = True | ||
super(ClockLine, self).set_dirty() | ||
|
||
@property | ||
def is_dirty(self): | ||
return self._dirty_clockline | ||
|
||
@property | ||
def finished(self): | ||
return isinstance(self.date, OrgTimeRange) | ||
|
||
@property | ||
def date(self): | ||
return self._date | ||
|
||
@date.setter | ||
def date(self, new_date): | ||
self._date = new_date | ||
self.set_dirty() | ||
|
||
|
||
class Logbook(DomObjList): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,38 @@ | |
|
||
import vim | ||
|
||
from datetime import * | ||
|
||
from orgmode._vim import echo, echom, echoe, ORGMODE, apply_count, repeat | ||
from orgmode.menu import Submenu, Separator, ActionEntry | ||
from orgmode.menu import Submenu, Separator, ActionEntry, add_cmd_mapping_menu | ||
from orgmode.keybinding import Keybinding, Plug, Command | ||
from orgmode.liborgmode.headings import ClockLine | ||
from orgmode.liborgmode.orgdate import OrgDateTime, OrgTimeRange | ||
|
||
from orgmode.py3compat.encode_compatibility import * | ||
from orgmode.py3compat.py_py3_string import * | ||
from orgmode.py3compat.unicode_compatibility import * | ||
|
||
|
||
def get_total_time(heading): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use generators:
|
||
total = None | ||
for clockline in heading.logbook: | ||
if not clockline.finished: | ||
continue | ||
|
||
if total is None: | ||
total = clockline.date.duration() | ||
else: | ||
total += clockline.date.duration() | ||
|
||
for child in heading.children: | ||
if total is None: | ||
total = get_total_time(child) | ||
else: | ||
total += get_total_time(child) | ||
|
||
return total | ||
|
||
|
||
class LoggingWork(object): | ||
u""" LoggingWork plugin """ | ||
|
@@ -26,17 +53,113 @@ def __init__(self): | |
self.commands = [] | ||
|
||
@classmethod | ||
def action(cls): | ||
u""" Some kind of action | ||
def clock_in(cls): | ||
d = ORGMODE.get_document() | ||
current_heading = d.current_heading() | ||
current_heading.init_logbook() | ||
|
||
:returns: TODO | ||
""" | ||
pass | ||
now = datetime.now() | ||
new_clock_line = ClockLine( | ||
date=OrgDateTime(False, now.year, now.month, now.day, now.hour, now.minute), | ||
level=current_heading.level + 1 | ||
) | ||
current_heading.logbook.append(new_clock_line) | ||
|
||
# + 1 line to skip the heading itself | ||
start = current_heading.start + 1 | ||
if len(current_heading.logbook) == 1: | ||
vim.current.buffer[start:start] = [u' ' * current_heading.level + u':LOGBOOK:'] | ||
|
||
# For ':LOGBOOK:' line | ||
start += 1 | ||
vim.current.buffer[start:start] = [unicode(new_clock_line)] | ||
|
||
if len(current_heading.logbook) == 1: | ||
start += 1 | ||
vim.current.buffer[start:start] = [u' ' * current_heading.level + u':END:'] | ||
|
||
@classmethod | ||
def clock_out(cls): | ||
d = ORGMODE.get_document() | ||
current_heading = d.current_heading() | ||
current_heading.init_logbook() | ||
|
||
if not current_heading.logbook: | ||
return | ||
|
||
for last_entry in current_heading.logbook: | ||
if not last_entry.finished: | ||
break | ||
else: | ||
return | ||
|
||
end_date = datetime.now() | ||
duration = OrgTimeRange(False, last_entry.date, end_date) | ||
last_entry.date = duration | ||
|
||
d.write_clockline(last_entry) | ||
|
||
@classmethod | ||
def clock_update(cls): | ||
d = ORGMODE.get_document() | ||
current_heading = d.current_heading() | ||
current_heading.init_logbook() | ||
|
||
if not current_heading.logbook: | ||
return | ||
|
||
position = vim.current.window.cursor[0] - 1 | ||
# -2 to skip heading itself and :LOGBOOK: | ||
clockline_index = position - current_heading.start - 2 | ||
|
||
if clockline_index < 0 or clockline_index >= len(current_heading.logbook): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return | ||
|
||
current_heading.logbook[clockline_index].set_dirty() | ||
|
||
d.write_clockline(current_heading.logbook[clockline_index]) | ||
|
||
@classmethod | ||
def clock_total(cls): | ||
d = ORGMODE.get_document() | ||
current_heading = d.current_heading() | ||
current_heading.init_logbook() | ||
|
||
total = get_total_time(current_heading) | ||
|
||
if total is not None: | ||
hours, minutes = divmod(total.total_seconds(), 3600) | ||
echo(u'Total time spent in this heading: %d:%d' % (hours, minutes // 60)) | ||
|
||
def register(self): | ||
u""" | ||
Registration of plugin. Key bindings and other initialization should be done. | ||
""" | ||
# an Action menu entry which binds "keybinding" to action ":action" | ||
self.commands.append(Command(u'OrgLoggingRecordDoneTime', u'%s ORGMODE.plugins[u"LoggingWork"].action()' % VIM_PY_CALL)) | ||
self.menu + ActionEntry(u'&Record DONE time', self.commands[-1]) | ||
add_cmd_mapping_menu( | ||
self, | ||
name=u'OrgLoggingClockIn', | ||
function=u'%s ORGMODE.plugins[u"LoggingWork"].clock_in()<CR>' % VIM_PY_CALL, | ||
key_mapping=u'<localleader>ci', | ||
menu_desrc=u'Clock in' | ||
) | ||
add_cmd_mapping_menu( | ||
self, | ||
name=u'OrgLoggingClockOut', | ||
function=u'%s ORGMODE.plugins[u"LoggingWork"].clock_out()<CR>' % VIM_PY_CALL, | ||
key_mapping=u'<localleader>co', | ||
menu_desrc=u'Clock out' | ||
) | ||
add_cmd_mapping_menu( | ||
self, | ||
name=u'OrgLoggingClockUpdate', | ||
function=u'%s ORGMODE.plugins[u"LoggingWork"].clock_update()<CR>' % VIM_PY_CALL, | ||
key_mapping=u'<localleader>cu', | ||
menu_desrc=u'Clock update' | ||
) | ||
add_cmd_mapping_menu( | ||
self, | ||
name=u'OrgLoggingClockTotal', | ||
function=u'%s ORGMODE.plugins[u"LoggingWork"].clock_total()<CR>' % VIM_PY_CALL, | ||
key_mapping=u'<localleader>ct', | ||
menu_desrc=u'Show total clocked time for the current heading' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if line_date:
is enough orif line_date is None: continue
with unindenting the current if block