Skip to content

Commit

Permalink
fix: Correct grep output when asking for line numbers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AtkinsSJ committed May 2, 2024
1 parent 71f8afa commit c8a20ca
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/phoenix/src/puter-shell/coreutils/grep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c8a20ca

Please sign in to comment.