Skip to content

Commit 3b19e7c

Browse files
authored
Format awk scripts (#201)
1 parent 00da9eb commit 3b19e7c

File tree

8 files changed

+162
-162
lines changed

8 files changed

+162
-162
lines changed

functions/enhancd/lib/fuzzy.awk

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
BEGIN {
2-
# add 'delete lines' essentially declares it as an array.
3-
# https://groups.google.com/g/comp.lang.awk/c/jrRiumpwr20
4-
lines["dummy"]; delete lines["dummy"]
2+
# add 'delete lines' essentially declares it as an array.
3+
# https://groups.google.com/g/comp.lang.awk/c/jrRiumpwr20
4+
lines["dummy"]; delete lines["dummy"]
55

6-
hit_leven_dist = 0;
7-
FS = "/";
6+
hit_leven_dist = 0;
7+
FS = "/";
88
}
99

1010
# https://stackoverflow.com/questions/11534173/how-to-use-awk-variables-in-regular-expressions
@@ -14,13 +14,13 @@ $0 ~ "\\/.?" search_string "[^\\/]*$" {
1414
}
1515

1616
{
17-
# calculates the degree of similarity
18-
if ( (1 - leven_dist($NF, search_string) / (length($NF) + length(search_string))) * 100 >= 70 ) {
19-
hit_leven_dist = 1
20-
# When the degree of similarity of search_string is greater than or equal to 70%,
21-
# to display the candidate path
22-
print $0
23-
}
17+
# calculates the degree of similarity
18+
if ( (1 - leven_dist($NF, search_string) / (length($NF) + length(search_string))) * 100 >= 70 ) {
19+
hit_leven_dist = 1
20+
# When the degree of similarity of search_string is greater than or equal to 70%,
21+
# to display the candidate path
22+
print $0
23+
}
2424
}
2525

2626
END {
@@ -35,50 +35,50 @@ END {
3535

3636
# leven_dist returns the Levenshtein distance two text string
3737
function leven_dist(a, b) {
38-
lena = length(a);
39-
lenb = length(b);
38+
lena = length(a);
39+
lenb = length(b);
4040

41-
if (lena == 0) {
42-
return lenb;
43-
}
44-
if (lenb == 0) {
45-
return lena;
46-
}
41+
if (lena == 0) {
42+
return lenb;
43+
}
44+
if (lenb == 0) {
45+
return lena;
46+
}
4747

48-
for (row = 1; row <= lena; row++) {
49-
m[row,0] = row
50-
}
51-
for (col = 1; col <= lenb; col++) {
52-
m[0,col] = col
53-
}
48+
for (row = 1; row <= lena; row++) {
49+
m[row,0] = row
50+
}
51+
for (col = 1; col <= lenb; col++) {
52+
m[0,col] = col
53+
}
5454

55-
for (row = 1; row <= lena; row++) {
56-
ai = substr(a, row, 1)
57-
for (col = 1; col <= lenb; col++) {
58-
bi = substr(b, col, 1)
59-
if (ai == bi) {
60-
cost = 0
61-
} else {
62-
cost = 1
63-
}
64-
m[row,col] = min(m[row-1,col]+1, m[row,col-1]+1, m[row-1,col-1]+cost)
65-
}
55+
for (row = 1; row <= lena; row++) {
56+
ai = substr(a, row, 1)
57+
for (col = 1; col <= lenb; col++) {
58+
bi = substr(b, col, 1)
59+
if (ai == bi) {
60+
cost = 0
61+
} else {
62+
cost = 1
63+
}
64+
m[row,col] = min(m[row-1,col]+1, m[row,col-1]+1, m[row-1,col-1]+cost)
6665
}
66+
}
6767

68-
return m[lena,lenb]
68+
return m[lena,lenb]
6969
}
7070

7171
# min returns the smaller of x, y or z
7272
function min(a, b, c) {
73-
result = a
73+
result = a
7474

75-
if (b < result) {
76-
result = b
77-
}
75+
if (b < result) {
76+
result = b
77+
}
7878

79-
if (c < result) {
80-
result = c
81-
}
79+
if (c < result) {
80+
result = c
81+
}
8282

83-
return result
83+
return result
8484
}
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
BEGIN {
2-
flag = 0;
2+
flag = 0;
33
}
44

55
{
6-
++a[$0];
7-
if (a[$0] > 1)
8-
flag = 1;
6+
++a[$0];
7+
if (a[$0] > 1)
8+
flag = 1;
99
}
1010

1111
END {
12-
exit flag == 1 ? 0 : 1;
12+
exit flag == 1 ? 0 : 1;
1313
}

functions/enhancd/lib/help.awk

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
BEGIN {
2-
FS = "\t";
3-
len = 0;
2+
FS = "\t";
3+
len = 0;
44

5-
# Print header
6-
print "Usage: cd [OPTIONS] [dir]"
7-
print ""
8-
print "OPTIONS:"
5+
# Print header
6+
print "Usage: cd [OPTIONS] [dir]"
7+
print ""
8+
print "OPTIONS:"
99
}
1010

1111
# Skip commented line starting with # or //
1212
/^(#|\/\/)/ { next }
1313

1414
{
15-
condition = ltsv("condition")
16-
if (condition != "") {
17-
command = sprintf("%s &>/dev/null", condition);
18-
code = system(command)
19-
close(command);
20-
if (code == 1) { next }
21-
}
22-
23-
len++;
24-
25-
short = ltsv("short")
26-
long = ltsv("long")
27-
desc = ltsv("desc")
28-
29-
if (short == "") {
30-
printf " %s %-15s %s\n", " ", long, desc
31-
} else if (long == "") {
32-
printf " %s %-15s %s\n", short, "", desc
33-
} else {
34-
printf " %s, %-15s %s\n", short, long, desc
35-
}
15+
condition = ltsv("condition")
16+
if (condition != "") {
17+
command = sprintf("%s &>/dev/null", condition);
18+
code = system(command)
19+
close(command);
20+
if (code == 1) { next }
21+
}
22+
23+
len++;
24+
25+
short = ltsv("short")
26+
long = ltsv("long")
27+
desc = ltsv("desc")
28+
29+
if (short == "") {
30+
printf " %s %-15s %s\n", " ", long, desc
31+
} else if (long == "") {
32+
printf " %s %-15s %s\n", short, "", desc
33+
} else {
34+
printf " %s, %-15s %s\n", short, long, desc
35+
}
3636
}
3737

3838
END {
39-
# Print footer
40-
if (len == 0) { print " No available options now" }
41-
print ""
42-
printf "Version: %s\n", ENVIRON["_ENHANCD_VERSION"]
39+
# Print footer
40+
if (len == 0) { print " No available options now" }
41+
print ""
42+
printf "Version: %s\n", ENVIRON["_ENHANCD_VERSION"]
4343
}
4444

4545
function ltsv(key) {
46-
for (i = 1; i <= NF; i++) {
47-
match($i, ":");
48-
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
49-
};
50-
return xs[key":"];
46+
for (i = 1; i <= NF; i++) {
47+
match($i, ":");
48+
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
49+
};
50+
return xs[key":"];
5151
}

functions/enhancd/lib/ltsv.awk

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
BEGIN {
2-
FS = "\t";
2+
FS = "\t";
33
}
44

55
# Skip commented line starting with # or //
66
/^(#|\/\/)/ {next}
77

88
function ltsv(key) {
9-
for (i = 1; i <= NF; i++) {
10-
match($i, ":");
11-
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
12-
};
13-
return xs[key":"];
9+
for (i = 1; i <= NF; i++) {
10+
match($i, ":");
11+
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
12+
};
13+
return xs[key":"];
1414
}

functions/enhancd/lib/reverse.awk

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
line[NR] = $0
2+
line[NR] = $0
33
}
44

55
END {
6-
for (i = NR; i > 0; i--) {
7-
print line[i]
8-
}
6+
for (i = NR; i > 0; i--) {
7+
print line[i]
8+
}
99
}

functions/enhancd/lib/split.awk

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
BEGIN {
2-
# check if arg is an valid path
3-
if (!isabs(arg)) {
4-
print "split_path requires an absolute path begins with a slash" >"/dev/stderr"
5-
exit 1
6-
}
2+
# check if arg is an valid path
3+
if (!isabs(arg)) {
4+
print "split_path requires an absolute path begins with a slash" >"/dev/stderr"
5+
exit 1
6+
}
77

8-
# except for the beginning of the slash
9-
s = substr(arg, 2)
10-
num = split(s, arr, "/")
8+
# except for the beginning of the slash
9+
s = substr(arg, 2)
10+
num = split(s, arr, "/")
1111

12-
# display the beginning of the path
13-
print substr(arg, 1, 1)
12+
# display the beginning of the path
13+
print substr(arg, 1, 1)
1414

15-
if (show_fullpath == 1) {
16-
# get dirname from /
17-
num = split(s, dirname, "/")
18-
for (i = 1; i < num; i++) {
19-
pre_dir = ""
20-
for (ii = 1; ii <= i; ii++) {
21-
pre_dir = pre_dir "/" dirname[ii]
22-
}
23-
print pre_dir
24-
}
25-
} else {
26-
# decompose the path by a slash
27-
for (i = 1; i < num; i++) {
28-
print arr[i]
29-
}
15+
if (show_fullpath == 1) {
16+
# get dirname from /
17+
num = split(s, dirname, "/")
18+
for (i = 1; i < num; i++) {
19+
pre_dir = ""
20+
for (ii = 1; ii <= i; ii++) {
21+
pre_dir = pre_dir "/" dirname[ii]
22+
}
23+
print pre_dir
24+
}
25+
} else {
26+
# decompose the path by a slash
27+
for (i = 1; i < num; i++) {
28+
print arr[i]
3029
}
30+
}
3131
}
3232

3333
# has_prefix tests whether the string s begins with pre.
3434
function has_prefix(s, pre, pre_len, s_len) {
35-
pre_len = length(pre)
36-
s_len = length(s)
35+
pre_len = length(pre)
36+
s_len = length(s)
3737

38-
return pre_len <= s_len && substr(s, 1, pre_len) == pre
38+
return pre_len <= s_len && substr(s, 1, pre_len) == pre
3939
}
4040

4141
# isabs returns true if the path is absolute.
4242
function isabs(pathname) {
43-
return length(pathname) > 0 && has_prefix(pathname, "/")
43+
return length(pathname) > 0 && has_prefix(pathname, "/")
4444
}
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
BEGIN {
2-
count = gsub(/\//, "/", dir);
3-
for (i = 0; i < count; i++) {
4-
gsub(/\/[^\/]*$/, "", dir);
5-
if (dir == "")
6-
print "/";
7-
else
8-
print dir;
9-
}
2+
count = gsub(/\//, "/", dir);
3+
for (i = 0; i < count; i++) {
4+
gsub(/\/[^\/]*$/, "", dir);
5+
if (dir == "")
6+
print "/";
7+
else
8+
print dir;
9+
}
1010
}

0 commit comments

Comments
 (0)