Skip to content

frontend: logViewer: Add toggle button for word break #3039

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
71 changes: 68 additions & 3 deletions frontend/src/components/common/LogViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function LogViewer(props: LogViewerProps) {
const searchAddonRef = React.useRef<any>(null);
const [terminalContainerRef, setTerminalContainerRef] = React.useState<HTMLElement | null>(null);
const [showSearch, setShowSearch] = React.useState(false);
const [isWordBreakEnabled, setIsWordBreakEnabled] = React.useState(true);

useHotkeys('ctrl+shift+f', () => {
setShowSearch(true);
Expand All @@ -64,6 +65,42 @@ export function LogViewer(props: LogViewerProps) {
element.click();
}

function wordBreakText(logs: string[], terminalWidth: number): string {
const wordBreakLogs: string[] = [];

logs.forEach(originalLine => {
const line = originalLine;

const subLine = line.split('\n');

subLine.forEach((part, index) => {
let newPart = part;

while (newPart.length > terminalWidth) {
let newLineIndex = terminalWidth;
while (newPart[newLineIndex] !== ' ') {
newLineIndex--;
}

const newPartSlice = newPart.slice(0, newLineIndex) + '\n';
wordBreakLogs.push(newPartSlice);

newPart = newPart.slice(newLineIndex + 1);
}

if (index < subLine.length - 1) {
newPart += '\n';
}

wordBreakLogs.push(newPart);
});
});

const newLogs = wordBreakLogs;

return newLogs.join('').replaceAll('\n', '\r\n');
}

React.useEffect(() => {
if (!terminalContainerRef || !!xtermRef.current) {
return;
Expand Down Expand Up @@ -97,6 +134,8 @@ export function LogViewer(props: LogViewerProps) {
const pageResizeHandler = () => {
fitAddonRef.current!.fit();
console.debug('resize');
xtermRef.current?.clear();
xtermRef.current?.write(getJointLogs());
};
window.addEventListener('resize', pageResizeHandler);

Expand All @@ -106,7 +145,7 @@ export function LogViewer(props: LogViewerProps) {
searchAddonRef.current?.dispose();
xtermRef.current = null;
};
}, [terminalContainerRef, xtermRef.current]);
}, [terminalContainerRef, xtermRef.current, isWordBreakEnabled]);

React.useEffect(() => {
if (!xtermRef.current) {
Expand All @@ -122,10 +161,25 @@ export function LogViewer(props: LogViewerProps) {
xtermRef.current?.write(getJointLogs());

return function cleanup() {};
}, [logs, xtermRef]);
}, [logs, xtermRef, isWordBreakEnabled]);

function getJointLogs() {
return logs?.join('').replaceAll('\n', '\r\n');
fitAddonRef.current!.fit();
const terminalWidth = xtermRef.current?.cols || 80;
if (isWordBreakEnabled) {
return wordBreakText(logs, terminalWidth);
} else {
return logs?.join('').replaceAll('\n', '\r\n');
}
}

function handleWordBreakText() {
setIsWordBreakEnabled(!isWordBreakEnabled);
xtermRef.current?.clear();
xtermRef.current?.write(getJointLogs());
setTimeout(() => {
fitAddonRef.current!.fit();
}, 1);
}

return (
Expand Down Expand Up @@ -170,6 +224,17 @@ export function LogViewer(props: LogViewerProps) {
</Grid>
))}
</Grid>
<Grid item xs>
<ActionButton
description={
isWordBreakEnabled
? t('translation|Disable Word Break')
: t('translation|Enable Word Break')
}
onClick={handleWordBreakText}
icon={isWordBreakEnabled ? 'mdi:wrap' : 'mdi:wrap-disabled'}
/>
</Grid>
<Grid item xs>
<ActionButton
description={t('translation|Find')}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Head back <1>home</1>.",
"Error Details": "",
"Copy": "",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "Finden",
"Download": "Herunterladen",
"No results": "Keine Ergebnisse",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Head back <1>home</1>.",
"Error Details": "Error Details",
"Copy": "Copy",
"Disable Word Break": "Disable Word Break",
"Enable Word Break": "Enable Word Break",
"Find": "Find",
"Download": "Download",
"No results": "No results",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Head back <1>home</1>.",
"Error Details": "Detalles del error",
"Copy": "Copiar",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "Buscar",
"Download": "Descargar",
"No results": "Sin resultados",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Head back <1>home</1>.",
"Error Details": "",
"Copy": "",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "Trouver",
"Download": "Télécharger",
"No results": "Aucun résultat",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/it/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Torna alla <1>home</1>.",
"Error Details": "Dettagli errore",
"Copy": "Copia",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "Trova",
"Download": "Scarica",
"No results": "Nessun risultato",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/pt/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "Voltar ao <1>início</1>.",
"Error Details": "Detalhes do erro",
"Copy": "Copiar",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "Pesquisar",
"Download": "Descarregar",
"No results": "Sem resultados",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/i18n/locales/zh-tw/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
"Head back <1>home</1>.": "返回<1>首頁</1>。",
"Error Details": "錯誤詳情",
"Copy": "複製",
"Disable Word Break": "",
"Enable Word Break": "",
"Find": "查詢",
"Download": "下載",
"No results": "無結果",
Expand Down
Loading