Skip to content

Commit 6625c5c

Browse files
committed
swapped encode function params, added tests
1 parent 40bc999 commit 6625c5c

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

example/c-vt-paste/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ int main() {
4545
char simple_paste[] = "pasted content";
4646
char encoded[128];
4747

48-
if (ghostty_paste_encoder_encode(simple_paste, strlen(simple_paste), encoder,
48+
if (ghostty_paste_encoder_encode(encoder, simple_paste, strlen(simple_paste),
4949
encoded, sizeof(encoded),
5050
&required) != GHOSTTY_SUCCESS) {
5151
printf("Failed to encode paste data\n");
@@ -69,7 +69,7 @@ int main() {
6969
char encoded_multi[128];
7070

7171
if (ghostty_paste_encoder_encode(
72-
multiline_paste, strlen(multiline_paste), encoder, encoded_multi,
72+
encoder, multiline_paste, strlen(multiline_paste), encoded_multi,
7373
sizeof(encoded_multi), &required) != GHOSTTY_SUCCESS) {
7474
printf("Failed to encode multiline paste data\n");
7575
return 1;

include/ghostty/vt/paste.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* char simple_paste[] = "pasted content";
7676
* char encoded[128];
7777
*
78-
* if (ghostty_paste_encoder_encode(simple_paste, strlen(simple_paste), encoder,
78+
* if (ghostty_paste_encoder_encode(encoder, simple_paste, strlen(simple_paste),
7979
* encoded, sizeof(encoded),
8080
* &required) != GHOSTTY_SUCCESS) {
8181
* printf("Failed to encode paste data\n");
@@ -197,9 +197,9 @@ void ghostty_paste_encoder_set_bracketed(GhosttyPasteEncoder encoder,
197197
* > WARNING: The input data is not checked for safety. See the
198198
* ghostty_paste_is_safe() function to check if the data is safe to paste.
199199
*
200+
* @param encoder The paste encoder handle, (must not be NULL)
200201
* @param data The paste data to encode (must not be NULL)
201202
* @param len The length of the data in bytes
202-
* @param encoder The paste encoder handle, (must not be NULL)
203203
* @param out The output buffer to write the encoded data into (must not be
204204
* NULL)
205205
* @param out_len The length of the output buffer in bytes
@@ -209,9 +209,9 @@ void ghostty_paste_encoder_set_bracketed(GhosttyPasteEncoder encoder,
209209
*
210210
* @ingroup paste
211211
*/
212-
GhosttyResult ghostty_paste_encoder_encode(char* data,
212+
GhosttyResult ghostty_paste_encoder_encode(GhosttyPasteEncoder encoder,
213+
char* data,
213214
size_t len,
214-
GhosttyPasteEncoder encoder,
215215
char* out,
216216
size_t out_len,
217217
size_t* out_written);

src/terminal/c/paste.zig

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub fn encoder_set_bracketed(encoder_: Encoder, enabled: bool) callconv(.c) void
4242
}
4343

4444
pub fn encode(
45+
encoder_: Encoder,
4546
data_: ?[*]u8,
4647
len: usize,
47-
encoder_: Encoder,
4848
out_: ?[*]u8,
4949
out_len: usize,
5050
out_written_: ?*usize,
@@ -74,16 +74,6 @@ pub fn is_safe(data: ?[*]const u8, len: usize) callconv(.c) bool {
7474
return paste.isSafe(slice);
7575
}
7676

77-
test "alloc paste encoder" {
78-
const testing = std.testing;
79-
var e: Encoder = undefined;
80-
try testing.expectEqual(Result.success, new(
81-
&lib_alloc.test_allocator,
82-
&e,
83-
));
84-
free(e);
85-
}
86-
8777
test "is_safe with safe data" {
8878
const testing = std.testing;
8979
const safe = "hello world";
@@ -112,3 +102,83 @@ test "is_safe with null empty data" {
112102
const testing = std.testing;
113103
try testing.expect(is_safe(null, 0));
114104
}
105+
106+
test "encode bracketed" {
107+
const testing = std.testing;
108+
const alloc = testing.allocator;
109+
110+
var encoder: Encoder = undefined;
111+
try testing.expect(new(&lib_alloc.test_allocator, &encoder) == .success);
112+
encoder_set_bracketed(encoder, true);
113+
114+
const text: []u8 = try alloc.dupe(u8, "hello");
115+
defer alloc.free(text);
116+
117+
var out: [128]u8 = undefined;
118+
var out_len: usize = 0;
119+
120+
try testing.expect(encode(encoder, text.ptr, text.len, &out, out.len, &out_len) == .success);
121+
try testing.expectEqualStrings("\x1b[200~hello\x1b[201~", out[0 .. out_len - 1]);
122+
123+
free(encoder);
124+
}
125+
126+
test "encode unbracketed no newlines" {
127+
const testing = std.testing;
128+
const alloc = testing.allocator;
129+
130+
var encoder: Encoder = undefined;
131+
try testing.expect(new(&lib_alloc.test_allocator, &encoder) == .success);
132+
encoder_set_bracketed(encoder, false);
133+
134+
const text: []u8 = try alloc.dupe(u8, "hello");
135+
defer alloc.free(text);
136+
137+
var out: [128]u8 = undefined;
138+
var out_len: usize = 0;
139+
140+
try testing.expect(encode(encoder, text.ptr, text.len, &out, out.len, &out_len) == .success);
141+
try testing.expectEqualStrings("hello", out[0 .. out_len - 1]);
142+
143+
free(encoder);
144+
}
145+
146+
test "encode unbracketed newlines" {
147+
const testing = std.testing;
148+
const alloc = testing.allocator;
149+
150+
var encoder: Encoder = undefined;
151+
try testing.expect(new(&lib_alloc.test_allocator, &encoder) == .success);
152+
encoder_set_bracketed(encoder, false);
153+
154+
const text: []u8 = try alloc.dupe(u8, "hello\nworld");
155+
defer alloc.free(text);
156+
157+
var out: [128]u8 = undefined;
158+
var out_len: usize = 0;
159+
160+
try testing.expect(encode(encoder, text.ptr, text.len, &out, out.len, &out_len) == .success);
161+
try testing.expectEqualStrings("hello\rworld", out[0 .. out_len - 1]);
162+
163+
free(encoder);
164+
}
165+
166+
test "encode unbracketed windows-style newline" {
167+
const testing = std.testing;
168+
const alloc = testing.allocator;
169+
170+
var encoder: Encoder = undefined;
171+
try testing.expect(new(&lib_alloc.test_allocator, &encoder) == .success);
172+
encoder_set_bracketed(encoder, false);
173+
174+
const text: []u8 = try alloc.dupe(u8, "hello\r\nworld");
175+
defer alloc.free(text);
176+
177+
var out: [128]u8 = undefined;
178+
var out_len: usize = 0;
179+
180+
try testing.expect(encode(encoder, text.ptr, text.len, &out, out.len, &out_len) == .success);
181+
try testing.expectEqualStrings("hello\r\rworld", out[0 .. out_len - 1]);
182+
183+
free(encoder);
184+
}

0 commit comments

Comments
 (0)