-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdcv-prettify
executable file
·113 lines (97 loc) · 2.35 KB
/
sdcv-prettify
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
#!/usr/bin/awk -f
# -*- coding: utf-8 -*-
#
# NAME
#
# sdcv-prettify - AWK script to format and prettify sdcv's output
#
# SYNOPSIS
#
# sdcv-prettify [--color] [<file>...]
#
# DESCRIPTION
#
# sdcv-prettify is an AWK script to filter sdcv's output. It has the
# following functions:
#
# o Colorizes dictionary titles and pronunciation if `--color' is
# supplied as the first argument.
# o Increases indentation so that the layout doesn't look
# congested.
# o Wraps paragraphs to 72 characters without breaking up words.
# o Wraps numbered lists with proper padding.
#
# The best way to use this script is by setting the SDCV_PAGER
# environment variable:
#
# export SDCV_PAGER="sdcv-prettify"
#
# or even better,
#
# export SDCV_PAGER="sdcv-prettify --color | less -icR"
#
BEGIN {
COLUMNS = 72
if (ARGV[1] == "--color") {
CYAN = sprintf("\033[38;5;6m")
YELLOW = sprintf("\033[38;5;3m")
RESET = sprintf("\033[0m")
# Cleanup arguments.
for (i = 1; i < ARGC; i++)
ARGV[i] = ARGV[i + 1]
delete ARGV[ARGC]
ARGC--
}
margin = 0
wrap_margin = 0
}
function padding(n) {
return sprintf("%" n "s", "")
}
function wrap(line, wrap_margin) {
n = split(line, words)
wrapped = ""
width = 0
for (i = 1; i <= n; i++) {
if (width + length(words[i]) <= COLUMNS) {
wrapped = wrapped " " words[i]
width = width + length(words[i]) + 1
} else {
wrapped = wrapped "\n" padding(wrap_margin) words[i]
width = wrap_margin + length(words[i])
}
}
return substr(wrapped, 2)
}
{
# There's some bug in sdcv which causes sdcv to not convert
# XML entities properly. So we do it here.
gsub("&", "\\&")
gsub("'", "'")
gsub(">", ">")
gsub("<", "<")
gsub(""", "\"")
gsub("<[^>]*>", "")
if (match($0, /^ */)) {
# Break line if indentation changes.
if (margin != RLENGTH)
printf("\n")
margin = RLENGTH
# Wrap numbered and bulleted items like vim: fo+=n.
if (match($0, /^ *([0-9]+[.)]|■) */)) {
printf("\n")
wrap_margin = RLENGTH + margin
}
else {
wrap_margin = 2 * margin
}
# Double the indentation.
printf padding(2 * margin)
}
if ($0 ~ /^-->/)
print CYAN $0 RESET
else if ($0 ~ /^\[.+\]$/)
print YELLOW $0 RESET
else
print wrap($0, wrap_margin)
}