Skip to content

Commit

Permalink
feat: style improvements, momentaneous transfer size printout
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinoKader committed Feb 13, 2023
1 parent ea88c06 commit 82a8f5d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
6 changes: 5 additions & 1 deletion ui/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion ui/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions ui/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 19 additions & 12 deletions ui/transferprogress/transferprogress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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:
Expand Down

0 comments on commit 82a8f5d

Please sign in to comment.