Skip to content

Commit dfeb166

Browse files
authored
feat: add a newline after commands that don't end their output with one (#810)
* feat: add a newline after commands that don't end their output with one * Resolves #776 Signed-off-by: chankruze <chankruze@gmail.com> * fix: optimize eol check Signed-off-by: chankruze <chankruze@gmail.com> * chore: encapsulate eol check and add unit test Signed-off-by: chankruze <chankruze@gmail.com> --------- Signed-off-by: chankruze <chankruze@gmail.com>
1 parent 3d24076 commit dfeb166

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/core.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
quote,
3939
quotePowerShell,
4040
noquote,
41+
ensureEol,
4142
} from './util.js'
4243

4344
export interface Shell {
@@ -651,7 +652,7 @@ export function log(entry: LogEntry) {
651652
case 'stdout':
652653
case 'stderr':
653654
if (!entry.verbose) return
654-
process.stderr.write(entry.data)
655+
process.stderr.write(ensureEol(entry.data))
655656
break
656657
case 'cd':
657658
if (!$.verbose) return

src/util.ts

+8
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,11 @@ export function getCallerLocationFromString(stackString = 'unknown') {
411411
?.trim() || stackString
412412
)
413413
}
414+
415+
export function ensureEol(buffer: Buffer): Buffer {
416+
if (buffer.toString('utf8', buffer.length - 1) !== '\n') {
417+
return Buffer.concat([buffer, Buffer.from('\n')])
418+
}
419+
420+
return buffer
421+
}

test/util.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
getCallerLocationFromString,
3030
tempdir,
3131
tempfile,
32+
ensureEol,
3233
} from '../build/util.js'
3334

3435
describe('util', () => {
@@ -165,3 +166,20 @@ test('tempfile() creates temporary files', () => {
165166
assert.match(tf, /\/zx-.+\/bar\.txt$/)
166167
assert.equal(fs.readFileSync(tf, 'utf-8'), 'bar')
167168
})
169+
170+
test('ensureEol() should ensure buffer ends with a newline character', () => {
171+
// Test case 1: Buffer without newline
172+
const buffer1 = Buffer.from('Hello, world!')
173+
const result1 = ensureEol(buffer1).toString()
174+
assert.strictEqual(result1, 'Hello, world!\n')
175+
176+
// Test case 2: Buffer with newline
177+
const buffer2 = Buffer.from('Hello, world!\n')
178+
const result2 = ensureEol(buffer2).toString()
179+
assert.strictEqual(result2, 'Hello, world!\n')
180+
181+
// Test case 3: Empty buffer
182+
const buffer3 = Buffer.from('')
183+
const result3 = ensureEol(buffer3).toString()
184+
assert.strictEqual(result3, '\n')
185+
})

0 commit comments

Comments
 (0)