0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

refactor formatter

This commit is contained in:
Zack 2024-09-01 14:35:36 -07:00
parent 0017def27c
commit 0f1d75a2ef

View file

@ -1698,12 +1698,26 @@ func (c *Client) recipientGetFileReady(finished bool) (err error) {
c.Step3RecipientRequestFile = true c.Step3RecipientRequestFile = true
return return
} }
func max(a int, b int) int { func max(a int, b int) int {
if a > b { if a > b {
return a return a
} }
return b return b
} }
func formatDescription(description string) string {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
width = max(20, width-60)
if err != nil {
return description
}
if len(description) > width {
description = description[:(width-3)] + "..."
}
return description
}
func (c *Client) createEmptyFileAndFinish(fileInfo FileInfo, i int) (err error) { func (c *Client) createEmptyFileAndFinish(fileInfo FileInfo, i int) (err error) {
log.Debugf("touching file with folder / name") log.Debugf("touching file with folder / name")
if !utils.Exists(fileInfo.FolderRemote) { if !utils.Exists(fileInfo.FolderRemote) {
@ -1741,20 +1755,12 @@ func (c *Client) createEmptyFileAndFinish(fileInfo FileInfo, i int) (err error)
} else { } else {
description = " " + description description = " " + description
} }
width, _, err := term.GetSize(int(os.Stdout.Fd()))
width = max(20, width-70)
if err != nil {
return
}
if len(description) > width {
description = description[:(width-3)] + "..."
}
c.bar = progressbar.NewOptions64(1, c.bar = progressbar.NewOptions64(1,
progressbar.OptionOnCompletion(func() { progressbar.OptionOnCompletion(func() {
c.fmtPrintUpdate() c.fmtPrintUpdate()
}), }),
progressbar.OptionSetWidth(20), progressbar.OptionSetWidth(20),
progressbar.OptionSetDescription(description), progressbar.OptionSetDescription(formatDescription(description)),
progressbar.OptionSetRenderBlankState(true), progressbar.OptionSetRenderBlankState(true),
progressbar.OptionShowBytes(true), progressbar.OptionShowBytes(true),
progressbar.OptionShowCount(), progressbar.OptionShowCount(),
@ -1878,12 +1884,13 @@ func (c *Client) updateState() (err error) {
description = c.FilesToTransfer[i].Name description = c.FilesToTransfer[i].Name
// description = "" // description = ""
} }
c.bar = progressbar.NewOptions64(1, c.bar = progressbar.NewOptions64(1,
progressbar.OptionOnCompletion(func() { progressbar.OptionOnCompletion(func() {
c.fmtPrintUpdate() c.fmtPrintUpdate()
}), }),
progressbar.OptionSetWidth(20), progressbar.OptionSetWidth(20),
progressbar.OptionSetDescription(description), progressbar.OptionSetDescription(formatDescription(description)),
progressbar.OptionSetRenderBlankState(true), progressbar.OptionSetRenderBlankState(true),
progressbar.OptionShowBytes(true), progressbar.OptionShowBytes(true),
progressbar.OptionShowCount(), progressbar.OptionShowCount(),
@ -1925,22 +1932,13 @@ func (c *Client) setBar() {
} else if !c.Options.IsSender { } else if !c.Options.IsSender {
description = " " + description description = " " + description
} }
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return
}
width = max(20, width-70)
description = strings.TrimSpace(description)
if len(description) > width {
description = description[:width-3] + "..."
}
c.bar = progressbar.NewOptions64( c.bar = progressbar.NewOptions64(
c.FilesToTransfer[c.FilesToTransferCurrentNum].Size, c.FilesToTransfer[c.FilesToTransferCurrentNum].Size,
progressbar.OptionOnCompletion(func() { progressbar.OptionOnCompletion(func() {
c.fmtPrintUpdate() c.fmtPrintUpdate()
}), }),
progressbar.OptionSetWidth(20), progressbar.OptionSetWidth(20),
progressbar.OptionSetDescription(description), progressbar.OptionSetDescription(formatDescription(description)),
progressbar.OptionSetRenderBlankState(true), progressbar.OptionSetRenderBlankState(true),
progressbar.OptionShowBytes(true), progressbar.OptionShowBytes(true),
progressbar.OptionShowCount(), progressbar.OptionShowCount(),