From bacacf6678168f3b3d0657c4fbbcac0edcb5c799 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 17 Feb 2019 14:38:11 -0500 Subject: Add line numbers to hi Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. --- bin/hi.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'bin/hi.c') diff --git a/bin/hi.c b/bin/hi.c index ca62aa16..b3584b10 100644 --- a/bin/hi.c +++ b/bin/hi.c @@ -41,7 +41,8 @@ typedef unsigned Set; X(Format) \ X(Interp) \ X(Comment) \ - X(Todo) + X(Todo) \ + X(Line) enum Class { #define X(class) class, @@ -361,6 +362,7 @@ static const enum SGR ANSIStyle[ClassLen][3] = { [Interp] = { SGRYellow }, [Comment] = { SGRBlue }, [Todo] = { SGRBlue, SGRBoldOn, SGRBoldOff }, + [Line] = { SGRRed }, }; static void @@ -474,6 +476,7 @@ static const char *HTMLStyle[ClassLen] = { [Interp] = "color: olive;", [Comment] = "color: navy;", [Todo] = "color: navy; font-weight: bold;", + [Line] = "color: maroon;", }; static void htmlTabSize(const char *tab) { @@ -613,9 +616,10 @@ int main(int argc, char *argv[]) { struct Language lang = {0}; struct Format format = Formats[0]; const char *opts[OptionLen] = {0}; + bool number = false; int opt; - while (0 < (opt = getopt(argc, argv, "cf:l:n:o:"))) { + while (0 < (opt = getopt(argc, argv, "cf:l:m:no:"))) { switch (opt) { break; case 'c': check(); return EX_OK; break; case 'f': { @@ -628,7 +632,8 @@ int main(int argc, char *argv[]) { errx(EX_USAGE, "no such language %s", optarg); } } - break; case 'n': name = optarg; + break; case 'm': name = optarg; + break; case 'n': number = true; break; case 'o': { enum Option key; char *keystr, *valstr; @@ -685,9 +690,15 @@ int main(int argc, char *argv[]) { highlight(lang, hi, str); - if (format.header) format.header(opts); size_t run = 0; + size_t line = 0; + if (format.header) format.header(opts); for (size_t i = 0; i < len; i += run) { + if (number && (!i || str[i - 1] == '\n')) { + char num[sizeof("9999 ")]; + snprintf(num, sizeof(num), "%4zu ", ++line); + format.output(opts, Line, num, sizeof(num) - 1); + } for (run = 1; i + run < len; ++run) { if (hi[i + run] != hi[i]) break; if (str[i + run - 1] == '\n') break; -- cgit 1.4.1 9'>unfollow)
Commit message (Expand)Author
2022-03-22Source ~/.profile.local if it existsJune McEnroe
2022-03-18Publish "Addendum 2021"June McEnroe
2022-03-16Remove wcwidth portJune McEnroe
2022-03-16Remove -j4 from ./PlanJune McEnroe
2022-03-15Rewrite Linux install.sh for DebianJune McEnroe
2022-03-15Remove dashJune McEnroe