hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit ddf7a846d7583292c5727543e46031bacf8c6dce
parent 287d733e13ea9cf503070c025f7def1133277c6a
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 28 May 2022 20:18:56 -0400

haredoc: highlight submodules bold in tty

This had to be done the way it was because `less` resets text attributes
at the beginning of each line, so the bold escape has to be sent for
every line.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mcmd/haredoc/hare.ha | 34+++-------------------------------
Mcmd/haredoc/tty.ha | 31++++++++++++++++++++++++++++++-
Mcmd/haredoc/util.ha | 36++++++++++++++++++++++++++++++++++++
3 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/cmd/haredoc/hare.ha b/cmd/haredoc/hare.ha @@ -33,7 +33,7 @@ fn emit_hare(ctx: *context) (void | error) = { case void => void; }; - emit_submodules(ctx)?; + emit_submodules_hare(ctx)?; // XXX: Should we emit the dependencies, too? for (let i = 0z; i < len(summary.types); i += 1) { @@ -73,38 +73,10 @@ fn emit_hare(ctx: *context) (void | error) = { }; }; -fn emit_submodules(ctx: *context) (void | error) = { - let identpath = module::identpath(ctx.ident); - defer free(identpath); - - let submodules: []str = []; +fn emit_submodules_hare(ctx: *context) (void | error) = { + const submodules = submodules(ctx)?; defer strings::freeall(submodules); - for (let i = 0z; i < len(ctx.version.subdirs); i += 1) { - let dir = ctx.version.subdirs[i]; - // XXX: the list of reserved directory names is not yet - // finalized. See https://todo.sr.ht/~sircmpwn/hare/516 - if (dir == "contrib") continue; - if (dir == "cmd") continue; - if (dir == "docs") continue; - if (dir == "ext") continue; - if (dir == "vendor") continue; - if (dir == "scripts") continue; - - let submod = [identpath, dir]: ast::ident; - match (module::lookup(ctx.mctx, submod)) { - case let ver: module::version => - // TODO: free version data - void; - case module::notfound => - continue; - case let err: module::error => - return err; - }; - - append(submodules, dir); - }; - if (len(submodules) != 0) { fmt::fprintln(ctx.out)?; if (len(ctx.ident) == 0) { diff --git a/cmd/haredoc/tty.ha b/cmd/haredoc/tty.ha @@ -33,7 +33,7 @@ fn emit_tty(ctx: *context) (void | error) = { case void => void; }; - emit_submodules(ctx)?; + emit_submodules_tty(ctx)?; // XXX: Should we emit the dependencies, too? for (let i = 0z; i < len(summary.types); i += 1) { @@ -53,6 +53,35 @@ fn emit_tty(ctx: *context) (void | error) = { }; }; +fn emit_submodules_tty(ctx: *context) (void | error) = { + const submodules = submodules(ctx)?; + defer strings::freeall(submodules); + + if (len(submodules) != 0) { + fmt::fprintln(ctx.out)?; + if (len(ctx.ident) == 0) { + fmt::fprintln(ctx.out, + "\x1b[1m" "// Modules" "\x1b[0m")?; + } else { + fmt::fprintln(ctx.out, + "\x1b[1m" "// Submodules" "\x1b[0m")?; + }; + for (let i = 0z; i < len(submodules); i += 1) { + let submodule = if (len(ctx.ident) != 0) { + const s = unparse::identstr(ctx.ident); + defer free(s); + yield strings::concat(s, "::", submodules[i]); + } else { + yield strings::dup(submodules[i]); + }; + defer free(submodule); + + fmt::fprintfln(ctx.out, + "\x1b[1m" "// - [[{}]]" "\x1b[0m", submodule)?; + }; + }; +}; + fn comment_tty(out: io::handle, s: str) (size | io::error) = { return fmt::fprintfln(out, "\x1b[1m" "//{}" "\x1b[0m", s)?; }; diff --git a/cmd/haredoc/util.ha b/cmd/haredoc/util.ha @@ -1,6 +1,9 @@ // License: GPL-3.0 +// (c) 2022 Bryan Torres <b@torresjrjr.com> // (c) 2022 Sebastian <sebastian@sebsite.pw> use fmt; +use hare::ast; +use hare::module; use io; use strings; use strio; @@ -32,3 +35,36 @@ fn trim_comment(s: str) str = { }; return strings::dup(strio::string(&trimmed)); }; + +fn submodules(ctx: *context) ([]str | error) = { + let identpath = module::identpath(ctx.ident); + defer free(identpath); + + let submodules: []str = []; + for (let i = 0z; i < len(ctx.version.subdirs); i += 1) { + let dir = ctx.version.subdirs[i]; + // XXX: the list of reserved directory names is not yet + // finalized. See https://todo.sr.ht/~sircmpwn/hare/516 + if (dir == "contrib") continue; + if (dir == "cmd") continue; + if (dir == "docs") continue; + if (dir == "ext") continue; + if (dir == "vendor") continue; + if (dir == "scripts") continue; + + let submod = [identpath, dir]: ast::ident; + match (module::lookup(ctx.mctx, submod)) { + case let ver: module::version => + // TODO: free version data + void; + case module::notfound => + continue; + case let err: module::error => + return err; + }; + + append(submodules, dir); + }; + + return submodules; +};