Skip to content

Commit 4f9e1be

Browse files
author
Luke Hudson
committed
fix: handle escaping of pipes as well as backslash itself p-e-w#65
1 parent 626ffaa commit 4f9e1be

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

argos@pew.worldwidemann.com/utilities.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,36 @@ export function parseFilename(filename) {
6969
return settings;
7070
}
7171

72+
function locatePipe(str) {
73+
let isEscaped = false;
74+
75+
for (let i = 0; i < str.length; i++) {
76+
if (isEscaped) {
77+
// Previous character was a backslash, reset state after current escaped character
78+
isEscaped = false;
79+
continue;
80+
}
81+
82+
if (str[i] === "\\") {
83+
// Enter escape state
84+
isEscaped = true;
85+
continue;
86+
}
87+
88+
if (str[i] === "|" && !isEscaped) {
89+
return i;
90+
}
91+
}
92+
93+
return -1;
94+
}
95+
7296
// Performs (mostly) BitBar-compatible output line parsing
7397
// (see https://github.com/matryer/bitbar#plugin-api)
7498
export function parseLine(lineString) {
7599
let line = {};
76-
let separatorIndex = undefined;
77-
78-
do
79-
{
80-
separatorIndex = lineString.indexOf("|", separatorIndex);
81-
if (separatorIndex === 0 || lineString[separatorIndex-1] != '\\') {
82-
// found a non-escaped pipe symbol
83-
break;
84-
}
85-
// keep looking
86-
++separatorIndex;
87-
} while(separatorIndex < lineString.length);
100+
101+
let separatorIndex = locatePipe(lineString);
88102

89103
if (separatorIndex >= 0) {
90104
let attributes = [];

0 commit comments

Comments
 (0)