Adds support for tag highlighting as proposed by #16

This commit is contained in:
Manuel Ebert 2012-04-24 18:44:57 +02:00
parent eab92dbb23
commit 526ee009e8
3 changed files with 33 additions and 5 deletions

View file

@ -1,11 +1,15 @@
Changelog Changelog
========= =========
### 0.2.3
* Adds tag export
* Adds coloured highlight of tags
### 0.2.2 ### 0.2.2
* Adds --encrypt and --decrypt to encrypt / descrypt existing journal files * Adds --encrypt and --decrypt to encrypt / descrypt existing journal files
* Adds markdown export (kudos to dedan) * Adds markdown export (kudos to dedan)
* Adds tag export
### 0.2.1 ### 0.2.1

View file

@ -107,16 +107,17 @@ It's just a regular `json` file:
default_hour: 9, default_hour: 9,
default_minute: 0, default_minute: 0,
timeformat: "%Y-%m-%d %H:%M", timeformat: "%Y-%m-%d %H:%M",
highlight: true
} }
- `journal`: path to your journal file - `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). - `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. - `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__) - `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 - `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 - `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 > __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
> >

27
jrnl.py
View file

@ -17,6 +17,11 @@ from Crypto.Cipher import AES
from Crypto.Random import random, atfork from Crypto.Random import random, atfork
import hashlib import hashlib
import getpass import getpass
try:
import clint
CLINT = True
except ImportError:
CLINT = False
default_config = { default_config = {
'journal': os.path.expanduser("~/journal.txt"), 'journal': os.path.expanduser("~/journal.txt"),
@ -26,7 +31,8 @@ default_config = {
'default_hour': 9, 'default_hour': 9,
'default_minute': 0, 'default_minute': 0,
'timeformat': "%Y-%m-%d %H:%M", 'timeformat': "%Y-%m-%d %H:%M",
'tagsymbols': '@' 'tagsymbols': '@',
'highlight': True,
} }
CONFIG_PATH = os.path.expanduser('~/.jrnl_config') CONFIG_PATH = os.path.expanduser('~/.jrnl_config')
@ -98,6 +104,12 @@ class Journal:
self.entries = self.parse(journal_txt) self.entries = self.parse(journal_txt)
self.sort() 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): 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""" """Decrypts a cipher string using self.key as the key and the first 16 byte of the cipher as the IV"""
if not cipher: if not cipher:
@ -183,7 +195,12 @@ class Journal:
def __str__(self): def __str__(self):
"""Prettyprints the journal's entries""" """Prettyprints the journal's entries"""
sep = "-"*60+"\n" 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): def to_json(self):
"""Returns a JSON representation of the Journal.""" """Returns a JSON representation of the Journal."""
@ -317,6 +334,12 @@ def setup():
default_config['encrypt'] = True default_config['encrypt'] = True
print("Journal will be encrypted.") 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.") 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 open(default_config['journal'], 'a').close() # Touch to make sure it's there
# Write config to ~/.jrnl_conf # Write config to ~/.jrnl_conf