Skip to content

Commit

Permalink
expose methods on filetable for setting options
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinoKader committed Feb 15, 2023
1 parent 1a40a29 commit d093763
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
36 changes: 22 additions & 14 deletions ui/filetable/filetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,35 @@ func New(opts ...Option) Model {
return m
}

func (m *Model) SetFiles(filePaths []string) {
for _, filePath := range filePaths {
size, err := file.FileSize(filePath)
var formattedSize string
if err != nil {
formattedSize = "N/A"
} else {
formattedSize = ui.ByteCountSI(size)
}
m.rows = append(m.rows, fileRow{path: filePath, formattedSize: formattedSize})
}
m.table.SetHeight(int(math.Min(float64(m.MaxHeight), float64(len(filePaths)))))
m.updateColumns()
m.updateRows()
}

func WithFiles(filePaths []string) Option {
return func(m *Model) {
for _, filePath := range filePaths {
size, err := file.FileSize(filePath)
var formattedSize string
if err != nil {
formattedSize = "N/A"
} else {
formattedSize = ui.ByteCountSI(size)
}
m.rows = append(m.rows, fileRow{path: filePath, formattedSize: formattedSize})
}
m.table.SetHeight(int(math.Min(float64(m.MaxHeight), float64(len(filePaths)))))
m.updateColumns()
m.updateRows()
m.SetFiles(filePaths)
}
}

func (m *Model) SetMaxHeight(height int) {
m.MaxHeight = height
}

func WithMaxHeight(height int) Option {
return func(m *Model) {
m.MaxHeight = height
m.SetMaxHeight(height)
m.updateRows()
}
}
Expand Down
4 changes: 2 additions & 2 deletions ui/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
ui.ByteCountSI(m.transferProgress.TransferSpeedEstimateBps),
)

filetable.WithMaxHeight(math.MaxInt)(&m.fileTable)
m.fileTable.SetMaxHeight(math.MaxInt)
m.fileTable = m.fileTable.Finalize().(filetable.Model)
return m, ui.TaskCmd(message, tea.Batch(m.spinner.Tick, decompressCmd(msg.temp)))

Expand All @@ -186,7 +186,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.receivedFiles = msg.filenames
m.decompressedPayloadSize = msg.decompressedPayloadSize

filetable.WithFiles(m.receivedFiles)(&m.fileTable)
m.fileTable.SetFiles(m.receivedFiles)
return m, ui.QuitCmd()

case ui.ErrorMsg:
Expand Down

0 comments on commit d093763

Please sign in to comment.