Update tui_layout.sh

This commit is contained in:
Sofia
2026-01-21 14:23:29 +08:00
committed by GitHub
parent 25c0680e3a
commit 41595bb643

View File

@@ -21,93 +21,103 @@ content_line() {
return return
fi fi
printf '%b' "$raw_input" | awk -v table_width="$TABLE_WIDTH" ' printf '%b' "$raw_input" | LC_ALL=C awk -v table_width="$TABLE_WIDTH" '
BEGIN { BEGIN {
textWidth = table_width - 3 textWidth = table_width - 3
currentDisplayWidth = 0
wordWidth = 0
currentLine = ""
wordBuffer = ""
lastColor = ""
ESC = sprintf("%c", 27)
}
currentDisplayWidth = 0 {
wordWidth = 0 n = split($0, chars, "")
currentLine = ""
wordBuffer = ""
lastColor = "" for (i = 1; i <= n; i++) {
r = chars[i]
ESC = sprintf("%c", 27) if (r == ESC && i+1 <= n && chars[i+1] == "[") {
} ansiSeq = ""
for (j = i; j <= n; j++) {
ansiSeq = ansiSeq chars[j]
if (chars[j] == "m") {
i = j
break
}
}
wordBuffer = wordBuffer ansiSeq
lastColor = ansiSeq
continue
}
{ charWidth = 1
n = split($0, chars, "")
for (i = 1; i <= n; i++) { if (r <= "\177") {
r = chars[i] charWidth = 1
}
else if (r >= "\340" && r <= "\357" && i+2 <= n) {
r = chars[i] chars[i+1] chars[i+2]
i += 2
charWidth = 2
}
else if (r >= "\300" && r <= "\337" && i+1 <= n) {
r = chars[i] chars[i+1]
i += 1
charWidth = 1
}
else {
charWidth = 1
}
if (r == ESC && i+1 <= n && chars[i+1] == "[") { if (r == " " || charWidth == 2) {
# Scan forward until "m" if (currentDisplayWidth + wordWidth + charWidth > textWidth) {
ansiSeq = "" printf " %s\033[0m\033[%dG||\n", currentLine, table_width
for (j = i; j <= n; j++) { currentLine = lastColor wordBuffer
ansiSeq = ansiSeq chars[j] currentDisplayWidth = wordWidth
if (chars[j] == "m") { wordBuffer = r
i = j # Advance main loop wordWidth = charWidth
break } else {
} currentLine = currentLine wordBuffer r
} currentDisplayWidth += wordWidth + charWidth
wordBuffer = wordBuffer ansiSeq wordBuffer = ""
lastColor = ansiSeq # Remember color for wrapping wordWidth = 0
continue }
} } else {
wordBuffer = wordBuffer r
wordWidth += charWidth
charWidth = 1 if (wordWidth > textWidth) {
if (r ~ /[^\001-\177]/) { printf " %s%s\033[0m\033[%dG||\n", currentLine, wordBuffer, table_width
charWidth = 2 currentLine = lastColor
} currentDisplayWidth = 0
wordBuffer = ""
wordWidth = 0
}
}
}
}
if (r == " " || charWidth == 2) { END {
if (currentDisplayWidth + wordWidth + charWidth > textWidth) { if (wordWidth > 0) {
printf " %s\033[0m\033[%dG||\n", currentLine, table_width if (currentDisplayWidth + wordWidth > textWidth) {
currentLine = lastColor wordBuffer printf " %s\033[0m\033[%dG||\n", currentLine, table_width
currentDisplayWidth = wordWidth currentLine = lastColor wordBuffer
wordBuffer = r } else {
wordWidth = charWidth currentLine = currentLine wordBuffer
} else { }
currentLine = currentLine wordBuffer r }
currentDisplayWidth += wordWidth + charWidth
wordBuffer = ""
wordWidth = 0
}
} else {
wordBuffer = wordBuffer r
wordWidth += charWidth
if (wordWidth > textWidth) { cleanText = currentLine
printf " %s%s\033[0m\033[%dG||\n", currentLine, wordBuffer, table_width gsub(/\033\[[0-9;]*m/, "", cleanText)
currentLine = lastColor gsub(/^[ \t]+|[ \t]+$/, "", cleanText)
currentDisplayWidth = 0
wordBuffer = ""
wordWidth = 0
}
}
}
}
END { if (cleanText != "") {
if (wordWidth > 0) { printf " %s\033[0m\033[%dG||\n", currentLine, table_width
if (currentDisplayWidth + wordWidth > textWidth) { }
printf " %s\033[0m\033[%dG||\n", currentLine, table_width }
currentLine = lastColor wordBuffer '
} else {
currentLine = currentLine wordBuffer
}
}
cleanText = currentLine
gsub(/\033\[[0-9;]*m/, "", cleanText)
gsub(/^[ \t]+|[ \t]+$/, "", cleanText)
if (cleanText != "") {
printf " %s\033[0m\033[%dG||\n", currentLine, table_width
}
}
'
} }
# function to print sub content lines # function to print sub content lines