优化输出面板表格字符串的显示

This commit is contained in:
fofolee 2025-01-14 22:06:23 +08:00
parent 7597d18f76
commit 021cce5947
2 changed files with 30 additions and 7 deletions

View File

@ -62,7 +62,7 @@
<q-resize-observer @resize="autoHeight" debounce="0" /> <q-resize-observer @resize="autoHeight" debounce="0" />
</div> </div>
<q-menu v-if="selectText" touch-position @before-hide="clearSelect"> <q-menu v-if="selectText" touch-position @before-hide="clearSelect">
<ResultMenu :dense="true" :content="selectText" :textbtn="true" /> <ResultMenu :dense="true" :selectText="selectText" :textbtn="true" />
</q-menu> </q-menu>
</q-card> </q-card>
</q-dialog> </q-dialog>

View File

@ -54,6 +54,7 @@ export default {
imagebtn: Boolean, imagebtn: Boolean,
closebtn: Boolean, closebtn: Boolean,
runResult: Array, runResult: Array,
selectText: String,
}, },
emits: ["updateResult"], emits: ["updateResult"],
computed: { computed: {
@ -133,11 +134,13 @@ export default {
}, },
methods: { methods: {
copyResult() { copyResult() {
window.utools.copyText(this.getFormattedContent()); window.utools.copyText(this.selectText || this.getFormattedContent());
quickcommand.showMessageBox("已复制到剪贴板"); quickcommand.showMessageBox("已复制到剪贴板");
}, },
sendResult() { sendResult() {
window.utools.hideMainWindowTypeString(this.getFormattedContent()); window.utools.hideMainWindowTypeString(
this.selectText || this.getFormattedContent()
);
}, },
dataUrlToImg() { dataUrlToImg() {
const imagePattern = /data:image\/.*?;base64,.*/g; const imagePattern = /data:image\/.*?;base64,.*/g;
@ -172,15 +175,33 @@ export default {
]; ];
if (!headers.length) return; if (!headers.length) return;
//
const getDisplayWidth = (str) => {
return String(str)
.split("")
.reduce((width, char) => {
//
//
return (
width +
(/[\u2E80-\u2FFF\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\u3100-\u312F\u3200-\u32FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF]/.test(
char
)
? 2
: 1)
);
}, 0);
};
// //
const columnWidths = headers.map((header) => { const columnWidths = headers.map((header) => {
const maxDataWidth = Math.max( const maxDataWidth = Math.max(
header.length, getDisplayWidth(header),
...tableData.map((obj) => { ...tableData.map((obj) => {
const value = obj[header]; const value = obj[header];
return value === undefined || value === null return value === undefined || value === null
? 0 ? 0
: String(value).replace(/\n/g, "\\n").length; : getDisplayWidth(String(value).replace(/\n/g, "\\n"));
}) })
); );
return maxDataWidth; return maxDataWidth;
@ -191,10 +212,12 @@ export default {
"| " + "| " +
cells cells
.map((cell, index) => { .map((cell, index) => {
const cellStr = String(cell);
const displayWidth = getDisplayWidth(cellStr);
const padding = " ".repeat( const padding = " ".repeat(
Math.max(0, columnWidths[index] - String(cell).length) Math.max(0, columnWidths[index] - displayWidth)
); );
return String(cell) + padding; return cellStr + padding;
}) })
.join(" | ") + .join(" | ") +
" |"; " |";