mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-10 21:29:20 +00:00
Move formatLogContent to a utils file
This commit is contained in:
@@ -4,52 +4,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|||||||
|
|
||||||
import { TPamSession } from "@app/hooks/api/pam";
|
import { TPamSession } from "@app/hooks/api/pam";
|
||||||
import { PamSessionLogOutput } from "./PamSessionLogOutput";
|
import { PamSessionLogOutput } from "./PamSessionLogOutput";
|
||||||
|
import { formatLogContent } from "./PamSessionLogsSection.utils";
|
||||||
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");
|
|
||||||
};
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
session: TPamSession;
|
session: TPamSession;
|
||||||
@@ -77,7 +32,6 @@ export const PamSessionLogsSection = ({ session }: Props) => {
|
|||||||
session.commandLogs.map((log) => {
|
session.commandLogs.map((log) => {
|
||||||
const isExpanded = expandedLogTimestamps.has(log.timestamp);
|
const isExpanded = expandedLogTimestamps.has(log.timestamp);
|
||||||
const formattedInput = formatLogContent(log.input);
|
const formattedInput = formatLogContent(log.input);
|
||||||
const formattedOutput = formatLogContent(log.output);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
@@ -114,7 +68,7 @@ export const PamSessionLogsSection = ({ session }: Props) => {
|
|||||||
<div className="h-px w-full bg-mineshaft-400"></div>
|
<div className="h-px w-full bg-mineshaft-400"></div>
|
||||||
</div>
|
</div>
|
||||||
<div className="pt-2 text-bunker-300">
|
<div className="pt-2 text-bunker-300">
|
||||||
<PamSessionLogOutput content={formattedOutput} />
|
<PamSessionLogOutput content={log.output} />
|
||||||
</div>
|
</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");
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user