implement feature "Support custom colours" (#197)

* implement "Support custom colours" from (#174)

# imported collections module for named tuple
# add "colormap" from googler project
# add named tuple for id and title, url, desc, tag
# adjusted print_single_rec so colors can be changed for each item
# add valid color string checker from googler
# adjusted man options
# add autocompletions

* Hotfix - implement "Support custom colours" from (#174)
# sorted import in alphabetical order
# adjusted var name's
# add colors table to man file
# add color os ENV option
# add --color documentation in README under "Usage"

* fix typo error in man page color section

* Update buku-completion.bash

--color reordered in alphabetical order

* Update buku.fish

--color reordered in alphabetical order

* Update _buku

--color reordered in alphabetical order

* Hotfix2 - implement "Support custom colours" - restructure feature
# separated id from ID_str and ID_DB_STR so it can have separate color
# add 5 string color for (id, title, url, desc, tag)
# adjusted print_single_rec for 5 colors result
# fixed syntax .BI \--colors " COLORS" in man page
# replaced four with five letter string for color
# fixed default colors to "GKlxe" and "\x1b[2m" for DB index
# add example to README and man page
# removed check for colorstr_env set by user for the moment

* Fixed reviews

* Review fixes
This commit is contained in:
shv-q3 2017-08-23 00:09:43 +02:00 committed by Arun Prakash Jana
parent cc2d932407
commit 9dcbd67ac5
6 changed files with 175 additions and 16 deletions

View File

@ -52,7 +52,7 @@ PRs are welcome. Please visit [#174](https://github.com/jarun/Buku/issues/174) f
### Features
- Lightweight, clean interface
- Lightweight, clean interface, custom colors
- Text editor integration
- Fetch, edit page title; add tags and notes
- Powerful search modes (regex, substring...)
@ -206,6 +206,7 @@ POWER TOYS:
N=1: URL, N=2: URL and tag, N=3: title,
N=4: URL, title and tag
-j, --json Json formatted output for -p and search
--colors --colors COLORS set output colors
--nc disable color output
--np do not show the prompt, run and exit
-o, --open [...] browse bookmarks by indices and ranges
@ -390,7 +391,10 @@ PROMPT KEYS:
buku (? for help) g > 5 3-2
// remove tags at taglist indices 4 and 6-9 from tags in bookmarks at indices 5 and 2-3
buku (? for help) g 4 9-6 << 5 3-2
37. More **help**:
37. List bookmarks with **colored output**:
$ buku --colors GKlxe -p
38. More **help**:
$ buku -h
$ man buku

View File

@ -13,7 +13,9 @@ _buku () {
opts=(
-a --add
--ai
--colors
-c --comment
--colors
-d --delete
--deep
-e --export
@ -51,6 +53,7 @@ _buku () {
)
opts_with_arg=(
-a --add
--colors
-e --export
--expand
-f --format

View File

@ -6,6 +6,7 @@
#
complete -c buku -s a -l add -r --description 'add bookmark'
complete -c buku -l ai --description 'auto-import bookmarks'
complete -c buku -l colors -r --description 'set output colors'
complete -c buku -s c -l comment --description 'comment on bookmark'
complete -c buku -s d -l delete --description 'delete bookmark'
complete -c buku -l deep --description 'search matching substrings'

View File

@ -11,6 +11,7 @@ local -a args
args=(
'(-a --add)'{-a,--add}'[add bookmark]:URL tags'
'(--ai)--ai[auto-import bookmarks]'
'(--colors)--colors[set output colors]:color string'
'(-c --comment)'{-c,--comment}'[comment on bookmark]'
'(-d --delete)'{-d,--delete}'[delete bookmark]'
'(--deep)--deep[search matching substrings]'

88
buku.1
View File

@ -9,7 +9,7 @@ is a command-line utility to store, tag, search and organize bookmarks.
.PP
.B Features
.PP
* Lightweight, clean interface
* Lightweight, clean interface, custom colors
* Text editor integration
* Fetch, edit page title; add tags and notes
* Powerful search modes (regex, substring...)
@ -218,6 +218,9 @@ Show selective monochrome output with specific fields. Works with --print. Searc
.BI \-j " " \--json
Output data formatted as json, works with --print output and search results.
.TP
.BI \--colors " COLORS"
Set output colors. Refer to the \fBCOLORS\fR section below for details.
.TP
.BI \--nc
Disable color output in all messages. Useful on terminals which can't handle ANSI color codes or scripted environments.
.TP
@ -336,6 +339,79 @@ http[s]://[username:password@]proxyhost:proxyport/
can be integrated in a GUI environment with simple tweaks. Refer to:
.br
.I https://github.com/jarun/Buku#gui-integration
.SH COLORS
\fBbuku\fR allows you to customize the color scheme via a five-letter string, reminiscent of BSD \fBLSCOLORS\fR. The five letters represent the colors of
.IP - 2
bookmark's id
.PD 0 \" Change paragraph spacing to 0 in the list
.IP - 2
bookmark's title
.IP - 2
bookmark's url
.IP - 2
bookmark's description
.IP - 2
bookmark's tag
.PD 1 \" Restore paragraph spacing
.TP
respectively. The five-letter string is passed is as the argument to the \fB--colors\fR option.
.TP
We offer the following colors/styles:
.TS
tab(;) box;
l|l
-|-
l|l.
Letter;Color/Style
a;black
b;red
c;green
d;yellow
e;blue
f;magenta
g;cyan
h;white
i;bright black
j;bright red
k;bright green
l;bright yellow
m;bright blue
n;bright magenta
o;bright cyan
p;bright white
A-H;bold version of the lowercase-letter color
I-P;bold version of the lowercase-letter bright color
x;normal
X;bold
y;reverse video
Y;bold reverse video
.TE
.TP
.TP
The default colors string is \fIGKlxe\fR, which stands for
.IP - 2
bold cyan index
.PD 0 \" Change paragraph spacing to 0 in the list
.IP - 2
bold bright green title
.IP - 2
bright yellow url
.IP - 2
comments/notes/desc - normal
.IP - 2
blue tag
.PD 1 \" Restore paragraph spacing
.TP
Note that
.IP - 2
Bright colors (implemented as \\x1b[90m - \\x1b[97m) may not be available in all color-capable terminal emulators;
.IP - 2
Some terminal emulators draw bold text in bright colors instead;
.IP - 2
Some terminal emulators only distinguish between bold and bright colors via a default-off switch.
.TP
Please consult the manual of your terminal emulator as well as \fIhttps://en.wikipedia.org/wiki/ANSI_escape_code\fR for details.
.SH EXAMPLES
.PP
.IP 1. 4
@ -687,6 +763,16 @@ List bookmarks with \fBimmutable title\fR:
.br
.B buku (? for help) g 4 9-6 << 5 3-2
.EE
.PP
.IP 37. 4
List bookmarks with \fBcolored output\fR:
.PP
.EX
.IP
.B $ buku --colors GKlxe -p
.EE
.PP
.SH AUTHOR
Arun Prakash Jana <engineerarun@gmail.com>
.SH HOME

90
buku.py
View File

@ -18,6 +18,7 @@
# along with Buku. If not, see <http://www.gnu.org/licenses/>.
import argparse
import collections
import html.parser as HTMLParser
import json
import logging
@ -48,12 +49,27 @@ SKIP_MIMES = {'.pdf', '.txt'}
colorize = True # Allow color output by default
# Default colour to print records
ID_str = '\x1b[96;1m%d. \x1b[1;92m%s\x1b[0;2m [%s]\x1b[0m\n'
ID_DB_str = '\x1b[96;1m%d. \x1b[1;92m%s\x1b[0m'
ID_srch = '\x1b[36m%d. \x1b[0m'
ID_str = ID_srch + '\x1b[1;92m%s\x1b[0;2m [%s]\x1b[0m\n'
ID_dbidx = '\x1b[36m%d. \x1b[0m'
ID_DB_str = ID_dbidx + '\x1b[1;92m%s\x1b[0m'
MUTE_str = '%s \x1b[2m(L)\x1b[0m\n'
URL_str = '%s \x1b[91m>\x1b[0m \x1b[2m%s\x1b[0m\n'
URL_str = '%s \x1b[91m>\x1b[93m \x1b[2m%s\x1b[0m\n'
DESC_str = '%s \x1b[91m+\x1b[0m %s\n'
TAG_str = '%s \x1b[91m#\x1b[0m %s\n'
TAG_str = '%s \x1b[91m#\x1b[34m %s\n'
# colormap for color output from "googler" project
COLORMAP = {k: '\x1b[%sm' % v for k, v in {
'a': '30', 'b': '31', 'c': '32', 'd': '33',
'e': '34', 'f': '35', 'g': '36', 'h': '37',
'i': '90', 'j': '91', 'k': '92', 'l': '93',
'm': '94', 'n': '95', 'o': '96', 'p': '97',
'A': '30;1', 'B': '31;1', 'C': '32;1', 'D': '33;1',
'E': '34;1', 'F': '35;1', 'G': '36;1', 'H': '37;1',
'I': '90;1', 'J': '91;1', 'K': '92;1', 'L': '93;1',
'M': '94;1', 'N': '95;1', 'O': '96;1', 'P': '97;1',
'x': '0', 'X': '1', 'y': '7', 'Y': '7;1',
}.items()}
# Disguise as Firefox on Ubuntu
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0'
@ -2068,6 +2084,17 @@ PROMPT KEYS:
''')
@staticmethod
def is_colorstr(arg):
"""Check if a string is a valid color string."""
try:
assert len(arg) == 5
for c in arg:
assert c in COLORMAP
except AssertionError:
raise argparse.ArgumentTypeError('%s is not a valid color string' % arg)
return arg
# Help
def print_help(self, file=sys.stdout):
super(ExtendedArgumentParser, self).print_help(file)
@ -2644,28 +2671,32 @@ def print_single_rec(row, idx=0): # NOQA
# Start with index and title
if idx != 0:
pr = ID_str % (idx, row[2] if row[2] else 'Untitled', row[0])
ID_srch = idx
id_title_res = ID_str % (ID_srch, row[2] if row[2] else 'Untitled', row[0])
else:
pr = ID_DB_str % (row[0], row[2] if row[2] else 'Untitled')
ID_dbidx = row[0]
id_title_res = ID_DB_str % (ID_dbidx, row[2] if row[2] else 'Untitled')
# Indicate if record is immutable
if row[5] & 1:
pr = MUTE_str % (pr)
id_title_res = MUTE_str % (id_title_res)
else:
pr += '\n'
id_title_res += '\n'
# Append URL
pr = URL_str % (pr, row[1])
url_res = ''
url_res = URL_str % (url_res, row[1])
# Append description
desc_res = ''
if row[4]:
pr = DESC_str % (pr, row[4])
desc_res = DESC_str % (desc_res, row[4])
# Append tags IF not default (delimiter)
tag_res = ''
if row[3] != DELIM:
pr = TAG_str % (pr, row[3][1:-1])
print(pr)
tag_res = TAG_str % (tag_res, row[3][1:-1])
print(id_title_res + url_res + desc_res + tag_res)
def format_json(resultset, single_record=False, field_filter=0):
'''Return results in Json format
@ -3044,6 +3075,18 @@ def piped_input(argv, pipeargs=None):
pipeargs += s.split()
# get colors from user input and separete into "result" list for use in args.colors
def setcolors(args):
Colors = collections.namedtuple('Colors', ' ID_srch, ID_str, URL_str, DESC_str, TAG_str')
colors = Colors(*[COLORMAP[c] for c in args])
id_col = colors.ID_srch
id_str_col = colors.ID_str
url_col = colors.URL_str
desc_col = colors.DESC_str
tag_col = colors.TAG_str
result = [id_col, id_str_col, url_col, desc_col, tag_col]
return result
# main starts here
def main():
global colorize, ID_str, ID_DB_str, MUTE_str, URL_str, DESC_str, TAG_str
@ -3193,6 +3236,7 @@ POSITIONAL ARGUMENTS:
N=1: URL, N=2: URL and tag, N=3: title,
N=4: URL, title and tag
-j, --json Json formatted output for -p and search
--colors COLORS set output colors
--nc disable color output
--np do not show the prompt, run and exit
-o, --open [...] browse bookmarks by indices and ranges
@ -3216,6 +3260,7 @@ POSITIONAL ARGUMENTS:
addarg('-p', '--print', nargs='*', help=HIDE)
addarg('-f', '--format', type=int, default=0, choices={1, 2, 3, 4}, help=HIDE)
addarg('-j', '--json', action='store_true', help=HIDE)
addarg('--colors', dest='colorstr', type=argparser.is_colorstr, metavar='COLORS', help=HIDE)
addarg('--nc', action='store_true', help=HIDE)
addarg('--np', action='store_true', help=HIDE)
addarg('-o', '--open', nargs='*', help=HIDE)
@ -3259,6 +3304,25 @@ POSITIONAL ARGUMENTS:
# Enable color in logs
setup_logger(logger)
# Set colors
if args.colorstr:
ID_srch = '%d.'
ID_str = '%s [%s]\n'
ID_dbidx = '%d.'
ID_DB_str = '%s'
MUTE_str = '%s (L)\n'
URL_str = '%s > %s\n'
DESC_str = '%s + %s\n'
TAG_str = '%s # %s\n'
ID_srch = setcolors(args.colorstr)[0] + ID_srch
ID_str = ID_srch + COLORMAP['x'] + setcolors(args.colorstr)[1] + ID_str
ID_DB_str = ID_dbidx + COLORMAP['x'] + setcolors(args.colorstr)[1] + ID_DB_str
MUTE_str = setcolors(args.colorstr)[3] + MUTE_str
URL_str = setcolors(args.colorstr)[2] + URL_str
DESC_str = setcolors(args.colorstr)[3] + DESC_str
TAG_str = setcolors(args.colorstr)[4] + TAG_str
# Set up debugging
if args.debug:
logger.setLevel(logging.DEBUG)