Skip to content

Commit 1c8327b

Browse files
author
Ido Frenkel
committed
Add the option to search with regex
1 parent 3646bca commit 1c8327b

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/components/LazyLog/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ export interface LazyLogProps {
218218
* certain browsers/devices. Defaults to `100`.
219219
*/
220220
overscanRowCount?: number;
221+
/**
222+
* Use Regex for search
223+
*/
224+
regexSearch?: boolean;
221225
/**
222226
* A fixed row height in pixels. Controls how tall a line is,
223227
* as well as the `lineHeight` style of the line's text.
@@ -304,6 +308,7 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
304308
overflow: "initial",
305309
},
306310
caseInsensitive: false,
311+
regexSearch: false,
307312
enableGutters: false,
308313
enableHotKeys: false,
309314
enableLineNumbers: true,
@@ -732,11 +737,16 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
732737

733738
handleSearch = (keywords: string | undefined) => {
734739
const { resultLines, searchKeywords } = this.state;
735-
const { caseInsensitive, stream, websocket } = this.props;
740+
const { caseInsensitive, stream, websocket, regexSearch } = this.props;
736741
const currentResultLines =
737742
!stream && !websocket && keywords === searchKeywords
738743
? resultLines
739-
: searchLines(keywords, this.encodedLog!, caseInsensitive!);
744+
: searchLines(
745+
keywords,
746+
this.encodedLog!,
747+
caseInsensitive!,
748+
regexSearch!
749+
);
740750

741751
this.setState(
742752
{

src/components/Utils/search.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,56 @@ export const searchIndexes = (
6565
return results;
6666
};
6767

68+
/**
69+
* Search for a given pattern in the text
70+
*
71+
* @param rawKeywords - The Regex pattern to search.
72+
* @param {Uint8Array} rawLog - The log data to search within.
73+
* @returns {number[]} An array of indices where the keyword is found in the log.
74+
*/
75+
export const searchIndexesRegex = (
76+
rawKeywords: string | undefined,
77+
rawLog: Uint8Array,
78+
isCaseInsensitive: boolean
79+
) => {
80+
if (!rawKeywords) return [];
81+
82+
const decodedLog = new TextDecoder("utf-8").decode(rawLog);
83+
const indexes: number[] = [];
84+
85+
let flags = "g";
86+
if (isCaseInsensitive) {
87+
flags += "i";
88+
}
89+
90+
try {
91+
const regex = new RegExp(rawKeywords, flags);
92+
let match;
93+
94+
while ((match = regex.exec(decodedLog)) !== null) {
95+
indexes.push(match.index);
96+
}
97+
} catch (e) {
98+
return [];
99+
}
100+
101+
return indexes;
102+
};
103+
68104
/**
69105
* Searches for keywords within log lines, handling case sensitivity.
70106
*
71107
* @param {string | undefined} rawKeywords - The search term to look for.
72108
* @param {Uint8Array} rawLog - The log data to search within.
73109
* @param {boolean} isCaseInsensitive - Whether the search should be case-insensitive.
110+
* @param {boolean} regexSearch - Search with regex.
74111
* @returns {number[]} An array of line numbers where the keyword is found.
75112
*/
76113
export const searchLines = (
77114
rawKeywords: string | undefined,
78115
rawLog: Uint8Array,
79-
isCaseInsensitive: boolean
116+
isCaseInsensitive: boolean,
117+
regexSearch: boolean
80118
) => {
81119
let keywords = rawKeywords;
82120
let log = rawLog;
@@ -91,8 +129,9 @@ export const searchLines = (
91129
decodedLog = decodedLog.endsWith("\n") ? decodedLog : decodedLog + "\n";
92130
log = encode(decodedLog);
93131

94-
// Perform the search
95-
const results = searchIndexes(keywords, log);
132+
const results = regexSearch
133+
? searchIndexesRegex(keywords, log, isCaseInsensitive)
134+
: searchIndexes(keywords, log);
96135
const linesRanges = getLinesLengthRanges(log);
97136
const maxLineRangeIndex = linesRanges.length;
98137
const maxResultIndex = results.length;

src/stories/LazyLog.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Story = StoryObj<typeof LazyLog>;
1212

1313
const BaseStory = {
1414
caseInsensitive: true,
15+
regexSearch: false,
1516
enableGutters: false,
1617
enableHotKeys: true,
1718
enableLineNumbers: true,

0 commit comments

Comments
 (0)