第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有顏色屬性的 golang 絲網(wǎng)印刷格式化數(shù)據(jù)

具有顏色屬性的 golang 絲網(wǎng)印刷格式化數(shù)據(jù)

Go
料青山看我應(yīng)如是 2022-08-24 16:19:38
想要打印格式設(shè)置為典型 fmt 中的行/列表。Printf(“%5s %5s %5s\n”,col1, col2, col3)如果字符串是純文本,當(dāng)然可以正常工作,但如果字符串具有顏色,粗體,字體等顯示屬性 - 即使可見數(shù)據(jù)與純文本的長度相同,并且在%5s中也可以;做 len(col1) 要長得多,它會扭曲表對齊方式。Printf有沒有辦法做到這一點(diǎn),或者另一個std Go包?要:Item    Item   Item=====   =====  ====abc     defgh  xyzx       abc    dvv      xxxxx                 zz      <=== this happens if string xxxxx has display attributes from                                            fatih,gchalk, etc. to set foreground/background color`//包裝主import ( “fmt” “github.com/jwalton/gchalk” “github.com/fatih/color” )func main() {var colorWithGchalk = gchalk.Redvar data = []string{"one", "ten", "twenty"}gchalk.SetLevel(gchalk.LevelAnsi16m) // seems needed for gitbash// note output columns framed by <> just to see actual widthfmt.Println("desired formatted output")fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n\n", data[0],data[1],data[2])/*** gchalk*/// first try using gchalk for color// colorize second column - column width ignored?fmt.Println("colorized field loses its 10 character width, so subsequent fields now misaligned")fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], colorWithGchalk(data[1]), data[2])// same as above but eliminate gchalk function and just apply colorizing directly - same resultfmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], gchalk.Red(data[1]), data[2])/*** fatih*/fmt.Println("\nwith fatih")var colorWithFatih = color.New(color.FgRed).SprintFunc()fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], colorWithFatih(data[1]), data[2])} `輸出: ' 所需的格式化輸出彩色字段失去其 10 個字符的寬度,因此后續(xù)字段現(xiàn)在未對齊與法提赫' 在屏幕上,上面的 3 行根據(jù)需要以紅色顯示單詞“10”,但字段不再為 10 寬。
查看完整描述

3 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個贊

Printf有沒有辦法做到這一點(diǎn),

還是另一個標(biāo)準(zhǔn) Go 包?

(你所謂的“顯示屬性”字節(jié)輸出流的一部分,它們不是“屬性”,這是終端仿真器解釋的“內(nèi)聯(lián)數(shù)據(jù)”。您可以做的是在打印之前過濾掉此內(nèi)聯(lián)數(shù)據(jù)。


查看完整回答
反對 回復(fù) 2022-08-24
?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個贊

您可以使用 https://github.com/olekukonko/tablewriter 作為如何輸出表或僅使用包的示例。


查看完整回答
反對 回復(fù) 2022-08-24
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個贊

在Jason Walton的建議下,Chalk to gchalk的搬運(yùn)工。我得到了fmt。Printf %s 以滿足我的需求,盡管如果字段寬度(%s)較窄,它們可能會出現(xiàn)問題。

我想將至少兩個字符串連接在一起以提供一個%s。第一個字符串是純文本(sgCharToPrint),下一個字符串是彩色的,所以它是實(shí)際的屏幕文本(missedRaw)(錯過的是顏色字符串,例如missueRaw用ansi格式字符包裝。

myLen = len(sgCharToPrint) + len(missedRaw)

填充 = sgCharToPrint + missed + 字符串。重復(fù)(“ ”, 30 - olen)

騰訊網(wǎng).Printf(“%30s %4d %10s \n”,填充,值,尾部)

現(xiàn)在,“表”顯示保持對齊狀態(tài)。


查看完整回答
反對 回復(fù) 2022-08-24
  • 3 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號