Skip to content

Lightpanda: the headless browser designed for AI and automation

Merge branch 'main' into run-command

Adrià Arrufat805b60d626fb
diff --cc src/Config.zig
index 726f95b03,556d54850..ff66a5cf8
--- a/src/Config.zig
+++ b/src/Config.zig
@@@ -722,9 -729,9 +740,9 @@@ pub fn printUsageAndExit(self: *const C
\\{s}
\\
, .{Help.general});
- std.debug.print(template, .{exec_name});
+ break :text try std.fmt.allocPrint(allocator, template, .{exec_name});
},
- inline .fetch, .serve, .mcp, .agent, .run => |tag| {
- inline .fetch, .serve, .mcp, .agent => |tag| text: {
++ inline .fetch, .serve, .mcp, .agent, .run => |tag| text: {
const template = comptimePrint(
\\{s}
\\
@@@ -745,8 -756,54 +767,54 @@@
std.process.exit(1);
}
+ fn printPlain(text: []const u8) void {
+ var stdout = std.fs.File.stdout().writer(&.{});
+ stdout.interface.writeAll(text) catch {};
+ }
+
+ /// Pages explicitly requested help through $PAGER (fallback: less) when
+ /// stdout is an interactive terminal; prints plainly otherwise.
+ fn printPaged(allocator: Allocator, text: []const u8) void {
+ if (!std.posix.isatty(std.posix.STDOUT_FILENO)) {
+ return printPlain(text);
+ }
+ const term = std.posix.getenv("TERM") orelse "";
+ if (term.len == 0 or std.mem.eql(u8, term, "dumb")) {
+ return printPlain(text);
+ }
+
+ const pager = std.posix.getenv("PAGER") orelse "";
+ const argv: []const []const u8 = if (pager.len > 0)
+ &.{ "/bin/sh", "-c", pager }
+ else
+ &.{ "less", "-FIRX" };
+
+ var child = std.process.Child.init(argv, allocator);
+ child.stdin_behavior = .Pipe;
+ child.stdout_behavior = .Inherit;
+ child.stderr_behavior = .Inherit;
+ child.spawn() catch return printPlain(text);
+
+ if (child.stdin) |stdin| {
+ var writer = stdin.writer(&.{});
+ // A write error here is the pager exiting early (user quit, or the
+ // command failed) — wait() below decides which.
+ writer.interface.writeAll(text) catch {};
+ stdin.close();
+ child.stdin = null;
+ }
+
+ const term_result = child.wait() catch return printPlain(text);
+ const clean_exit = term_result == .Exited and term_result.Exited == 0;
+ // Quitting the pager early is still exit 0; a non-zero exit means the
+ // pager failed (e.g. $PAGER not found) and the help was never shown.
+ if (!clean_exit) {
+ printPlain(text);
+ }
+ }
+
pub fn parseArgs(allocator: Allocator) !Config {
- const exec_name, const command = try Commands.parse(allocator);
+ const exec_name, var command = try Commands.parse(allocator);
if (command == .serve and command.serve.timeout != null) {
log.warn(.app, "--timeout is deprecated", .{});
}
diff --cc src/help.zon
index eb4023a9c,ad9ee2ec4..70809b9b7
--- a/src/help.zon
+++ b/src/help.zon
@@@ -7,8 -7,7 +7,8 @@@
\\ agent starts an interactive AI agent that can browse the web
\\ fetch fetches the specified URL
\\ help displays this message
- \\ mcp starts an MCP (Model Context Protocol) server over stdio
+ \\ mcp starts an MCP (Model Context Protocol) server (stdio or HTTP)
+ \\ run runs a saved script (no LLM), then exits
\\ serve starts a WebSocket CDP server
\\ version displays the version of {0s}
\\