Skip to content

Commit

Permalink
fix(tauri-utils): include \n in io::read_line, closes #6388 (#6519)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <[email protected]>
fix(tauri-utils): include `\n` in `io::read_line`, closes #6388
  • Loading branch information
amrbashir and lucasfernog committed Aug 8, 2023
1 parent 9edebbb commit a6b52e4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-utils-read-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'patch:bug'
---

Fix `io::read_line` not including the new line character `\n`.
6 changes: 2 additions & 4 deletions core/tauri-utils/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use std::io::BufRead;

/// Read a line breaking in both \n and \r.
/// Read all bytes until a newline (the `0xA` byte) or a carriage return (`\r`) is reached, and append them to the provided buffer.
///
/// Adapted from <https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line>.
pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
Expand All @@ -16,6 +16,7 @@ pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::
let available = match r.fill_buf() {
Ok(n) => n,
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,

Err(e) => return Err(e),
};
match memchr::memchr(b'\n', available) {
Expand All @@ -40,9 +41,6 @@ pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::
r.consume(used);
read += used;
if done || used == 0 {
if buf.ends_with(&[b'\n']) {
buf.pop();
}
return Ok(read);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/api/process/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ mod test {
assert_eq!(payload.code, Some(1));
}
CommandEvent::Stderr(line) => {
assert_eq!(line, "cat: test/api/: Is a directory".to_string());
assert_eq!(line, "cat: test/api/: Is a directory\n".to_string());
}
_ => {}
}
Expand Down
89 changes: 45 additions & 44 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

33 changes: 29 additions & 4 deletions examples/api/src/views/Shell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
let encoding = ''
let stdin = ''
let child
let output = []
function _getEnv() {
return env.split(' ').reduce((env, clause) => {
Expand All @@ -26,10 +27,11 @@
function spawn() {
child = null
output = []
const command = new Command(cmd, [...args, script], {
cwd: cwd || null,
env: _getEnv(),
encoding,
encoding: encoding || null
})
command.on('close', (data) => {
Expand All @@ -40,8 +42,23 @@
})
command.on('error', (error) => onMessage(`command error: "${error}"`))
command.stdout.on('data', (line) => onMessage(`command stdout: "${line}"`))
command.stderr.on('data', (line) => onMessage(`command stderr: "${line}"`))
function onOutput(line, kind) {
onMessage(`command ${kind}: "${line}"`)
if (line.endsWith('\n')) {
line = line.substring(0, line.length - 1)
}
const last = output[output.length - 1]
if (last && last.endsWith('\r')) {
output = [...output.slice(0, output.length - 1), line]
} else {
output = [...output, line]
}
}
command.stdout.on('data', (line) => onOutput(line, 'stdout'))
command.stderr.on('data', (line) => onOutput(line, 'stderr'))
command
.spawn()
Expand Down Expand Up @@ -90,11 +107,19 @@
</div>
<div class="flex children:grow gap-1">
<button class="btn" on:click={spawn}>Run</button>
<button class="btn" on:click={kill}>Kill</button>
{#if child}
<button class="btn" on:click={kill}>Kill</button>
{/if}
</div>
{#if child}
<br />
<input class="input" placeholder="write to stdin" bind:value={stdin} />
<button class="btn" on:click={writeToStdin}>Write</button>
{/if}

<div>
{#each output as l}
<p>{l}</p>
{/each}
</div>
</div>

0 comments on commit a6b52e4

Please sign in to comment.