Skip to content

Commit 7864ad8

Browse files
Empactneekey
authored andcommitted
fix(pencil): Fix repeated headers to be bounded properly
Previously, the field lookup occurred from the beginning of the line so would repeated find the same line location if the same string or a substring of a header was searched more than once. Instead track the current end index of the previous field to base the search state location on.
1 parent 4c3d3ed commit 7864ad8

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ module.exports.parse = function (output) {
4444
// Treat the first line as the title fields line
4545
if (index == 0) {
4646
var fields = line.split(/\s+/);
47+
var currentIndex = 0;
4748

4849
// record the beginning and ending for each field
4950
fields.forEach(function (field, idx) {
5051

5152
if (field) {
5253
var info = titleInfo[field] = {};
53-
var indexBegin = line.indexOf(field);
54-
var indexEnd = indexBegin + field.length;
54+
var indexBegin = line.indexOf(field, currentIndex);
55+
var indexEnd = currentIndex = (indexBegin + field.length);
5556

5657
if (idx == 0) {
5758
info.titleBegin = 0;

test/output/ps-repeated-header.log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PPID PID COMMAND
2+
1 338 /usr/sbin/distnoted agent
3+
1 341 /usr/libexec/trustd --agent
4+
1 342 /usr/libexec/lsd

test/spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ describe('tabler-parser', function () {
4444
Assert.deepEqual(result, expectResult);
4545
});
4646

47+
it('with a repeated header', function () {
48+
var output = GetOutput('ps-repeated-header.log');
49+
var result = TableParser.parse(output);
50+
51+
var expectResult = [
52+
{
53+
'PID': ['338'],
54+
'PPID': ['1'],
55+
'COMMAND': [
56+
'/usr/sbin/distnoted',
57+
'agent'
58+
]
59+
},
60+
{
61+
'PID': ['341'],
62+
'PPID': ['1'],
63+
'COMMAND': [
64+
'/usr/libexec/trustd',
65+
'--agent'
66+
]
67+
},
68+
{
69+
'PID': ['342'],
70+
'PPID': ['1'],
71+
'COMMAND': ['/usr/libexec/lsd']
72+
},
73+
];
74+
75+
Assert.deepEqual(result, expectResult);
76+
});
77+
4778
it('a windows output', function () {
4879
var output = GetOutput('wmic.log');
4980
var result = TableParser.parse(output);

0 commit comments

Comments
 (0)