From c8a20cadbfd539d185d32f4558916825fcf265ba Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 1 May 2024 11:28:32 +0100 Subject: [PATCH] fix: Correct grep output when asking for line numbers A couple of issues here: - We didn't pass the line number to do_grep_line() so `i` was undefined - Operator precedence messed with the ternary so when line numbers were requested, the line wouldn't be output. Found thanks to this now-solved eslint issue: /puter/packages/phoenix/src/puter-shell/coreutils/grep.js 100:60 error 'i' is not defined no-undef --- .../phoenix/src/puter-shell/coreutils/grep.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/phoenix/src/puter-shell/coreutils/grep.js b/packages/phoenix/src/puter-shell/coreutils/grep.js index 29351e1c..eb183144 100644 --- a/packages/phoenix/src/puter-shell/coreutils/grep.js +++ b/packages/phoenix/src/puter-shell/coreutils/grep.js @@ -78,7 +78,7 @@ export default { } } - const do_grep_line = async ( line ) => { + const do_grep_line = async ( line, lineNumber ) => { if ( line.endsWith('\n') ) line = line.slice(0, -1); const re = new RegExp( pattern, @@ -97,10 +97,9 @@ export default { ); if ( lxor(values['invert-match'], re.test(line)) ) { - const lineNumber = values['line-number'] ? i + 1 : ''; - const lineToPrint = - lineNumber ? lineNumber + ':' : '' + - line; + const lineToPrint = values['line-number'] + ? `${lineNumber + 1}:${line}` + : line; console.log(`LINE{${lineToPrint}}`); await ctx.externs.out.write(lineToPrint + '\n'); @@ -111,7 +110,7 @@ export default { for ( let i=0 ; i < lines.length ; i++ ) { const line = lines[i]; - await do_grep_line(line); + await do_grep_line(line, i); } } @@ -139,10 +138,10 @@ export default { for ( let file of files ) { if ( file === '-' ) { - for ( ;; ) { + for ( let i = 0; ; i++) { const { value, done } = await ctx.externs.in_.read(); if ( done ) break; - await do_grep_line(value); + await do_grep_line(value, i); } } else { file = resolveRelativePath(ctx.vars, file);