From 526ee009e8ee319f82b4cc1f7e9059653ee3b8f6 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Tue, 24 Apr 2012 18:44:57 +0200 Subject: [PATCH] Adds support for tag highlighting as proposed by #16 --- CHANGELOG.md | 6 +++++- README.md | 5 +++-- jrnl.py | 27 +++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de3b4277..f56e3615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,15 @@ Changelog ========= +### 0.2.3 + +* Adds tag export +* Adds coloured highlight of tags + ### 0.2.2 * Adds --encrypt and --decrypt to encrypt / descrypt existing journal files * Adds markdown export (kudos to dedan) -* Adds tag export ### 0.2.1 diff --git a/README.md b/README.md index 250f6c2b..82fc0a2e 100644 --- a/README.md +++ b/README.md @@ -107,16 +107,17 @@ It's just a regular `json` file: default_hour: 9, default_minute: 0, timeformat: "%Y-%m-%d %H:%M", + highlight: true } - `journal`: path to your journal file - `editor`: if set, executes this command to launch an external editor for writing your entries, e.g. `vim` or `subl -w` (note the `-w` flag to make sure _jrnl_ waits for Sublime Text to close the file before writing into the journal). - - `encrypt`: if true, encrypts your journal using AES. + - `encrypt`: if `true`, encrypts your journal using AES. - `password`: you may store the password you used to encrypt your journal in plaintext here. This is useful if your journal file lives in an unsecure space (ie. your Dropbox), but the config file itself is more or less safe. - `tagsymbols`: Symbols to be interpreted as tags. (__See note below__) - `default_hour` and `default_minute`: if you supply a date, such as `last thursday`, but no specific time, the entry will be created at this time - `timeformat`: how to format the timestamps in your journal, see the [python docs](http://docs.python.org/library/time.html#time.strftime) for reference - +- `highlight`: if `true` and you have [clint](http://www.nicosphere.net/clint-command-line-library-for-python/) installed, tags will be highlighted in cyan. > __Note on `tagsymbols`:__ Although it seems intuitive to use the `#` character for tags, there's a drawback: on most shells, this is interpreted as a meta-character starting a comment. This means that if you type > diff --git a/jrnl.py b/jrnl.py index 76f4b8ee..5a180aee 100755 --- a/jrnl.py +++ b/jrnl.py @@ -17,6 +17,11 @@ from Crypto.Cipher import AES from Crypto.Random import random, atfork import hashlib import getpass +try: + import clint + CLINT = True +except ImportError: + CLINT = False default_config = { 'journal': os.path.expanduser("~/journal.txt"), @@ -26,7 +31,8 @@ default_config = { 'default_hour': 9, 'default_minute': 0, 'timeformat': "%Y-%m-%d %H:%M", - 'tagsymbols': '@' + 'tagsymbols': '@', + 'highlight': True, } CONFIG_PATH = os.path.expanduser('~/.jrnl_config') @@ -98,6 +104,12 @@ class Journal: self.entries = self.parse(journal_txt) self.sort() + def _colorize(self, string, color='red'): + if CLINT: + return str(clint.textui.colored.ColoredString(color.upper(), string)) + else: + return string + def _decrypt(self, cipher): """Decrypts a cipher string using self.key as the key and the first 16 byte of the cipher as the IV""" if not cipher: @@ -183,7 +195,12 @@ class Journal: def __str__(self): """Prettyprints the journal's entries""" sep = "-"*60+"\n" - return sep.join([str(e) for e in self.entries]) + pp = sep.join([str(e) for e in self.entries]) + if self.config['highlight']: # highlight tags + pp = re.sub(r"([%s]\w+)" % self.config['tagsymbols'], + lambda match: self._colorize(match.group(0), 'cyan'), + pp) + return pp def to_json(self): """Returns a JSON representation of the Journal.""" @@ -317,6 +334,12 @@ def setup(): default_config['encrypt'] = True print("Journal will be encrypted.") print("If you want to, you can store your password in .jrnl_config and will never be bothered about it again.") + + # Use highlighting: + if not CLINT: + print("clint not found. To turn on highlighting, install clint and set highlight to true in your .jrnl_conf.") + default_config['highlight'] = False + open(default_config['journal'], 'a').close() # Touch to make sure it's there # Write config to ~/.jrnl_conf