Created
May 8, 2014 13:17
-
-
Save persquare/3466ce4292d4f1140f51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/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