File: tty.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (212 lines) | stat: -rw-r--r-- 5,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: GPL-3.0-only
// (c) Hare authors <https://harelang.org>

use bufio;
use fmt;
use hare::ast;
use hare::lex;
use hare::unparse;
use io;
use os;
use strings;
use unix::tty;

let no_color: bool = false;

// Formats output as Hare source code (prototypes) with syntax highlighting
export fn emit_tty(ctx: *context) (void | error) = {
	if (os::tryenv("FORCE_COLOR", "") != "" ||
		(os::tryenv("NO_COLOR", "") == "" &&
		tty::isatty(os::stdout_file))) {
		init_colors()?;
	} else {
		no_color = true;
	};

	const summary = ctx.summary;

	if (ctx.ambiguous) {
		const id = unparse::identstr(ctx.ident);
		defer free(id);

		if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprint(ctx.out, "// ")?;
		if (!no_color) fmt::fprint(ctx.out, "\x1b[93m")?;
		fmt::fprint(ctx.out, "NOTE")?;
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[m" "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprintf(ctx.out, ": {} also refers to module [[{}::]]",
			id, id)?;
		if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?;
		fmt::fprintln(ctx.out, "\n")?;
	};

	if (len(ctx.parse_errs) > 0) {
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprint(ctx.out, "// ")?;
		if (!no_color) fmt::fprint(ctx.out, "\x1b[93m")?;
		fmt::fprint(ctx.out, "WARNING")?;
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[m" "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprintln(ctx.out,
			": parsing errors occurred; documentation may be incomplete")?;
		if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?;
	};
	for (let i = 0z; i < len(ctx.parse_errs); i += 1) {
		fmt::fprintln(ctx.out, "//", lex::strerror(ctx.parse_errs[i]))?;
	};
	if (len(ctx.parse_errs) > 0) {
		fmt::fprintln(ctx.out)?;
	};

	match (ctx.readme) {
	case let readme: io::file =>
		let rbuf: [os::BUFSZ]u8 = [0...];
		let readme = bufio::init(readme, rbuf, []);
		let sc = bufio::newscanner(&readme);
		defer bufio::finish(&sc);
		for (let line => bufio::scan_line(&sc)?) {
			firstline = false;
			if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
				color(unparse::synkind::COMMENT))?;
			fmt::fprint(ctx.out, "//", line)?;
			if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?;
			fmt::fprintln(ctx.out)?;
		};
	case void => void;
	};

	emit_submodules_tty(ctx)?;

	// XXX: Should we emit the dependencies, too?
	let printed = false;
	for (let t &.. summary.types) {
		if (details_tty(ctx, t)?) {
			printed = true;
		};
	};
	for (let c &.. summary.constants) {
		if (details_tty(ctx, c)?) {
			printed = true;
		};
	};
	for (let e &.. summary.errors) {
		if (details_tty(ctx, e)?) {
			printed = true;
		};
	};
	for (let g &.. summary.globals) {
		if (details_tty(ctx, g)?) {
			printed = true;
		};
	};
	for (let f &.. summary.funcs) {
		if (details_tty(ctx, f)?) {
			printed = true;
		};
	};

	if (!printed) {
		if (!firstline) {
			fmt::fprintln(ctx.out)?;
		};
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprint(ctx.out, "// No exported declarations")?;
		if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?;
		fmt::fprintln(ctx.out)?;
	};
};

fn emit_submodules_tty(ctx: *context) (void | error) = {
	const ident = unparse::identstr(ctx.ident);
	defer free(ident);

	let max_submod = 0z;
	for (let submod .. ctx.submods) {
		const ln = len(submod.name);
		if (max_submod < ln) {
			max_submod = ln;
		};
	};

	if (len(ctx.submods) != 0) {
		if (!firstline) {
			fmt::fprintln(ctx.out)?;
		};
		firstline = false;
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		if (len(ctx.ident) == 0) {
			fmt::fprintln(ctx.out, "// Modules")?;
		} else {
			fmt::fprintln(ctx.out, "// Submodules")?;
		};
		for (let submod .. ctx.submods) {
			const id = if (len(ident) != 0) {
				yield strings::concat(ident, "::", submod.name)!;
			} else {
				yield strings::dup(submod.name)!;
			};
			defer free(id);

			if (submod.desc != "") {
				const padding = max_submod - len(submod.name);
				fmt::fprintfln(ctx.out, "// - [[{}::]] {%}{}",
					id,
					"", &fmt::mods {
						pad = ' ',
						width = padding,
						...
					},
					submod.desc)?;
			} else {
				fmt::fprintfln(ctx.out, "// - [[{}::]]", id)?;
			};
		};
	};
};

fn details_tty(ctx: *context, decl: *ast::decl) (bool | error) = {
	if (len(decl.docs) == 0 && !ctx.show_undocumented) {
		return false;
	};

	if (!no_color) fmt::fprint(ctx.out, "\x1b[m")?; // reset styling
	if (!firstline) {
		fmt::fprintln(ctx.out)?;
	};
	firstline = false;

	match (&decl.decl) {
	case let d: *ast::decl_func =>
		d.symbol = "";
	case => void;
	};

	unparse::decl(ctx.out, &syn_tty, decl)?;
	fmt::fprintln(ctx.out)?;

	if (ctx.show_lineno) {
		if (!no_color) fmt::fprintf(ctx.out, "\x1b[{}m",
			color(unparse::synkind::COMMENT))?;
		fmt::fprintfln(ctx.out, "// See {}:{}",
			cwdpath(decl.loc.start.path), decl.loc.start.line)?;
	};

	return true;
};

fn syn_tty(
	ctx: *unparse::context,
	s: str,
	kind: unparse::synkind,
) (size | io::error) = {
	let z = 0z;
	if (!no_color) z += fmt::fprintf(ctx.out, "\x1b[{}m", color(kind))?;
	z += unparse::syn_wrap(ctx, s, kind)?;
	if (!no_color) z += fmt::fprint(ctx.out, "\x1b[m")?;
	return z;
};