1
- #!/usr/bin/env python
1
+ #! /usr/bin/env python3
2
+
3
+ from __future__ import print_function
2
4
3
5
import sys , os , string , re , signal , errno
4
6
@@ -12,7 +14,7 @@ colours = {
12
14
'reverse' : "\033 [7m" ,
13
15
'concealed' : "\033 [8m" ,
14
16
15
- 'black' : "\033 [30m" ,
17
+ 'black' : "\033 [30m" ,
16
18
'red' : "\033 [31m" ,
17
19
'green' : "\033 [32m" ,
18
20
'yellow' : "\033 [33m" ,
@@ -21,7 +23,7 @@ colours = {
21
23
'cyan' : "\033 [36m" ,
22
24
'white' : "\033 [37m" ,
23
25
24
- 'on_black' : "\033 [40m" ,
26
+ 'on_black' : "\033 [40m" ,
25
27
'on_red' : "\033 [41m" ,
26
28
'on_green' : "\033 [42m" ,
27
29
'on_yellow' : "\033 [43m" ,
@@ -39,6 +41,26 @@ colours = {
39
41
'italic' : "\033 [3m" ,
40
42
'rapidblink' : "\033 [6m" ,
41
43
'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" ,
42
64
}
43
65
44
66
@@ -64,12 +86,30 @@ def get_colour(x):
64
86
65
87
home = []
66
88
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
+
70
109
conffile_arg = sys .argv [1 ] # tentative conffile
71
110
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 ):
73
113
conffile = i + conffile_arg
74
114
break
75
115
@@ -95,9 +135,18 @@ while not is_last:
95
135
continue
96
136
if not l [0 ] in letters :
97
137
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
99
146
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" ]:
101
150
raise ValueError ("Invalid keyword" )
102
151
ll [keyword ] = value
103
152
@@ -114,17 +163,14 @@ while not is_last:
114
163
# do not try to understand the optimized form below :-)
115
164
if 'colours' in ll :
116
165
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' ], ',' )]
122
167
)
123
168
ll ['colours' ] = colstrings
124
169
125
170
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 )
128
174
129
175
prevcolour = colours ['default' ]
130
176
prevcount = "more"
@@ -134,7 +180,8 @@ while 1:
134
180
line = freadline ()
135
181
if line == "" :
136
182
break
137
- line = line [:- 1 ]
183
+ if line [- 1 ] in '\r \n ' :
184
+ line = line [:- 1 ]
138
185
clist = []
139
186
skip = 0
140
187
for pattern in regexplist :
@@ -143,6 +190,11 @@ while 1:
143
190
while 1 :
144
191
m = pattern ['regexp' ](line , pos )
145
192
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
146
198
if 'colours' in pattern :
147
199
if currcount == "block" :
148
200
blockflag = 1
@@ -160,10 +212,21 @@ while 1:
160
212
break
161
213
if currcount == "more" :
162
214
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
164
222
else :
165
223
prevcount = "once"
166
224
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
167
230
if 'command' in pattern :
168
231
os .system (pattern ['command' ])
169
232
if 'colours' not in pattern :
@@ -185,7 +248,7 @@ while 1:
185
248
cline = (length_line + 1 )* [colours ['default' ]]
186
249
for i in clist :
187
250
# each position in the string has its own colour
188
- if i [2 ] == "prev" :
251
+ if i [2 ] == "prev" :
189
252
cline [i [0 ]:i [1 ]] = [colours ['default' ]+ prevcolour ]* (i [1 ]- i [0 ])
190
253
elif i [2 ] != "unchanged" :
191
254
cline [i [0 ]:i [1 ]] = [colours ['default' ]+ i [2 ]]* (i [1 ]- i [0 ])
0 commit comments