Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to search with regex #40

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/components/LazyLog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ export interface LazyLogProps {
* certain browsers/devices. Defaults to `100`.
*/
overscanRowCount?: number;
/**
* Use Regex for search
*/
regexSearch?: boolean;
/**
* A fixed row height in pixels. Controls how tall a line is,
* as well as the `lineHeight` style of the line's text.
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
45 changes: 42 additions & 3 deletions src/components/Utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,56 @@ 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,
isCaseInsensitive: boolean
) => {
if (!rawKeywords) return [];

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

let flags = "g";
if (isCaseInsensitive) {
flags += "i";
}

try {
const regex = new RegExp(rawKeywords, flags);
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 +129,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, isCaseInsensitive)
: 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