Skip to content

Instantly share code, notes, and snippets.

@persquare
Created May 8, 2014 13:17
Show Gist options
  • Save persquare/3466ce4292d4f1140f51 to your computer and use it in GitHub Desktop.
Save persquare/3466ce4292d4f1140f51 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import re
# Python print vs sys.stdout.write():
# http://stackoverflow.com/q/3263672
# Regexes
COMPLETE = re.compile(r"^(\s*)(\+ .*)$")
COMMENT_TEMPLATE = r"^%s( (?!\+|-|x).*)$"
COMMENT = None
completed = []
with sys.stdin as f:
for line in f:
line = line.rstrip().decode('utf-8')
if COMMENT:
# Match comment of completed task?
match = re.match(COMMENT, line)
if match:
completed.append(match.group(1))
continue
COMMENT = None
# Match completed task?
match = re.match(COMPLETE, line)
if match:
# Drop the indent when archiving
completed.append(match.group(2))
indent = match.group(1)
COMMENT = re.compile(COMMENT_TEMPLATE % indent)
continue
# Line is not completed nor comment of completed =>
# write it back (sans trailing whitespace)
sys.stdout.write(line.encode('utf-8'))
sys.stdout.write(u'\n')
for line in completed:
sys.stdout.write(line.encode('utf-8'))
sys.stdout.write(u'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment