diff --git a/ui/constants.go b/ui/constants.go index 5b49242..ecdd543 100644 --- a/ui/constants.go +++ b/ui/constants.go @@ -58,7 +58,11 @@ var QuitKeys = []string{"ctrl+c", "q", "esc"} var PadText = strings.Repeat(" ", PADDING) var QuitCommandsHelpText = HelpStyle(fmt.Sprintf("(any of [%s] to abort)", strings.Join(QuitKeys, ", "))) -var Progressbar = progress.New(progress.WithGradient(SECONDARY_ELEMENT_COLOR, ELEMENT_COLOR)) +func NewProgressBar() progress.Model { + p := progress.New(progress.WithGradient(SECONDARY_ELEMENT_COLOR, ELEMENT_COLOR)) + p.PercentFormat = " %.2f%%" + return p +} var baseStyle = lipgloss.NewStyle() diff --git a/ui/receiver/receiver.go b/ui/receiver/receiver.go index dd6cf3e..c3fddb5 100644 --- a/ui/receiver/receiver.go +++ b/ui/receiver/receiver.go @@ -85,7 +85,7 @@ type model struct { // New creates a receiver program. func New(addr string, password string, opts ...Option) *tea.Program { m := model{ - progressBar: ui.Progressbar, + progressBar: ui.NewProgressBar(), msgs: make(chan interface{}, 10), password: password, rendezvousAddr: addr, diff --git a/ui/sender/sender.go b/ui/sender/sender.go index 08cb1ce..7a1f970 100644 --- a/ui/sender/sender.go +++ b/ui/sender/sender.go @@ -323,8 +323,6 @@ func (m model) View() string { return ui.PadText + ui.LogSeparator(m.width) + ui.PadText + ui.InfoStyle(fileInfoText) + "\n\n" + ui.PadText + m.transferProgress.View() + "\n\n" + - ui.PadText + fmt.Sprintf("%s/s", ui.ByteCountSI(m.transferProgress.TransferSpeedEstimateBps)) + "\n" + - ui.PadText + fmt.Sprintf("~%v remaining", m.transferProgress.EstimatedRemainingDuration.Round(time.Second).String()) + "\n\n" + ui.PadText + m.help.View(m.keys) + "\n\n" case showFinished: diff --git a/ui/transferprogress/transferprogress.go b/ui/transferprogress/transferprogress.go index 97b413a..9b67055 100644 --- a/ui/transferprogress/transferprogress.go +++ b/ui/transferprogress/transferprogress.go @@ -14,12 +14,14 @@ import ( type Option func(*Model) type Model struct { + Width int + PayloadSize int64 + bytesTransferred int64 TransferStartTime time.Time TransferSpeedEstimateBps int64 - EstimatedRemainingDuration time.Duration + estimatedRemainingDuration time.Duration - Width int progress float64 progressBar progress.Model } @@ -30,7 +32,7 @@ func (m *Model) StartTransfer() { func New(opts ...Option) Model { m := Model{ - progressBar: ui.Progressbar, + progressBar: ui.NewProgressBar(), } for _, opt := range opts { @@ -44,7 +46,13 @@ func (Model) Init() tea.Cmd { } func (m Model) View() string { - return m.progressBar.ViewAs(m.progress) + bytesProgress := fmt.Sprintf("(%s/%s, %s/s)", + ui.ByteCountSI(m.bytesTransferred), ui.ByteCountSI(m.PayloadSize), ui.ByteCountSI(m.TransferSpeedEstimateBps)) + eta := fmt.Sprintf("%v remaining", m.estimatedRemainingDuration.Round(time.Second).String()) + progressBar := m.progressBar.ViewAs(m.progress) + + return bytesProgress + "\t\t" + eta + "\n\n" + + ui.PadText + progressBar } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -60,20 +68,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case ui.ProgressMsg: secondsSpent := time.Since(m.TransferStartTime).Seconds() - if m.progress > 0 { - bytesTransferred := m.progress * float64(m.PayloadSize) - bytesRemaining := m.PayloadSize - int64(bytesTransferred) - linearRemainingSeconds := float64(bytesRemaining) * secondsSpent / bytesTransferred + if m.bytesTransferred > 0 { + bytesRemaining := m.PayloadSize - m.bytesTransferred + linearRemainingSeconds := float64(bytesRemaining) * secondsSpent / float64(m.bytesTransferred) if remainingDuration, err := time.ParseDuration(fmt.Sprintf("%fs", linearRemainingSeconds)); err != nil { return m, ui.ErrorCmd(errors.Wrap(err, "failed to parse duration of estimated remaining transfer time")) } else { - m.EstimatedRemainingDuration = remainingDuration + m.estimatedRemainingDuration = remainingDuration } - m.TransferSpeedEstimateBps = int64(bytesTransferred / secondsSpent) + m.TransferSpeedEstimateBps = int64(float64(m.bytesTransferred) / secondsSpent) } - currentBytesReceived := float64(msg) - m.progress = math.Min(1.0, currentBytesReceived/float64(m.PayloadSize)) + m.bytesTransferred = int64(msg) + m.progress = math.Min(1.0, float64(m.bytesTransferred)/float64(m.PayloadSize)) return m, nil default: