tty: correctly handle partial reads
This commit is contained in:
parent
473357f24a
commit
11dcd099fa
1 changed files with 13 additions and 1 deletions
14
src/Tty.zig
14
src/Tty.zig
|
@ -144,12 +144,24 @@ pub fn run(
|
||||||
|
|
||||||
// initialize the read buffer
|
// initialize the read buffer
|
||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
|
var read_start: usize = 0;
|
||||||
// read loop
|
// read loop
|
||||||
while (!self.should_quit) {
|
while (!self.should_quit) {
|
||||||
const n = try posix.read(self.fd, &buf);
|
const n = try posix.read(self.fd, buf[read_start..]);
|
||||||
var start: usize = 0;
|
var start: usize = 0;
|
||||||
while (start < n) {
|
while (start < n) {
|
||||||
const result = try parser.parse(buf[start..n], paste_allocator);
|
const result = try parser.parse(buf[start..n], paste_allocator);
|
||||||
|
if (result.n == 0) {
|
||||||
|
// copy the read to the beginning. We don't use memcpy because
|
||||||
|
// this could be overlapping, and it's also rare
|
||||||
|
const initial_start = start;
|
||||||
|
while (start < n) : (start += 1) {
|
||||||
|
buf[start - initial_start] = buf[start];
|
||||||
|
}
|
||||||
|
read_start = start - initial_start + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
read_start = 0;
|
||||||
start += result.n;
|
start += result.n;
|
||||||
// TODO: if we get 0 byte read, copy the remaining bytes to the
|
// TODO: if we get 0 byte read, copy the remaining bytes to the
|
||||||
// beginning of the buffer and read mmore? this should only happen
|
// beginning of the buffer and read mmore? this should only happen
|
||||||
|
|
Loading…
Reference in a new issue