Skip to content

Commit 2325669

Browse files
committed
feat: python/java formats + wrap option
1 parent 69e660b commit 2325669

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"highlight.js": "^11.7.0",
6969
"iarna-toml-esm": "^3.0.5",
7070
"ibantools": "^4.3.3",
71+
"javastack.js": "^1.0.2",
7172
"js-base64": "^3.7.6",
7273
"json5": "^2.2.3",
7374
"jsstack.js": "^2.0.0",
@@ -87,6 +88,7 @@
8788
"pdf-signature-reader": "^1.4.2",
8889
"pinia": "^2.0.34",
8990
"plausible-tracker": "^0.3.8",
91+
"pythonstack.js": "^1.0.2",
9092
"qrcode": "^1.5.1",
9193
"randexp": "^0.5.3",
9294
"sql-formatter": "^13.0.0",

pnpm-lock.yaml

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "javastack.js" {
2+
export default function javastack(selector: string, options: { prettyprint: boolean }) : void;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "pythonstack.js" {
2+
export default function pythonstack(selector: string, options: { prettyprint: boolean }) : void;
3+
}

src/tools/stacktrace-prettier/stacktrace-prettier.vue

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script setup lang="ts">
22
import JSStack from 'jsstack.js';
3+
import JavaStack from 'javastack.js';
4+
import PythonStack from 'pythonstack.js';
35
import NetStack from 'netstack.js';
46
import domtoimage from 'dom-to-image-more';
57
import { useQueryParamOrStorage } from '@/composable/queryParams';
@@ -8,7 +10,7 @@ import { useCopy } from '@/composable/copy';
810
911
const styleStore = useStyleStore();
1012
11-
const stackType = useQueryParamOrStorage<'net' | 'js'>({ name: 'type', storageName: 'stackfmt:type', defaultValue: 'net' });
13+
const stackType = useQueryParamOrStorage<'net' | 'js' | 'java' | 'python'>({ name: 'type', storageName: 'stackfmt:type', defaultValue: 'net' });
1214
const stackTrace = ref('');
1315
const formatedStackTrace = ref<HTMLElement>();
1416
@@ -17,13 +19,28 @@ watchEffect(() => {
1719
if (!formatedStackTrace.value) {
1820
return;
1921
}
22+
let cleanedStackTrace;
23+
try {
24+
cleanedStackTrace = JSON.parse(stackTrace.value);
25+
}
26+
catch (_) {
27+
cleanedStackTrace = stackTrace.value.replace(/(\\r)?\\n/g, '\n').replace(/(?:^['"])|(?:['"]$)/, '');
28+
};
2029
try {
2130
if (stackType.value === 'js') {
22-
formatedStackTrace.value.textContent = stackTrace.value;
31+
formatedStackTrace.value.textContent = cleanedStackTrace;
2332
JSStack('.stacktrace');
2433
}
34+
else if (stackType.value === 'java') {
35+
formatedStackTrace.value.textContent = cleanedStackTrace;
36+
JavaStack('.stacktrace', { prettyprint: true });
37+
}
38+
else if (stackType.value === 'python') {
39+
formatedStackTrace.value.textContent = cleanedStackTrace;
40+
PythonStack('.stacktrace', { prettyprint: true });
41+
}
2542
else if (stackType.value === 'net') {
26-
formatedStackTrace.value.innerText = stackTrace.value;
43+
formatedStackTrace.value.innerText = cleanedStackTrace;
2744
const _ = new NetStack(formatedStackTrace.value, { prettyprint: true });
2845
}
2946
stackTraceText.value = formatedStackTrace.value.innerText;
@@ -32,7 +49,19 @@ watchEffect(() => {
3249
});
3350
3451
const stackTraceMarkdown = computed(() => {
35-
const lang = stackType.value === 'net' ? 'csharp' : 'javascript';
52+
let lang;
53+
if (stackType.value === 'net') {
54+
lang = 'csharp';
55+
}
56+
else if (stackType.value === 'js') {
57+
lang = 'javascript';
58+
}
59+
else if (stackType.value === 'java') {
60+
lang = 'java';
61+
}
62+
else if (stackType.value === 'python') {
63+
lang = 'python';
64+
}
3665
return `\`\`\`${lang}\n${formatedStackTrace.value?.innerText}\n\`\`\``;
3766
});
3867
@@ -46,10 +75,12 @@ async function downloadAsPNG() {
4675
link.href = dataUrl;
4776
link.click();
4877
}
78+
79+
const wrap = ref(true);
4980
</script>
5081

5182
<template>
52-
<div>
83+
<div max-w-600px>
5384
<n-radio-group v-model:value="stackType" name="radiogroup" mb-2 flex justify-center>
5485
<n-space>
5586
<n-radio
@@ -60,9 +91,22 @@ async function downloadAsPNG() {
6091
value="js"
6192
label="Javascript"
6293
/>
94+
<n-radio
95+
value="python"
96+
label="Python"
97+
/>
98+
<n-radio
99+
value="java"
100+
label="Java"
101+
/>
63102
</n-space>
64103
</n-radio-group>
65104

105+
<div mb-2 flex justify-center>
106+
<n-checkbox v-model:checked="wrap">
107+
Wrap lines?
108+
</n-checkbox>
109+
</div>
66110
<c-input-text
67111
v-model:value="stackTrace"
68112
label="Stacktrace"
@@ -73,7 +117,7 @@ async function downloadAsPNG() {
73117

74118
<n-divider />
75119

76-
<pre ref="formatedStackTrace" class="stacktrace" style="padding: 20px;" />
120+
<pre ref="formatedStackTrace" class="stacktrace" :style="{ padding: '20px', whiteSpace: wrap ? 'pre-wrap' : 'pre', overflowX: 'auto', wordBreak: 'break-all' }" />
77121

78122
<div v-if="stackTraceText" flex justify-center gap-1>
79123
<c-button @click="copyText()">
@@ -103,5 +147,7 @@ pre, code {background-color:#333 !important; color: #fff !important;}
103147
.st-column {color: #f8b068;}
104148
.st-lineNumber {color: #ff4f68;}
105149
.st-fileName {color: #85dbff;}
150+
.st-intro {color: #0044dd;}
151+
.st-exception {color: #e40000;}
106152
}
107153
</style>

0 commit comments

Comments
 (0)