summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Étienne Deparis 2020-01-28 13:56:52 +0100
committerGravatar Étienne Deparis 2020-01-28 13:56:52 +0100
commit6e97e651b647dda05acea45847bf52785e2e6244 (patch)
tree52d8c4d1e7641e42321de3d252d946a9d3c41c28
parent52f6cbe8230713495b982988131a5e2d98ee86f0 (diff)
downloadeshelf-6e97e651b647dda05acea45847bf52785e2e6244.tar.gz
eshelf-6e97e651b647dda05acea45847bf52785e2e6244.tar.xz
eshelf-6e97e651b647dda05acea45847bf52785e2e6244.zip
Add color to file listing outputs
-rw-r--r--eshelf/cli.py27
-rw-r--r--eshelf/utils.py25
2 files changed, 45 insertions, 7 deletions
diff --git a/eshelf/cli.py b/eshelf/cli.py
index 7c83e84..0f632eb 100644
--- a/eshelf/cli.py
+++ b/eshelf/cli.py
@@ -4,9 +4,10 @@ import sys
import argparse
from configparser import ConfigParser
from eshelf.wizard import setup_local_library
-from eshelf.reader_orm import ReaderOrm
-from eshelf.utils import print_file_tree, print_listing, read_file
from eshelf.bookeen import identify
+from eshelf.reader_orm import ReaderOrm
+from eshelf.utils import print_file_tree, print_listing, read_file, \
+ Logger
class EShelfCli:
@@ -24,6 +25,8 @@ class EShelfCli:
default=[])
parser.add_argument('--path', '-p', default=os.getcwd(),
help='Path to your library.')
+ parser.add_argument('--no-color', action='store_true',
+ help='Remove colors from output.')
args = parser.parse_args()
if args.command == 'help':
@@ -34,6 +37,8 @@ class EShelfCli:
setup_local_library(args.path)
self.exit()
+ self.logger = Logger(not args.no_color)
+
command = 'cmd_{}'.format(args.command)
if command not in dir(EShelfCli):
parser.print_help()
@@ -84,8 +89,12 @@ class EShelfCli:
for data in self.reader_db.list_shelves():
finaldata.append(
'[{id}] {name}'.format(
- id=data['id'],
- name=data['name']
+ id=self.logger.hilite(data['id'], 'yellow'),
+ name=self.logger.hilite(
+ data['name'],
+ color='cyan',
+ bold=True
+ )
)
)
print_listing('Collections:', finaldata)
@@ -139,9 +148,13 @@ class EShelfCli:
if os.path.isfile(mounted_file_path):
finaldata.append(
'[{id}] {title} — {author}{other}'.format(
- id=book_data['id'],
- title=book_data['title'],
- author=book_data['author'],
+ id=self.logger.hilite(book_data['id'], 'yellow'),
+ title=self.logger.hilite(
+ book_data['title'],
+ color='cyan',
+ bold=True
+ ),
+ author=self.logger.hilite(book_data['author'], 'blue'),
other=local_file
)
)
diff --git a/eshelf/utils.py b/eshelf/utils.py
index b348443..7061cbd 100644
--- a/eshelf/utils.py
+++ b/eshelf/utils.py
@@ -51,3 +51,28 @@ def read_file(f):
with open(f, 'r') as s:
return s.read().strip()
return ''
+
+
+class Logger:
+ def __init__(self, use_color=True):
+ self.use_color = use_color
+
+ def hilite(self, string, color=None, bold=False, underline=False):
+ if not self.use_color:
+ return string
+ attr = []
+ color_map = {
+ 'red': '31',
+ 'green': '32',
+ 'yellow': '33',
+ 'blue': '34',
+ 'magenta': '35',
+ 'cyan': '36'
+ }
+ if color in color_map:
+ attr.append(color_map[color])
+ if bold:
+ attr.append('1')
+ if underline:
+ attr.append('4')
+ return '\x1b[{}m{}\x1b[0m'.format(';'.join(attr), string)