terminal2: better blank line handling

This commit is contained in:
Mitchell Hashimoto
2024-03-07 13:47:17 -08:00
parent 0b2b56506a
commit 17cfdc0487

View File

@ -885,11 +885,16 @@ pub fn selectionString(
const cells = cells_ptr[start_x..end_x]; const cells = cells_ptr[start_x..end_x];
for (cells) |*cell| { for (cells) |*cell| {
if (!cell.hasText()) continue; // Skip wide spacers
switch (cell.wide) {
.narrow, .wide => {},
.spacer_head, .spacer_tail => continue,
}
var buf: [4]u8 = undefined; var buf: [4]u8 = undefined;
{ {
const char = if (cell.content.codepoint > 0) cell.content.codepoint else ' '; const raw: u21 = if (cell.hasText()) cell.content.codepoint else 0;
const char = if (raw > 0) raw else ' ';
const encode_len = try std.unicode.utf8Encode(char, &buf); const encode_len = try std.unicode.utf8Encode(char, &buf);
try strbuilder.appendSlice(buf[0..encode_len]); try strbuilder.appendSlice(buf[0..encode_len]);
} }
@ -931,8 +936,11 @@ pub fn selectionString(
try strbuilder.append('\n'); try strbuilder.append('\n');
} }
// Remove our trailing newline again // Remove all trailing newlines
if (strbuilder.items.len > 0) strbuilder.items.len -= 1; for (0..strbuilder.items.len) |_| {
if (strbuilder.items[strbuilder.items.len - 1] != '\n') break;
strbuilder.items.len -= 1;
}
} }
// Get our final string // Get our final string
@ -5221,7 +5229,7 @@ test "Screen: selectionString trim empty line" {
var s = try init(alloc, 5, 5, 0); var s = try init(alloc, 5, 5, 0);
defer s.deinit(); defer s.deinit();
const str = "1AB \n \n2EFGH\n3IJKL"; const str = "1AB \n\n2EFGH\n3IJKL";
try s.testWriteString(str); try s.testWriteString(str);
const sel = Selection.init( const sel = Selection.init(
@ -5462,38 +5470,38 @@ test "Screen: selectionString, rectangle, w/EOL" {
try testing.expectEqualStrings(expected, contents); try testing.expectEqualStrings(expected, contents);
} }
// test "Screen: selectionString, rectangle, more complex w/breaks" { test "Screen: selectionString, rectangle, more complex w/breaks" {
// const testing = std.testing; const testing = std.testing;
// const alloc = testing.allocator; const alloc = testing.allocator;
//
// var s = try init(alloc, 30, 8, 0); var s = try init(alloc, 30, 8, 0);
// defer s.deinit(); defer s.deinit();
// const str = const str =
// \\Lorem ipsum dolor \\Lorem ipsum dolor
// \\sit amet, consectetur \\sit amet, consectetur
// \\adipiscing elit, sed do \\adipiscing elit, sed do
// \\eiusmod tempor incididunt \\eiusmod tempor incididunt
// \\ut labore et dolore \\ut labore et dolore
// \\ \\
// \\magna aliqua. Ut enim \\magna aliqua. Ut enim
// \\ad minim veniam, quis \\ad minim veniam, quis
// ; ;
// const sel = Selection.init( const sel = Selection.init(
// s.pages.pin(.{ .screen = .{ .x = 11, .y = 2 } }).?, s.pages.pin(.{ .screen = .{ .x = 11, .y = 2 } }).?,
// s.pages.pin(.{ .screen = .{ .x = 26, .y = 7 } }).?, s.pages.pin(.{ .screen = .{ .x = 26, .y = 7 } }).?,
// true, true,
// ); );
// const expected = const expected =
// \\elit, sed do \\elit, sed do
// \\por incididunt \\por incididunt
// \\t dolore \\t dolore
// \\ \\
// \\a. Ut enim \\a. Ut enim
// \\niam, quis \\niam, quis
// ; ;
// try s.testWriteString(str); try s.testWriteString(str);
//
// const contents = try s.selectionString(alloc, sel, true); const contents = try s.selectionString(alloc, sel, true);
// defer alloc.free(contents); defer alloc.free(contents);
// try testing.expectEqualStrings(expected, contents); try testing.expectEqualStrings(expected, contents);
// } }