From d8d153579ec7e82b6bd35c620b7e72f68f5d71cd Mon Sep 17 00:00:00 2001 From: b4b4r07 Date: Mon, 3 Apr 2023 11:19:00 +0900 Subject: [PATCH] Format awk scripts --- functions/enhancd/lib/fuzzy.awk | 92 ++++++++++++------------- functions/enhancd/lib/has_dup_lines.awk | 10 +-- functions/enhancd/lib/help.awk | 72 +++++++++---------- functions/enhancd/lib/ltsv.awk | 12 ++-- functions/enhancd/lib/reverse.awk | 8 +-- functions/enhancd/lib/split.awk | 58 ++++++++-------- functions/enhancd/lib/step_by_step.awk | 16 ++--- functions/enhancd/lib/to_abspath.awk | 56 +++++++-------- 8 files changed, 162 insertions(+), 162 deletions(-) diff --git a/functions/enhancd/lib/fuzzy.awk b/functions/enhancd/lib/fuzzy.awk index 8e246c6..4519519 100644 --- a/functions/enhancd/lib/fuzzy.awk +++ b/functions/enhancd/lib/fuzzy.awk @@ -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 @@ -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 { @@ -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 } diff --git a/functions/enhancd/lib/has_dup_lines.awk b/functions/enhancd/lib/has_dup_lines.awk index c62e97a..a3de195 100644 --- a/functions/enhancd/lib/has_dup_lines.awk +++ b/functions/enhancd/lib/has_dup_lines.awk @@ -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; } diff --git a/functions/enhancd/lib/help.awk b/functions/enhancd/lib/help.awk index 9fb3b3b..645ff49 100644 --- a/functions/enhancd/lib/help.awk +++ b/functions/enhancd/lib/help.awk @@ -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":"]; } diff --git a/functions/enhancd/lib/ltsv.awk b/functions/enhancd/lib/ltsv.awk index 147226f..0bb8a29 100644 --- a/functions/enhancd/lib/ltsv.awk +++ b/functions/enhancd/lib/ltsv.awk @@ -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":"]; } diff --git a/functions/enhancd/lib/reverse.awk b/functions/enhancd/lib/reverse.awk index 744b482..1a71d5c 100644 --- a/functions/enhancd/lib/reverse.awk +++ b/functions/enhancd/lib/reverse.awk @@ -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] + } } diff --git a/functions/enhancd/lib/split.awk b/functions/enhancd/lib/split.awk index 7f003f2..90044ea 100644 --- a/functions/enhancd/lib/split.awk +++ b/functions/enhancd/lib/split.awk @@ -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, "/") } diff --git a/functions/enhancd/lib/step_by_step.awk b/functions/enhancd/lib/step_by_step.awk index 2f4beb9..f82ae46 100644 --- a/functions/enhancd/lib/step_by_step.awk +++ b/functions/enhancd/lib/step_by_step.awk @@ -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; + } } diff --git a/functions/enhancd/lib/to_abspath.awk b/functions/enhancd/lib/to_abspath.awk index 782e10b..6ade7ea 100644 --- a/functions/enhancd/lib/to_abspath.awk +++ b/functions/enhancd/lib/to_abspath.awk @@ -1,43 +1,43 @@ BEGIN { - # If dir is a slash, return a slash and exit - if (dir == "/") { - print "/" - exit - } - if (cwd !~ dir) { - exit - } + # If dir is a slash, return a slash and exit + if (dir == "/") { + print "/" + exit + } + if (cwd !~ dir) { + exit + } - # pos is a position of dir in cwd - pos = rindex(cwd, dir) + # pos is a position of dir in cwd + pos = rindex(cwd, dir) - # If it is not find the dir in the cwd, then exit - if (pos == 0) { - print cwd - exit - } + # If it is not find the dir in the cwd, then exit + if (pos == 0) { + print cwd + exit + } - # convert the divided directory name to the absolute path - # that the directory name is contained - print erase(cwd, pos+length(dir)) + # convert the divided directory name to the absolute path + # that the directory name is contained + print erase(cwd, pos+length(dir)) } # erase erases the part of the path function erase(str, pos) { - return substr(str, 1, pos-1) + return substr(str, 1, pos-1) } # rindex returns the index of the last instance of find in string, # or 0 if find is not present function rindex(string, find, k, ns, nf) { - ns = length(string) - nf = length(find) - for (k = ns+1-nf; k >= 1; k--) { - if (substr(string, k, nf) == find) { - if (k > 1 && substr(string, k-1, 1) == "/") - return k - else if (k == 1) - return k - } + ns = length(string) + nf = length(find) + for (k = ns+1-nf; k >= 1; k--) { + if (substr(string, k, nf) == find) { + if (k > 1 && substr(string, k-1, 1) == "/") + return k + else if (k == 1) + return k } + } }