Skip to content

Commit 7dea8f4

Browse files
Update plugin
1 parent e4ec7e7 commit 7dea8f4

File tree

3 files changed

+86
-24
lines changed

3 files changed

+86
-24
lines changed

grcat

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/usr/bin/env python
1+
#! /usr/bin/env python3
2+
3+
from __future__ import print_function
24

35
import sys, os, string, re, signal, errno
46

@@ -12,7 +14,7 @@ colours = {
1214
'reverse' : "\033[7m",
1315
'concealed' : "\033[8m",
1416

15-
'black' : "\033[30m",
17+
'black' : "\033[30m",
1618
'red' : "\033[31m",
1719
'green' : "\033[32m",
1820
'yellow' : "\033[33m",
@@ -21,7 +23,7 @@ colours = {
2123
'cyan' : "\033[36m",
2224
'white' : "\033[37m",
2325

24-
'on_black' : "\033[40m",
26+
'on_black' : "\033[40m",
2527
'on_red' : "\033[41m",
2628
'on_green' : "\033[42m",
2729
'on_yellow' : "\033[43m",
@@ -39,6 +41,26 @@ colours = {
3941
'italic' : "\033[3m",
4042
'rapidblink' : "\033[6m",
4143
'strikethrough': "\033[9m",
44+
45+
# aixterm bright color codes
46+
# prefixed with standard ANSI codes for graceful failure
47+
'bright_black' : "\033[30;90m",
48+
'bright_red' : "\033[31;91m",
49+
'bright_green' : "\033[32;92m",
50+
'bright_yellow' : "\033[33;93m",
51+
'bright_blue' : "\033[34;94m",
52+
'bright_magenta' : "\033[35;95m",
53+
'bright_cyan' : "\033[36;96m",
54+
'bright_white' : "\033[37;97m",
55+
56+
'on_bright_black' : "\033[40;100m",
57+
'on_bright_red' : "\033[41;101m",
58+
'on_bright_green' : "\033[42;102m",
59+
'on_bright_yellow' : "\033[43;103m",
60+
'on_bright_blue' : "\033[44;104m",
61+
'on_bright_magenta' : "\033[45;105m",
62+
'on_bright_cyan' : "\033[46;106m",
63+
'on_bright_white' : "\033[47;107m",
4264
}
4365

4466

@@ -64,12 +86,30 @@ def get_colour(x):
6486

6587
home = []
6688
conffile = None
67-
if 'HOME' in os.environ:
68-
home = [os.environ['HOME']+"/.grc/"]
69-
conffilepath = [""] + home + ["/usr/local/share/grc/", "/usr/share/grc/"]
89+
xdg_config = os.environ.get('XDG_CONFIG_HOME')
90+
xdg_data = os.environ.get('XDG_DATA_HOME')
91+
home = os.environ.get('HOME')
92+
if home and not xdg_config:
93+
xdg_config = home + '/.config'
94+
if home and not xdg_data:
95+
xdg_data = home + '/.local/share'
96+
97+
conffilepath = [""]
98+
if xdg_data:
99+
conffilepath += [xdg_data + '/grc/']
100+
if xdg_config:
101+
conffilepath += [xdg_config + '/grc/']
102+
if home:
103+
conffilepath += [home + '/.grc/']
104+
conffilepath += ['/usr/local/share/grc/', '/usr/share/grc/']
105+
if len(sys.argv) != 2:
106+
sys.stderr.write("You are not supposed to call grcat directly, but the usage is: grcat conffile\n")
107+
sys.exit(1)
108+
70109
conffile_arg = sys.argv[1] # tentative conffile
71110
for i in conffilepath:
72-
if os.path.isfile(i+conffile_arg):
111+
# test if conffile exists, it can be also a pipe
112+
if os.path.exists(i+conffile_arg) and not os.path.isdir(i+conffile_arg):
73113
conffile = i+conffile_arg
74114
break
75115

@@ -95,9 +135,18 @@ while not is_last:
95135
continue
96136
if not l[0] in letters:
97137
break
98-
keyword, value = split(l[:-1], "=", 1)
138+
fields = split(l.rstrip('\r\n'), "=", 1)
139+
if len(fields) != 2:
140+
sys.stderr.write('Error in configuration, I expect keyword=value line\n')
141+
sys.stderr.write('But I got instead:\n')
142+
sys.stderr.write(repr(l))
143+
sys.stderr.write('\n')
144+
sys.exit(1)
145+
keyword, value = fields
99146
keyword = lower(keyword)
100-
if not keyword in ["regexp", "colours", "count", "command", "skip"]:
147+
if keyword in ('colors', 'colour', 'color'):
148+
keyword = 'colours'
149+
if not keyword in ["regexp", "colours", "count", "command", "skip", "replace", "concat"]:
101150
raise ValueError("Invalid keyword")
102151
ll[keyword] = value
103152

@@ -114,17 +163,14 @@ while not is_last:
114163
# do not try to understand the optimized form below :-)
115164
if 'colours' in ll:
116165
colstrings = list(
117-
map(
118-
lambda colgroup:
119-
''.join(map(lambda x: get_colour(x), split(colgroup))),
120-
split(ll['colours'], ',')
121-
)
166+
[''.join([get_colour(x) for x in split(colgroup)]) for colgroup in split(ll['colours'], ',')]
122167
)
123168
ll['colours'] = colstrings
124169

125170
cs = ll['count']
126-
ll['regexp'] = re.compile(ll['regexp']).search
127-
regexplist.append(ll)
171+
if 'regexp' in ll:
172+
ll['regexp'] = re.compile(ll['regexp']).search
173+
regexplist.append(ll)
128174

129175
prevcolour = colours['default']
130176
prevcount = "more"
@@ -134,7 +180,8 @@ while 1:
134180
line = freadline()
135181
if line == "" :
136182
break
137-
line = line[:-1]
183+
if line[-1] in '\r\n':
184+
line = line[:-1]
138185
clist = []
139186
skip = 0
140187
for pattern in regexplist:
@@ -143,6 +190,11 @@ while 1:
143190
while 1:
144191
m = pattern['regexp'](line, pos)
145192
if m:
193+
if 'replace' in pattern:
194+
line = re.sub(m.re, pattern['replace'], line)
195+
#m = pattern['regexp'](line, pos)
196+
#if not m:
197+
# break
146198
if 'colours' in pattern:
147199
if currcount == "block":
148200
blockflag = 1
@@ -160,10 +212,21 @@ while 1:
160212
break
161213
if currcount == "more":
162214
prevcount = "more"
163-
pos = m.end(0)
215+
newpos = m.end(0)
216+
# special case, if the regexp matched but did not consume anything,
217+
# advance the position by 1 to escape endless loop
218+
if newpos == pos:
219+
pos += 1
220+
else:
221+
pos = newpos
164222
else:
165223
prevcount = "once"
166224
pos = len(line)
225+
if 'concat' in pattern:
226+
with open(pattern['concat'], 'a') as f :
227+
f.write(line + '\n')
228+
if 'colours' not in pattern:
229+
break
167230
if 'command' in pattern:
168231
os.system(pattern['command'])
169232
if 'colours' not in pattern:
@@ -185,7 +248,7 @@ while 1:
185248
cline = (length_line+1)*[colours['default']]
186249
for i in clist:
187250
# each position in the string has its own colour
188-
if i[2] == "prev":
251+
if i[2] == "prev":
189252
cline[i[0]:i[1]] = [colours['default']+prevcolour]*(i[1]-i[0])
190253
elif i[2] != "unchanged":
191254
cline[i[0]:i[1]] = [colours['default']+i[2]]*(i[1]-i[0])

mycat

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env bash
22

33
if [[ $OSTYPE == "linux-gnu" ]]; then
4-
sed -f "$( dirname "${BASH_SOURCE[0]}" )"/mycat.sed | "$( dirname "${BASH_SOURCE[0]}" )"/grcat "$( dirname "${BASH_SOURCE[0]}" )"/mysql.grcat | less -S -R -M;
4+
_SED=sed
55
else
6-
if [[ $OSTYPE == "freebsd"* || $OSTYPE == "darwin"* ]]; then
7-
gsed -f "$( dirname "${BASH_SOURCE[0]}" )"/mycat.sed | "$( dirname "${BASH_SOURCE[0]}" )"/grcat "$( dirname "${BASH_SOURCE[0]}" )"/mysql.grcat | less -S -R -M;
8-
fi
6+
_SED=gsed
97
fi
108

11-
echo "$( dirname "${BASH_SOURCE[0]}" )"
9+
$_SED -f "$( dirname "${BASH_SOURCE[0]}" )"/mycat.sed | "$( dirname "${BASH_SOURCE[0]}" )"/grcat "$( dirname "${BASH_SOURCE[0]}" )"/mysql.grcat | less -S -R -M;
10+

mysql-colorize.plugin.zsh.zwc

472 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)