Skip to content

Commit fafae17

Browse files
Improve comments and enhance output formatting in 'ls' command
1 parent 7b1d67d commit fafae17

File tree

1 file changed

+9
-5
lines changed
  • implement-shell-tools/ls

1 file changed

+9
-5
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function sortEntries(entries) {
5656
}
5757

5858

59-
// print entries either one per line (-1 flag)
59+
// print each entry on its own line (used for -1 flag)
6060
function printEntries(entries) {
6161
entries.forEach(entry => console.log(entry));
6262
}
@@ -76,14 +76,18 @@ async function newLs(directory, oneFlag, allFlag) {
7676
// reads directory contents
7777
const entries = await fs.readdir(directory);
7878

79-
// Filter out hidden files if no -a flag
79+
// filter out hidden files if no -a flag
8080
const filteredEntries = filterFiles(entries, allFlag);
8181

82-
// Sort the entries using the sortEntries helper
82+
// sort the entries using the sortEntries helper
8383
const sortedEntries = sortEntries(filteredEntries);
8484

85-
// print entries for -1 flag (one per line)
86-
printEntries(sortedEntries);
85+
// print entries based on -1 flag
86+
if (oneFlag) {
87+
printEntries(sortedEntries); // one per line
88+
} else {
89+
console.log(sortedEntries.join(" ")); // all on one line, separated by spaces
90+
}
8791
} catch (err) {
8892
console.error(`ls: cannot access '${directory}': ${err.message}`);
8993
}

0 commit comments

Comments
 (0)