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

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" />
</div>
<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-card>
</q-dialog>

View File

@ -54,6 +54,7 @@ export default {
imagebtn: Boolean,
closebtn: Boolean,
runResult: Array,
selectText: String,
},
emits: ["updateResult"],
computed: {
@ -133,11 +134,13 @@ export default {
},
methods: {
copyResult() {
window.utools.copyText(this.getFormattedContent());
window.utools.copyText(this.selectText || this.getFormattedContent());
quickcommand.showMessageBox("已复制到剪贴板");
},
sendResult() {
window.utools.hideMainWindowTypeString(this.getFormattedContent());
window.utools.hideMainWindowTypeString(
this.selectText || this.getFormattedContent()
);
},
dataUrlToImg() {
const imagePattern = /data:image\/.*?;base64,.*/g;
@ -172,15 +175,33 @@ export default {
];
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 maxDataWidth = Math.max(
header.length,
getDisplayWidth(header),
...tableData.map((obj) => {
const value = obj[header];
return value === undefined || value === null
? 0
: String(value).replace(/\n/g, "\\n").length;
: getDisplayWidth(String(value).replace(/\n/g, "\\n"));
})
);
return maxDataWidth;
@ -191,10 +212,12 @@ export default {
"| " +
cells
.map((cell, index) => {
const cellStr = String(cell);
const displayWidth = getDisplayWidth(cellStr);
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(" | ") +
" |";