我有以下代碼,使用包來繪制進度條type tmpStruct struct {}func (t *tmpStruct) Write(p []byte) (n int, err error) {? ? fmt.Fprintf(os.Stdout, "%s", string(p))? ? return len(p), nil}func demoLoadingBarCount(maximumInt int) {? ? buf := tmpStruct{}? ? if nBuf, ok := interface{}(&buf).(io.Writer); ok {? ? ? ? bar := progressbar.NewOptions(? ? ? ? ? ? maximumInt,? ? ? ? ? ? progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),? ? ? ? ? ? progressbar.OptionSetWidth(100),? ? ? ? ? ? progressbar.OptionSetWriter(nBuf),? ? ? ? )? ? ? ? for i := 0; i < maximumInt; i++ {? ? ? ? ? ? bar.Add(1)? ? ? ? ? ? time.Sleep(10 * time.Millisecond)? ? ? ? }? ? }}一切正常,除了最后沒有換行,正如您在此處看到的那樣?我無法在 Write 函數中添加新行字符,因為這會導致在將每個字節推送到寫入器后將其添加到新行。有什么巧妙的方法可以做到這一點嗎?編輯:我想要在進度條之后和下一行打印之前添加新行
1 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
您所問問題的簡單答案就是在進度條完成后打印一個額外的換行符:
func demoLoadingBarCount(maximumInt int) {
? ? buf := &tmpStruct{}
? ? bar := progressbar.NewOptions(
? ? ? ? maximumInt,
? ? ? ? progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
? ? ? ? progressbar.OptionSetWidth(100),
? ? ? ? progressbar.OptionSetWriter(buf),
? ? )
? ? for i := 0; i < maximumInt; i++ {
? ? ? ? bar.Add(1)
? ? ? ? time.Sleep(10 * time.Millisecond)
? ? }
? ? fmt.Fprintf(buf, "\n") // <---- Add this
}
- 1 回答
- 0 關注
- 254 瀏覽
添加回答
舉報
0/150
提交
取消