Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format awk scripts #201

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions functions/enhancd/lib/fuzzy.awk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
BEGIN {
# add 'delete lines' essentially declares it as an array.
# https://groups.google.com/g/comp.lang.awk/c/jrRiumpwr20
lines["dummy"]; delete lines["dummy"]
# add 'delete lines' essentially declares it as an array.
# https://groups.google.com/g/comp.lang.awk/c/jrRiumpwr20
lines["dummy"]; delete lines["dummy"]

hit_leven_dist = 0;
FS = "/";
hit_leven_dist = 0;
FS = "/";
}

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

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

END {
Expand All @@ -35,50 +35,50 @@ END {

# leven_dist returns the Levenshtein distance two text string
function leven_dist(a, b) {
lena = length(a);
lenb = length(b);
lena = length(a);
lenb = length(b);

if (lena == 0) {
return lenb;
}
if (lenb == 0) {
return lena;
}
if (lena == 0) {
return lenb;
}
if (lenb == 0) {
return lena;
}

for (row = 1; row <= lena; row++) {
m[row,0] = row
}
for (col = 1; col <= lenb; col++) {
m[0,col] = col
}
for (row = 1; row <= lena; row++) {
m[row,0] = row
}
for (col = 1; col <= lenb; col++) {
m[0,col] = col
}

for (row = 1; row <= lena; row++) {
ai = substr(a, row, 1)
for (col = 1; col <= lenb; col++) {
bi = substr(b, col, 1)
if (ai == bi) {
cost = 0
} else {
cost = 1
}
m[row,col] = min(m[row-1,col]+1, m[row,col-1]+1, m[row-1,col-1]+cost)
}
for (row = 1; row <= lena; row++) {
ai = substr(a, row, 1)
for (col = 1; col <= lenb; col++) {
bi = substr(b, col, 1)
if (ai == bi) {
cost = 0
} else {
cost = 1
}
m[row,col] = min(m[row-1,col]+1, m[row,col-1]+1, m[row-1,col-1]+cost)
}
}

return m[lena,lenb]
return m[lena,lenb]
}

# min returns the smaller of x, y or z
function min(a, b, c) {
result = a
result = a

if (b < result) {
result = b
}
if (b < result) {
result = b
}

if (c < result) {
result = c
}
if (c < result) {
result = c
}

return result
return result
}
10 changes: 5 additions & 5 deletions functions/enhancd/lib/has_dup_lines.awk
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
BEGIN {
flag = 0;
flag = 0;
}

{
++a[$0];
if (a[$0] > 1)
flag = 1;
++a[$0];
if (a[$0] > 1)
flag = 1;
}

END {
exit flag == 1 ? 0 : 1;
exit flag == 1 ? 0 : 1;
}
72 changes: 36 additions & 36 deletions functions/enhancd/lib/help.awk
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
BEGIN {
FS = "\t";
len = 0;
FS = "\t";
len = 0;

# Print header
print "Usage: cd [OPTIONS] [dir]"
print ""
print "OPTIONS:"
# Print header
print "Usage: cd [OPTIONS] [dir]"
print ""
print "OPTIONS:"
}

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

{
condition = ltsv("condition")
if (condition != "") {
command = sprintf("%s &>/dev/null", condition);
code = system(command)
close(command);
if (code == 1) { next }
}

len++;

short = ltsv("short")
long = ltsv("long")
desc = ltsv("desc")

if (short == "") {
printf " %s %-15s %s\n", " ", long, desc
} else if (long == "") {
printf " %s %-15s %s\n", short, "", desc
} else {
printf " %s, %-15s %s\n", short, long, desc
}
condition = ltsv("condition")
if (condition != "") {
command = sprintf("%s &>/dev/null", condition);
code = system(command)
close(command);
if (code == 1) { next }
}

len++;

short = ltsv("short")
long = ltsv("long")
desc = ltsv("desc")

if (short == "") {
printf " %s %-15s %s\n", " ", long, desc
} else if (long == "") {
printf " %s %-15s %s\n", short, "", desc
} else {
printf " %s, %-15s %s\n", short, long, desc
}
}

END {
# Print footer
if (len == 0) { print " No available options now" }
print ""
printf "Version: %s\n", ENVIRON["_ENHANCD_VERSION"]
# Print footer
if (len == 0) { print " No available options now" }
print ""
printf "Version: %s\n", ENVIRON["_ENHANCD_VERSION"]
}

function ltsv(key) {
for (i = 1; i <= NF; i++) {
match($i, ":");
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
};
return xs[key":"];
for (i = 1; i <= NF; i++) {
match($i, ":");
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
};
return xs[key":"];
}
12 changes: 6 additions & 6 deletions functions/enhancd/lib/ltsv.awk
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
BEGIN {
FS = "\t";
FS = "\t";
}

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

function ltsv(key) {
for (i = 1; i <= NF; i++) {
match($i, ":");
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
};
return xs[key":"];
for (i = 1; i <= NF; i++) {
match($i, ":");
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
};
return xs[key":"];
}
8 changes: 4 additions & 4 deletions functions/enhancd/lib/reverse.awk
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
line[NR] = $0
line[NR] = $0
}

END {
for (i = NR; i > 0; i--) {
print line[i]
}
for (i = NR; i > 0; i--) {
print line[i]
}
}
58 changes: 29 additions & 29 deletions functions/enhancd/lib/split.awk
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
BEGIN {
# check if arg is an valid path
if (!isabs(arg)) {
print "split_path requires an absolute path begins with a slash" >"/dev/stderr"
exit 1
}
# check if arg is an valid path
if (!isabs(arg)) {
print "split_path requires an absolute path begins with a slash" >"/dev/stderr"
exit 1
}

# except for the beginning of the slash
s = substr(arg, 2)
num = split(s, arr, "/")
# except for the beginning of the slash
s = substr(arg, 2)
num = split(s, arr, "/")

# display the beginning of the path
print substr(arg, 1, 1)
# display the beginning of the path
print substr(arg, 1, 1)

if (show_fullpath == 1) {
# get dirname from /
num = split(s, dirname, "/")
for (i = 1; i < num; i++) {
pre_dir = ""
for (ii = 1; ii <= i; ii++) {
pre_dir = pre_dir "/" dirname[ii]
}
print pre_dir
}
} else {
# decompose the path by a slash
for (i = 1; i < num; i++) {
print arr[i]
}
if (show_fullpath == 1) {
# get dirname from /
num = split(s, dirname, "/")
for (i = 1; i < num; i++) {
pre_dir = ""
for (ii = 1; ii <= i; ii++) {
pre_dir = pre_dir "/" dirname[ii]
}
print pre_dir
}
} else {
# decompose the path by a slash
for (i = 1; i < num; i++) {
print arr[i]
}
}
}

# has_prefix tests whether the string s begins with pre.
function has_prefix(s, pre, pre_len, s_len) {
pre_len = length(pre)
s_len = length(s)
pre_len = length(pre)
s_len = length(s)

return pre_len <= s_len && substr(s, 1, pre_len) == pre
return pre_len <= s_len && substr(s, 1, pre_len) == pre
}

# isabs returns true if the path is absolute.
function isabs(pathname) {
return length(pathname) > 0 && has_prefix(pathname, "/")
return length(pathname) > 0 && has_prefix(pathname, "/")
}
16 changes: 8 additions & 8 deletions functions/enhancd/lib/step_by_step.awk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
BEGIN {
count = gsub(/\//, "/", dir);
for (i = 0; i < count; i++) {
gsub(/\/[^\/]*$/, "", dir);
if (dir == "")
print "/";
else
print dir;
}
count = gsub(/\//, "/", dir);
for (i = 0; i < count; i++) {
gsub(/\/[^\/]*$/, "", dir);
if (dir == "")
print "/";
else
print dir;
}
}
Loading