Something went wrong. Try again.
Zig bindings for all 506 operations of the Forgejo API, generated from its own description, and zfj, a command line built on them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The text of an issue's body or a comment, from wherever the command line//! says: `-b TEXT`, `-b -` or `-F -` for stdin, `-F FILE`, or -- when none of//! them is given and there is a terminal to use -- an editor.
const std = @import("std");const builtin = @import("builtin");const Io = std.Io;
const Context = @import("Context.zig");
pub const Source = struct { /// `-b` body: ?[]const u8 = null, /// `-F` body_file: ?[]const u8 = null,};
pub const Need = enum { /// Nothing given, and nobody to ask, is no body at all. optional, /// Nothing given, and nobody to ask, is a usage error; so is an editor /// left empty. required,};
/// The body, or null when there is none and none was needed. Fails with/// `error.Usage`, having said why.pub fn body(ctx: *Context, source: Source, need: Need, what: []const u8) (Context.Failure || error{OutOfMemory})!?[]const u8 { if (source.body != null and source.body_file != null) { _ = ctx.usage("give -b or -F, not both", .{}); return error.Usage; } if (source.body) |b| { if (!std.mem.eql(u8, b, "-")) return b; return try readStdin(ctx); } if (source.body_file) |path| { if (std.mem.eql(u8, path, "-")) return try readStdin(ctx); return Io.Dir.cwd().readFileAlloc(ctx.io, path, ctx.arena, .limited(16 << 20)) catch |err| { _ = ctx.usage("cannot read {s}: {s}", .{ path, @errorName(err) }); return error.Usage; }; }
const stdin: Io.File = .stdin(); const interactive = (stdin.isTty(ctx.io) catch false) and ctx.stdoutIsTty(); if (!interactive) { if (need == .optional) return null; _ = ctx.usage("no {s}; give one with -b TEXT, -b - (stdin) or -F FILE", .{what}); return error.Usage; }
const written = try edit(ctx, what); if (std.mem.trim(u8, written, " \t\r\n").len == 0) { if (need == .optional) return null; _ = ctx.usage("the {s} was left empty, so nothing was done", .{what}); return error.Usage; } return written;}
fn readStdin(ctx: *Context) (Context.Failure || error{OutOfMemory})![]const u8 { const stdin: Io.File = .stdin(); var buffer: [4096]u8 = undefined; var reader = stdin.reader(ctx.io, &buffer); return reader.interface.allocRemaining(ctx.arena, .limited(16 << 20)) catch |err| switch (err) { error.OutOfMemory => error.OutOfMemory, else => { _ = ctx.usage("cannot read stdin: {s}", .{@errorName(err)}); return error.Usage; }, };}
/// What was written in `$VISUAL`, else `$EDITOR`, else `vi` (`notepad` on/// Windows), opened on an empty temporary file that is removed afterwards.fn edit(ctx: *Context, what: []const u8) (Context.Failure || error{OutOfMemory})![]const u8 { const windows = builtin.os.tag == .windows; const editor = ctx.environ.get("VISUAL") orelse ctx.environ.get("EDITOR") orelse if (windows) "notepad" else "vi"; const directory = if (windows) ctx.environ.get("TEMP") orelse ctx.environ.get("TMP") orelse "." else ctx.environ.get("TMPDIR") orelse "/tmp"; var random: [8]u8 = undefined; ctx.io.random(&random); const path = try std.fmt.allocPrint(ctx.arena, "{s}{c}zfj-{x}.md", .{ std.mem.trimEnd(u8, directory, "/\\"), @as(u8, if (windows) '\\' else '/'), random, });
var file = Io.Dir.cwd().createFile(ctx.io, path, .{ .exclusive = true }) catch |err| { _ = ctx.environment("cannot make a file to write the {s} in: {s}", .{ what, @errorName(err) }); return error.Environment; }; file.close(ctx.io); defer Io.Dir.cwd().deleteFile(ctx.io, path) catch {};
// The editor's command may carry arguments of its own (`code --wait`): // it is split into words, and the file goes on the end. const words = try Context.splitCommand(ctx.arena, editor); if (words.len == 0) { _ = ctx.usage("the editor is empty; set $EDITOR, or give the {s} with -b or -F", .{what}); return error.Usage; } const argv = try std.mem.concat(ctx.arena, []const u8, &.{ words, &.{path} }); var child = std.process.spawn(ctx.io, .{ .argv = argv }) catch |err| { _ = ctx.environment("cannot start the editor '{s}': {s}", .{ editor, @errorName(err) }); return error.Environment; }; const term = child.wait(ctx.io) catch |err| { _ = ctx.environment("the editor '{s}' was lost: {s}", .{ editor, @errorName(err) }); return error.Environment; }; switch (term) { .exited => |code| if (code != 0) { _ = ctx.usage("the editor '{s}' exited with {d}, so nothing was done", .{ editor, code }); return error.Usage; }, else => { _ = ctx.usage("the editor '{s}' did not finish, so nothing was done", .{editor}); return error.Usage; }, } return Io.Dir.cwd().readFileAlloc(ctx.io, path, ctx.arena, .limited(16 << 20)) catch |err| { _ = ctx.environment("cannot read back what was written: {s}", .{@errorName(err)}); return error.Environment; };}