Skip to content

Commit

Permalink
Merge a70548e into 28c3563
Browse files Browse the repository at this point in the history
  • Loading branch information
icemac committed Nov 30, 2023
2 parents 28c3563 + a70548e commit 265d33b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Changelog
- Grouped task entries can now be sorted by start date, name, duration or
according to tasks.txt order (GH: #228).

- Add the ability to change the last entry using Primary+BackSpace (GH: #247).


0.11.3 (2019-04-23)
~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions src/gtimelog/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ In alphabetic order:
- Laurynas Speičys
- Martin Pitt
- Michael Vogt
- Michael Howitz
- Nathan Pratta Teodosio
- Olivier Crête
- Patrick Gerken
Expand Down
2 changes: 1 addition & 1 deletion src/gtimelog/about.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<property name="modal">True</property>
<property name="program_name" translatable="yes">Time Log</property>
<property name="version">(placeholder)</property>
<property name="copyright" translatable="yes">Copyright © 2004–2020 Marius Gedminas and contributors.</property>
<property name="copyright" translatable="yes">Copyright © 2004–2023 Marius Gedminas and contributors.</property>
<property name="comments" translatable="yes">A time tracking application</property>
<property name="website">https://gtimelog.org/</property>
<property name="authors">Marius Gedminas
Expand Down
10 changes: 10 additions & 0 deletions src/gtimelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class Actions(object):
'about',
'quit',
'edit-log',
'edit-last-item',
'edit-tasks',
'refresh-tasks',
]
Expand Down Expand Up @@ -294,6 +295,7 @@ def do_startup(self):
self.set_accels_for_action("win.go-home", ["<Alt>Home"])
self.set_accels_for_action("win.focus-task-entry", ["<Primary>L"])
self.set_accels_for_action("app.edit-log", ["<Primary>E"])
self.set_accels_for_action("app.edit-last-item", ["<Primary>BackSpace"])
self.set_accels_for_action("app.edit-tasks", ["<Primary>T"])
self.set_accels_for_action("app.shortcuts", ["<Primary>question"])
self.set_accels_for_action("app.preferences", ["<Primary>P"])
Expand All @@ -319,6 +321,14 @@ def on_edit_log(self, action, parameter):
filename = Settings().get_timelog_file()
self.open_in_editor(filename)

def on_edit_last_item(self, action, parameter):
window = self.get_active_window()
text = window.timelog.remove_last_entry()
if text is not None:
window.task_entry.set_text(text)
window.task_entry.grab_focus()
window.task_entry.select_region(-1, -1)

def on_edit_tasks(self, action, parameter):
gsettings = Gio.Settings.new("org.gtimelog")
if gsettings.get_boolean('remote-task-list'):
Expand Down
4 changes: 4 additions & 0 deletions src/gtimelog/menus.ui
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<attribute name="label" translatable="yes">Edit log</attribute>
<attribute name="action">app.edit-log</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Edit last item</attribute>
<attribute name="action">app.edit-last-item</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Edit tasks</attribute>
<attribute name="action">app.edit-tasks</attribute>
Expand Down
7 changes: 7 additions & 0 deletions src/gtimelog/shortcuts.ui
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@
<property name="title" translatable="yes" context="shortcut window">Focus the task entry</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="accelerator">&lt;primary&gt;BackSpace</property>
<property name="title" translatable="yes" context="shortcut window">Edit last task entry</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
Expand Down
57 changes: 57 additions & 0 deletions src/gtimelog/tests/test_timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,63 @@ def test_append_rounds_the_time(self):
timelog.append('now')
self.assertEqual(timelog.items[-1][0], datetime.datetime(2015, 5, 12, 16, 27))

@freezegun.freeze_time("2018-12-09 16:27")
def test_remove_last_entry(self):

TEST_TIMELOG = textwrap.dedent("""
2018-12-09 08:30: start at home
2018-12-09 08:40: emails
2018-12-09 12:15: coding
""")

filename = self.tempfile()
self.write_file(filename, TEST_TIMELOG)
timelog = TimeLog(filename, datetime.time(2, 0))
timelog.reread()
last_entry = timelog.remove_last_entry()
self.assertEqual(last_entry, 'coding')
items_after_call = [
(datetime.datetime(2018, 12, 9, 8, 30), 'start at home'),
(datetime.datetime(2018, 12, 9, 8, 40), 'emails')]
self.assertEqual(timelog.items, items_after_call)
self.assertEqual(timelog.window.items, items_after_call)
with open(filename) as f:
self.assertEqual(f.read(), textwrap.dedent("""
2018-12-09 08:30: start at home
2018-12-09 08:40: emails
"""))

@freezegun.freeze_time("2018-12-10 10:40")
def test_remove_last_entry_start_of_day(self):

TEST_TIMELOG = textwrap.dedent("""
2018-12-09 08:30: start at home
2018-12-09 08:40: emails
2018-12-10 08:30: start at home
""")

filename = self.tempfile()
self.write_file(filename, TEST_TIMELOG)
timelog = TimeLog(filename, datetime.time(2, 0))
timelog.reread()
last_entry = timelog.remove_last_entry()
self.assertEqual(last_entry, 'start at home')
items_after_call = [
(datetime.datetime(2018, 12, 9, 8, 30), 'start at home'),
(datetime.datetime(2018, 12, 9, 8, 40), 'emails')]
self.assertEqual(timelog.items, items_after_call)
self.assertEqual(timelog.window.items, [])
with open(filename) as f:
self.assertEqual(f.read(), textwrap.dedent("""
2018-12-09 08:30: start at home
2018-12-09 08:40: emails
"""))

# no further remove possible at beginning of the day:
last_entry = timelog.remove_last_entry()
self.assertIsNone(last_entry)

@freezegun.freeze_time("2015-05-12 16:27")
def test_valid_time_accepts_any_time_in_the_past_when_log_is_empty(self):
timelog = TimeLog(StringIO(), datetime.time(2, 0))
Expand Down
18 changes: 18 additions & 0 deletions src/gtimelog/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,24 @@ def raw_append(self, line, need_space):
f.write(line + '\n')
self.last_mtime = get_mtime(self.filename)

def remove_last_entry(self):
self.check_reload()
if self.window.items:
self.window.items.pop()
else:
# entries list is empty, so nothing to remove
return None
_, last_entry = self.items.pop()
with open(self.filename, "r", encoding='utf-8') as f:
lines = f.readlines()
lines = lines[:-1]
if not lines[-1].strip():
# remove line which divides days if necessary
lines = lines[:-1]
with open(self.filename, "w", encoding='utf-8') as f:
f.write(''.join(lines))
return last_entry

def append(self, entry, now=None):
"""Append a new entry to the time log."""
if not now:
Expand Down

0 comments on commit 265d33b

Please sign in to comment.