-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(panel/console): added search bar
- Loading branch information
Showing
5 changed files
with
303 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
import { Input } from "@/components/ui/input"; | ||
import { cn } from "@/lib/utils"; | ||
import type { ISearchDecorationOptions, ISearchOptions, SearchAddon } from "@xterm/addon-search"; | ||
import { ArrowDownIcon, ArrowUpIcon, CaseSensitiveIcon, RegexIcon, WholeWordIcon, XIcon } from "lucide-react"; | ||
import { ReactNode, useEffect, useRef, useState } from "react"; | ||
|
||
|
||
type ButtonProps = { | ||
title?: string; | ||
onClick: () => void; | ||
isActive?: boolean; | ||
children: ReactNode; | ||
}; | ||
|
||
function SearchBarButton({ title, onClick, isActive, children }: ButtonProps) { | ||
return ( | ||
<button | ||
title={title} | ||
className={cn( | ||
"rounded p-0.5", | ||
"hover:bg-secondary-foreground hover:text-secondary", | ||
"focus:outline-none focus:ring-1 focus:ring-secondary-foreground focus:ring-offset-1x focus:ring-offset-secondary-foreground", | ||
isActive && 'bg-muted-foreground text-secondary' | ||
)} | ||
onClick={onClick} | ||
> | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
|
||
const labelNoResults = 'No results'; | ||
const xtermDecorations = { | ||
activeMatchBackground: '#FF00DC', | ||
activeMatchColorOverviewRuler: '#FF00DC', | ||
matchBackground: '#732268', | ||
matchOverviewRuler: '#732268', | ||
} satisfies ISearchDecorationOptions; | ||
|
||
type LiveConsoleSearchBarProps = { | ||
show: boolean; | ||
setShow: (show: boolean) => void; | ||
searchAddon: SearchAddon; | ||
}; | ||
|
||
export default function LiveConsoleSearchBar({ show, setShow, searchAddon }: LiveConsoleSearchBarProps) { | ||
const [caseSensitive, setCaseSensitive] = useState(false); | ||
const [wholeWord, setWholeWord] = useState(false); | ||
const [regex, setRegex] = useState(false); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const [resultCount, setResultCount] = useState(labelNoResults); | ||
|
||
//helpers | ||
const clearSearchState = (newStatus?: string) => { | ||
searchAddon.clearDecorations(); | ||
if (newStatus) { | ||
setResultCount(newStatus); | ||
} | ||
} | ||
const getSearchOptions = (overrides?: Partial<ISearchOptions>): ISearchOptions => ({ | ||
decorations: xtermDecorations, | ||
caseSensitive, | ||
wholeWord, | ||
regex, | ||
...overrides, | ||
}) | ||
|
||
//autofocus the input | ||
useEffect(() => { | ||
if (show) { | ||
inputRef.current?.focus(); | ||
} else { | ||
clearSearchState(labelNoResults); | ||
} | ||
}, [show]); | ||
|
||
//listens to the result count change | ||
useEffect(() => { | ||
if (!searchAddon) return; | ||
const dispose = searchAddon.onDidChangeResults(({ resultIndex, resultCount }) => { | ||
if (resultIndex === -1) { | ||
setResultCount(labelNoResults); | ||
} else { | ||
setResultCount(`${resultIndex + 1}/${resultCount}`); | ||
} | ||
}); | ||
return () => { | ||
dispose.dispose(); | ||
} | ||
}, []); | ||
|
||
//Handlers | ||
const handlePrevious = () => { | ||
if (!inputRef.current) return; | ||
console.log('backward search for', inputRef.current.value); | ||
searchAddon.findPrevious(inputRef.current.value, getSearchOptions()); | ||
} | ||
const handleNext = () => { | ||
if (!inputRef.current) return; | ||
console.log('forward search for', inputRef.current.value); | ||
searchAddon.findNext(inputRef.current.value, getSearchOptions()); | ||
} | ||
|
||
const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (!inputRef.current) return; | ||
if ((e.key === 'Enter' || e.key === 'F3') && !e.shiftKey) { | ||
handleNext(); | ||
e.preventDefault(); | ||
} else if ((e.key === 'Enter' || e.key === 'F3') && e.shiftKey) { | ||
handlePrevious(); | ||
e.preventDefault(); | ||
} else if (e.key === 'Escape') { | ||
setShow(false); | ||
clearSearchState(labelNoResults); | ||
} | ||
} | ||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (!inputRef.current) return; | ||
handleNext(); | ||
} | ||
|
||
const handleCaseSensitiveMode = () => { | ||
if (!inputRef.current) return; | ||
setCaseSensitive(!caseSensitive); | ||
clearSearchState(); | ||
searchAddon.findNext(inputRef.current.value, getSearchOptions({ caseSensitive: !caseSensitive })); | ||
} | ||
const handleWholeWordMode = () => { | ||
if (!inputRef.current) return; | ||
setWholeWord(!wholeWord); | ||
clearSearchState(); | ||
searchAddon.findNext(inputRef.current.value, getSearchOptions({ wholeWord: !wholeWord })); | ||
} | ||
const handleRegexMode = () => { | ||
if (!inputRef.current) return; | ||
setRegex(!regex); | ||
clearSearchState(); | ||
searchAddon.findNext(inputRef.current.value, getSearchOptions({ regex: !regex })); | ||
} | ||
|
||
if (!show) return null; | ||
return ( | ||
<div className="absolute top-0 xs:right-4 bg-secondary border z-10 flex items-center justify-center gap-1 xs:gap-4 shadow-xl p-1 rounded-b-lg border-t-0 w-full xs:w-auto flex-wrap"> | ||
<div className="relative"> | ||
<Input | ||
ref={inputRef} | ||
id="playerlistFilter" | ||
className="h-8" | ||
placeholder="Search string" | ||
onKeyDown={handleInputKeyDown} | ||
onChange={handleInputChange} | ||
onBlur={() => { searchAddon.clearActiveDecoration() }} | ||
/> | ||
<div className="absolute top-1/2 right-1 transform -translate-y-1/2 flex text-muted-foreground gap-2"> | ||
<SearchBarButton | ||
title="Case Sensitive" | ||
isActive={caseSensitive} | ||
onClick={handleCaseSensitiveMode} | ||
> | ||
<CaseSensitiveIcon className="h-5 w-5" /> | ||
</SearchBarButton> | ||
<SearchBarButton | ||
title="Whole Word" | ||
isActive={wholeWord} | ||
onClick={handleWholeWordMode} | ||
> | ||
<WholeWordIcon className="h-5 w-5" /> | ||
</SearchBarButton> | ||
<SearchBarButton | ||
title="Regex" | ||
isActive={regex} | ||
onClick={handleRegexMode} | ||
> | ||
<RegexIcon className="h-4 w-5" /> | ||
</SearchBarButton> | ||
</div> | ||
</div> | ||
<div className="flex grow text-sm text-muted-foreground whitespace-nowrap min-w-[8ch]"> | ||
{resultCount} | ||
</div> | ||
<div className="flex gap-2 text-muted-foreground"> | ||
<SearchBarButton | ||
title="Previous" | ||
onClick={handlePrevious} | ||
> | ||
<ArrowUpIcon className="h-5 w-5" /> | ||
</SearchBarButton> | ||
<SearchBarButton | ||
title="Next" | ||
onClick={handleNext} | ||
> | ||
<ArrowDownIcon className="h-5 w-5" /> | ||
</SearchBarButton> | ||
<SearchBarButton | ||
title="Close" | ||
onClick={() => { setShow(false) }} | ||
> | ||
<XIcon className="h-5 w-5" /> | ||
</SearchBarButton> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ITerminalInitOnlyOptions, ITerminalOptions, ITheme } from "@xterm/xterm"; | ||
|
||
//From legacy systemLog.ejs, based on the ANSI-UP colors | ||
//TODO: at component instantiation, grab those as variables from the CSS | ||
// putting css variables here will not work (i think) | ||
const baseTheme: ITheme = { | ||
background: '#222326', //card bg | ||
foreground: '#F8F8F8', | ||
black: '#000000', | ||
brightBlack: '#555555', | ||
red: '#D62341', | ||
brightRed: '#FF5370', | ||
green: '#9ECE58', | ||
brightGreen: '#C3E88D', | ||
yellow: '#FAED70', | ||
brightYellow: '#FFCB6B', | ||
blue: '#396FE2', | ||
brightBlue: '#82AAFF', | ||
magenta: '#BB80B3', | ||
brightMagenta: '#C792EA', | ||
cyan: '#2DDAFD', | ||
brightCyan: '#89DDFF', | ||
white: '#D0D0D0', | ||
brightWhite: '#FFFFFF', | ||
}; | ||
|
||
const terminalOptions: ITerminalOptions | ITerminalInitOnlyOptions = { | ||
theme: baseTheme, | ||
convertEol: true, | ||
cursorBlink: true, | ||
cursorStyle: 'bar', | ||
disableStdin: true, | ||
drawBoldTextInBrightColors: false, | ||
fontFamily: "JetBrains Mono Variable, monospace", | ||
fontSize: 13, | ||
fontWeight: "300", | ||
letterSpacing: 0.8, | ||
scrollback: 5000, | ||
// scrollback: 2500, //more or less equivalent to the legacy 250kb limit | ||
allowProposedApi: true, | ||
allowTransparency: true, | ||
overviewRulerWidth: 15, | ||
}; | ||
|
||
export default terminalOptions; |
Oops, something went wrong.