mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-20 18:56:08 +03:00
config file allows and ignores empty lines, supports comments
This commit is contained in:
@ -333,11 +333,23 @@ pub fn LineIterator(comptime ReaderType: type) type {
|
|||||||
|
|
||||||
pub fn next(self: *Self) ?[]const u8 {
|
pub fn next(self: *Self) ?[]const u8 {
|
||||||
// TODO: detect "--" prefixed lines and give a friendlier error
|
// TODO: detect "--" prefixed lines and give a friendlier error
|
||||||
const buf = self.r.readUntilDelimiterOrEof(self.entry[2..], '\n') catch {
|
const buf = buf: {
|
||||||
|
while (true) {
|
||||||
|
// Read the full line
|
||||||
|
const entry = self.r.readUntilDelimiterOrEof(self.entry[2..], '\n') catch {
|
||||||
// TODO: handle errors
|
// TODO: handle errors
|
||||||
unreachable;
|
unreachable;
|
||||||
} orelse return null;
|
} orelse return null;
|
||||||
|
|
||||||
|
// Trim any whitespace around it
|
||||||
|
const trim = std.mem.trim(u8, entry, " \t");
|
||||||
|
if (trim.len != entry.len) std.mem.copy(u8, entry, trim);
|
||||||
|
|
||||||
|
// Ignore empty lines
|
||||||
|
if (entry.len > 0 and entry[0] != '#') break :buf entry[0..trim.len];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// We need to reslice so that we include our '--' at the beginning
|
// We need to reslice so that we include our '--' at the beginning
|
||||||
// of our buffer so that we can trick the CLI parser to treat it
|
// of our buffer so that we can trick the CLI parser to treat it
|
||||||
// as CLI args.
|
// as CLI args.
|
||||||
@ -357,12 +369,30 @@ test "LineIterator" {
|
|||||||
\\A
|
\\A
|
||||||
\\B
|
\\B
|
||||||
\\C
|
\\C
|
||||||
|
\\
|
||||||
|
\\# A comment
|
||||||
|
\\D
|
||||||
|
\\
|
||||||
|
\\ # An indented comment
|
||||||
|
\\ E
|
||||||
);
|
);
|
||||||
|
|
||||||
var iter = lineIterator(fbs.reader());
|
var iter = lineIterator(fbs.reader());
|
||||||
try testing.expectEqualStrings("--A", iter.next().?);
|
try testing.expectEqualStrings("--A", iter.next().?);
|
||||||
try testing.expectEqualStrings("--B", iter.next().?);
|
try testing.expectEqualStrings("--B", iter.next().?);
|
||||||
try testing.expectEqualStrings("--C", iter.next().?);
|
try testing.expectEqualStrings("--C", iter.next().?);
|
||||||
|
try testing.expectEqualStrings("--D", iter.next().?);
|
||||||
|
try testing.expectEqualStrings("--E", iter.next().?);
|
||||||
|
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
||||||
|
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "LineIterator end in newline" {
|
||||||
|
const testing = std.testing;
|
||||||
|
var fbs = std.io.fixedBufferStream("A\n\n");
|
||||||
|
|
||||||
|
var iter = lineIterator(fbs.reader());
|
||||||
|
try testing.expectEqualStrings("--A", iter.next().?);
|
||||||
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
||||||
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
try testing.expectEqual(@as(?[]const u8, null), iter.next());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user