mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
Improve invalid keybind error messages
This commit is contained in:
@ -7,6 +7,7 @@ const diags = @import("diagnostics.zig");
|
|||||||
const internal_os = @import("../os/main.zig");
|
const internal_os = @import("../os/main.zig");
|
||||||
const Diagnostic = diags.Diagnostic;
|
const Diagnostic = diags.Diagnostic;
|
||||||
const DiagnosticList = diags.DiagnosticList;
|
const DiagnosticList = diags.DiagnosticList;
|
||||||
|
const binding = @import("../input/Binding.zig");
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - Only `--long=value` format is accepted. Do we want to allow
|
// - Only `--long=value` format is accepted. Do we want to allow
|
||||||
@ -126,7 +127,7 @@ pub fn parse(
|
|||||||
|
|
||||||
// The error set is dependent on comptime T, so we always add
|
// The error set is dependent on comptime T, so we always add
|
||||||
// an extra error so we can have the "else" below.
|
// an extra error so we can have the "else" below.
|
||||||
const ErrSet = @TypeOf(err) || error{ Unknown, OutOfMemory };
|
const ErrSet = @TypeOf(err) || error{ Unknown, OutOfMemory } || binding.Error;
|
||||||
const message: [:0]const u8 = switch (@as(ErrSet, @errorCast(err))) {
|
const message: [:0]const u8 = switch (@as(ErrSet, @errorCast(err))) {
|
||||||
// OOM is not recoverable since we need to allocate to
|
// OOM is not recoverable since we need to allocate to
|
||||||
// track more error messages.
|
// track more error messages.
|
||||||
@ -134,6 +135,7 @@ pub fn parse(
|
|||||||
error.InvalidField => "unknown field",
|
error.InvalidField => "unknown field",
|
||||||
error.ValueRequired => formatValueRequired(T, arena_alloc, key) catch "value required",
|
error.ValueRequired => formatValueRequired(T, arena_alloc, key) catch "value required",
|
||||||
error.InvalidValue => formatInvalidValue(T, arena_alloc, key, value) catch "invalid value",
|
error.InvalidValue => formatInvalidValue(T, arena_alloc, key, value) catch "invalid value",
|
||||||
|
error.InvalidFormat, error.InvalidAction => try formatInvalidKeybind(arena_alloc, err == error.InvalidFormat, value.?),
|
||||||
else => try std.fmt.allocPrintZ(
|
else => try std.fmt.allocPrintZ(
|
||||||
arena_alloc,
|
arena_alloc,
|
||||||
"unknown error {}",
|
"unknown error {}",
|
||||||
@ -180,6 +182,28 @@ fn formatInvalidValue(
|
|||||||
return buf.items[0 .. buf.items.len - 1 :0];
|
return buf.items[0 .. buf.items.len - 1 :0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn formatInvalidKeybind(
|
||||||
|
arena_alloc: std.mem.Allocator,
|
||||||
|
isInvalidFormat: bool,
|
||||||
|
value: []const u8,
|
||||||
|
) std.mem.Allocator.Error![:0]const u8 {
|
||||||
|
var buf = std.ArrayList(u8).init(arena_alloc);
|
||||||
|
errdefer buf.deinit();
|
||||||
|
const writer = buf.writer();
|
||||||
|
|
||||||
|
const splitIdx = std.mem.indexOf(u8, value, "=");
|
||||||
|
try writer.print("invalid value \"{s}\"", .{value});
|
||||||
|
const invalid = if (splitIdx) |idx|
|
||||||
|
// If there is an `=` delimiter,
|
||||||
|
if (isInvalidFormat) value[0..idx] else value[idx + 1 ..]
|
||||||
|
else
|
||||||
|
"Missing action";
|
||||||
|
const errorName = if (isInvalidFormat) "InvalidFormat" else "InvalidAction";
|
||||||
|
try writer.print(", {s}: {s}", .{ errorName, invalid });
|
||||||
|
try writer.writeByte(0);
|
||||||
|
return buf.items[0 .. buf.items.len - 1 :0];
|
||||||
|
}
|
||||||
|
|
||||||
fn formatValues(comptime T: type, key: []const u8, writer: anytype) std.mem.Allocator.Error!void {
|
fn formatValues(comptime T: type, key: []const u8, writer: anytype) std.mem.Allocator.Error!void {
|
||||||
const typeinfo = @typeInfo(T);
|
const typeinfo = @typeInfo(T);
|
||||||
inline for (typeinfo.Struct.fields) |f| {
|
inline for (typeinfo.Struct.fields) |f| {
|
||||||
|
Reference in New Issue
Block a user