Skip to content

Commit

Permalink
Add the option to search with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido Frenkel committed Aug 29, 2024
1 parent 3646bca commit 9fdbab7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/LazyLog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export interface LazyLogProps {
* Enable hyperlinks to be discovered in log text and made clickable links. Default is false.
*/
enableLinks?: boolean;
/**
* Use Regex for search
*/
regexSearch?: boolean;
/**
* Enable the search feature.
*/
Expand Down Expand Up @@ -304,6 +308,7 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
overflow: "initial",
},
caseInsensitive: false,
regexSearch: false,
enableGutters: false,
enableHotKeys: false,
enableLineNumbers: true,
Expand Down Expand Up @@ -732,11 +737,16 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {

handleSearch = (keywords: string | undefined) => {
const { resultLines, searchKeywords } = this.state;
const { caseInsensitive, stream, websocket } = this.props;
const { caseInsensitive, stream, websocket, regexSearch } = this.props;
const currentResultLines =
!stream && !websocket && keywords === searchKeywords
? resultLines
: searchLines(keywords, this.encodedLog!, caseInsensitive!);
: searchLines(
keywords,
this.encodedLog!,
caseInsensitive!,
regexSearch!
);

this.setState(
{
Expand Down
39 changes: 36 additions & 3 deletions src/components/Utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,50 @@ export const searchIndexes = (
return results;
};

/**
* Search for a given pattern in the text
*
* @param rawKeywords - The Regex pattern to search.
* @param {Uint8Array} rawLog - The log data to search within.
* @returns {number[]} An array of indices where the keyword is found in the log.
*/
export const searchIndexesRegex = (
rawKeywords: string | undefined,
rawLog: Uint8Array
) => {
if (!rawKeywords) return [];

const decodedLog = new TextDecoder("utf-8").decode(rawLog);
const indexes: number[] = [];

try {
const regex = new RegExp(rawKeywords, "g");
let match;

while ((match = regex.exec(decodedLog)) !== null) {
indexes.push(match.index);
}
} catch (e) {
return [];
}

return indexes;
};

/**
* Searches for keywords within log lines, handling case sensitivity.
*
* @param {string | undefined} rawKeywords - The search term to look for.
* @param {Uint8Array} rawLog - The log data to search within.
* @param {boolean} isCaseInsensitive - Whether the search should be case-insensitive.
* @param {boolean} regexSearch - Search with regex.
* @returns {number[]} An array of line numbers where the keyword is found.
*/
export const searchLines = (
rawKeywords: string | undefined,
rawLog: Uint8Array,
isCaseInsensitive: boolean
isCaseInsensitive: boolean,
regexSearch: boolean
) => {
let keywords = rawKeywords;
let log = rawLog;
Expand All @@ -91,8 +123,9 @@ export const searchLines = (
decodedLog = decodedLog.endsWith("\n") ? decodedLog : decodedLog + "\n";
log = encode(decodedLog);

// Perform the search
const results = searchIndexes(keywords, log);
const results = regexSearch
? searchIndexesRegex(keywords, log)
: searchIndexes(keywords, log);
const linesRanges = getLinesLengthRanges(log);
const maxLineRangeIndex = linesRanges.length;
const maxResultIndex = results.length;
Expand Down
1 change: 1 addition & 0 deletions src/stories/LazyLog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Story = StoryObj<typeof LazyLog>;

const BaseStory = {
caseInsensitive: true,
regexSearch: false,
enableGutters: false,
enableHotKeys: true,
enableLineNumbers: true,
Expand Down

0 comments on commit 9fdbab7

Please sign in to comment.