-
Notifications
You must be signed in to change notification settings - Fork 0
/
showaliases
executable file
·123 lines (99 loc) · 3.53 KB
/
showaliases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################################################
#
# NAME: showaliases
#
# DESCRIPTION: Print a two column list of all shell aliases that exist in
# all the files that are passed as parameters or from stdin
# if no file is specified.
#
# The alias name is coloured so that it stands out.
#
# Note that for an alias to be list, the word alias must be the
# first in the line. If an alias is defined inside a conditional
# block, it will listed regardless of the value of the
# condition.
#
# USAGE: showaliases [FILE [FILE ...] | - ]
#
# Since it can also read its input from stdin, it can be run as
# filter for the output of the alias command (at least in bash):
# $ alias | showaliases
#
#
# DEPENDENCIES: None
#
##############################################################################
#
__description__ = ''
# ---------------------------------------------------------------------------
# Standard imports:
import argparse
import fileinput
import os
import re
import sys
from functools import reduce
# Third party imports
# Local imports
# ---------------------------------------------------------------------------
ANSI_BLUE = "\x1b[0;36m"
ANSI_RESET = "\x1b[0m"
def ansify(string, colour):
"""
Paints a string usgint eh given ANSI colour code.
"""
return "%s%s%s" % (colour, string, ANSI_RESET)
def printalias(alist):
"""
Print a list of alias tuples in two left aligned columns.
The first column contains the alias name in blue colour and the second
column has the definition.
"""
# Find the largest alias name length to align all definitions
colsize = reduce(max, [len(x[0]) for x in alist])
for alias in alist:
fmt = "%%%ds = %%s" % (colsize+12)
print(fmt % (ansify(alias[0], ANSI_BLUE), alias[1]))
def get_aliases(files, args):
"""
For each line of all the input files, find those that define aliases and
return a list where each element is a tuple with the alias name and its
value.
"""
if args.without_alias:
regex = r'^\s*(\S+?)\s*=\s*(.*)$'
else:
regex = r'^\s*alias\s+(\S+)\s*=\s*(.*)$'
line_matcher = re.compile(regex)
aliases = []
for line in fileinput.input(files):
m = line_matcher.search(line)
if m:
aliases.append((m.group(1), m.group(2)))
return aliases
def parse_args(argv):
""" Parse and validate command line arguments """
parser = argparse.ArgumentParser(description=__description__)
# TODO: Add arguments here
parser.add_argument('file', nargs='+', help='Files to look for aliases in. Use - for STDIN')
parser.add_argument('--without-alias', '-a', action='store_true', help='Do not expect aliases to have the word alias')
args = parser.parse_args(argv[1:])
# TODO: Add validation here
# if args.blah: parser.error("error")
return args
def main(argv=[__name__]):
""" Run this program """
args = parse_args(argv)
try:
files = [x for x in args.file if os.path.isfile(x) or x=='-']
if len(files) == 0 and args != sys.argv[1:]:
print("Some files could not be found", file=sys.stderr)
return 1
alist = get_aliases(files, args)
printalias(alist)
except KeyboardInterrupt:
return -1
if __name__ == '__main__':
sys.exit(main(sys.argv) or 0)