Skip to content

Commit d4d6616

Browse files
committed
enter shows timestamp when enter is pressed
1 parent 2df7534 commit d4d6616

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

tail.js

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
11
const fs = require('fs');
22
const readline = require('readline');
33

4-
// Define the filename and number of lines
5-
const filename = process.argv[2];
6-
const numLines = parseInt(process.argv[3]) || 10;
4+
// Parse command line arguments
5+
const args = process.argv.slice(2);
6+
let numLines = 10; // default number of lines
7+
let filePath = null;
78

8-
if (!filename) {
9-
console.error('Usage: node tail.js <filename> [number of lines]');
10-
process.exit(1);
9+
for (let i = 0; i < args.length; i++) {
10+
if (args[i] === '-n' && i + 1 < args.length) {
11+
numLines = parseInt(args[i + 1]);
12+
i++; // skip the next argument since we used it
13+
} else if (!filePath) {
14+
filePath = args[i];
15+
}
1116
}
1217

13-
// Function to read the last N lines
14-
function readLastLines(file, numLines) {
15-
return new Promise((resolve, reject) => {
16-
const lines = [];
17-
const stream = fs.createReadStream(file, { encoding: 'utf8' });
18-
const rl = readline.createInterface({ input: stream });
18+
if (!filePath) {
19+
console.error('Usage: node tail.js <filename> [-n lines]');
20+
process.exit(1);
21+
}
1922

20-
rl.on('line', (line) => {
21-
lines.push(line);
22-
if (lines.length > numLines) {
23-
lines.shift();
24-
}
25-
});
23+
// Create interface for stdin/stdout
24+
const rl = readline.createInterface({
25+
input: process.stdin,
26+
output: process.stdout
27+
});
2628

27-
rl.on('close', () => resolve(lines));
28-
rl.on('error', reject);
29-
});
29+
// ANSI escape codes for red background and reset
30+
const RED_BG = '\x1b[41m';
31+
const RESET = '\x1b[0m';
32+
33+
// Function to read last N lines of a file
34+
function readLastLines(file, n) {
35+
try {
36+
const content = fs.readFileSync(file, 'utf8');
37+
const lines = content.split('\n');
38+
return lines.slice(-n).join('\n');
39+
} catch (err) {
40+
console.error(`Error reading file: ${err.message}`);
41+
process.exit(1);
42+
}
3043
}
3144

32-
// Function to follow the file
45+
// Function to follow file changes
3346
function followFile(file) {
34-
let fileSize = fs.statSync(file).size;
47+
let fileSize = fs.statSync(file).size;
3548

36-
fs.watch(file, (eventType) => {
37-
if (eventType === 'change') {
38-
const newFileSize = fs.statSync(file).size;
39-
if (newFileSize > fileSize) {
40-
const stream = fs.createReadStream(file, {
41-
encoding: 'utf8',
42-
start: fileSize,
43-
});
44-
45-
stream.on('data', (chunk) => {
46-
process.stdout.write(chunk);
47-
});
48-
49-
fileSize = newFileSize;
50-
}
51-
}
52-
});
49+
fs.watch(file, (eventType) => {
50+
if (eventType === 'change') {
51+
const newSize = fs.statSync(file).size;
52+
if (newSize > fileSize) {
53+
const stream = fs.createReadStream(file, {
54+
start: fileSize,
55+
encoding: 'utf8'
56+
});
57+
stream.on('data', (chunk) => process.stdout.write(chunk));
58+
fileSize = newSize;
59+
}
60+
}
61+
});
5362
}
5463

55-
// Read the last few lines and start following the file
56-
readLastLines(filename, numLines)
57-
.then((lines) => {
58-
console.log(lines.join('\n'));
59-
console.log('\n--- Start Following File ---');
60-
followFile(filename);
61-
})
62-
.catch((err) => {
63-
console.error(`Error: ${err.message}`);
64-
});
64+
// Show initial lines
65+
console.log(`Last ${numLines} lines of ${filePath}:`);
66+
console.log(readLastLines(filePath, numLines));
67+
console.log('\n--- Following file (Press Enter for timestamp, Ctrl+C to exit) ---');
68+
69+
// Start following the file
70+
followFile(filePath);
6571

72+
// Listen for Enter key press
73+
rl.on('line', () => {
74+
const now = new Date();
75+
const timestamp = now.toISOString();
76+
console.log(`${RED_BG}${timestamp}${RESET}`);
77+
});

0 commit comments

Comments
 (0)