Move formatLogContent to a utils file

This commit is contained in:
x032205
2025-11-01 03:47:03 -04:00
parent 0425a085fe
commit 7d978443c7
2 changed files with 48 additions and 48 deletions
@@ -4,52 +4,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { TPamSession } from "@app/hooks/api/pam";
import { PamSessionLogOutput } from "./PamSessionLogOutput";
const formatLogContent = (text: string | null | undefined): string => {
if (!text) return "";
let lines = text.split("\n");
// Find the first and last non-empty lines to trim vertical padding
let firstLineIndex = -1;
for (let i = 0; i < lines.length; i += 1) {
if (lines[i].trim() !== "") {
firstLineIndex = i;
break;
}
}
if (firstLineIndex === -1) {
return "";
}
let lastLineIndex = -1;
for (let i = lines.length - 1; i >= 0; i -= 1) {
if (lines[i].trim() !== "") {
lastLineIndex = i;
break;
}
}
lines = lines.slice(firstLineIndex, lastLineIndex + 1);
// Determine the minimum indentation of non-empty lines
const indentations = lines
.filter((line) => line.trim() !== "")
.map((line) => {
const match = line.match(/^\s*/);
return match ? match[0].length : 0;
});
const minIndentation = Math.min(...indentations);
// Remove the common indentation from all lines
if (minIndentation > 0) {
lines = lines.map((line) => line.substring(minIndentation));
}
return lines.join("\n");
};
import { formatLogContent } from "./PamSessionLogsSection.utils";
type Props = {
session: TPamSession;
@@ -77,7 +32,6 @@ export const PamSessionLogsSection = ({ session }: Props) => {
session.commandLogs.map((log) => {
const isExpanded = expandedLogTimestamps.has(log.timestamp);
const formattedInput = formatLogContent(log.input);
const formattedOutput = formatLogContent(log.output);
return (
<button
@@ -114,7 +68,7 @@ export const PamSessionLogsSection = ({ session }: Props) => {
<div className="h-px w-full bg-mineshaft-400"></div>
</div>
<div className="pt-2 text-bunker-300">
<PamSessionLogOutput content={formattedOutput} />
<PamSessionLogOutput content={log.output} />
</div>
</>
)}
@@ -0,0 +1,46 @@
// This function trims top and bottom empty padding, as well as moves all relative text to the left while still respecting indentation
export const formatLogContent = (text: string | null | undefined): string => {
if (!text) return "";
let lines = text.split("\n");
// Find the first and last non-empty lines to trim vertical padding
let firstLineIndex = -1;
for (let i = 0; i < lines.length; i += 1) {
if (lines[i].trim() !== "") {
firstLineIndex = i;
break;
}
}
if (firstLineIndex === -1) {
return "";
}
let lastLineIndex = -1;
for (let i = lines.length - 1; i >= 0; i -= 1) {
if (lines[i].trim() !== "") {
lastLineIndex = i;
break;
}
}
lines = lines.slice(firstLineIndex, lastLineIndex + 1);
// Determine the minimum indentation of non-empty lines
const indentations = lines
.filter((line) => line.trim() !== "")
.map((line) => {
const match = line.match(/^\s*/);
return match ? match[0].length : 0;
});
const minIndentation = Math.min(...indentations);
// Remove the common indentation from all lines
if (minIndentation > 0) {
lines = lines.map((line) => line.substring(minIndentation));
}
return lines.join("\n");
};