52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package sendaround
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
)
|
|
|
|
type ConnectionStateType byte
|
|
|
|
const (
|
|
WaitingForClient ConnectionStateType = iota
|
|
Connected
|
|
TransmittingFile
|
|
Disconnected
|
|
Failed
|
|
)
|
|
|
|
func (state ConnectionStateType) String() string {
|
|
switch state {
|
|
case WaitingForClient:
|
|
return "WaitingForClient"
|
|
case Connected:
|
|
return "Connected"
|
|
case TransmittingFile:
|
|
return "TransmittingFile"
|
|
case Disconnected:
|
|
return "Disconnected"
|
|
case Failed:
|
|
return "Failed"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type ConnectionState struct {
|
|
Type ConnectionStateType
|
|
CurrentFile RemoteFile
|
|
TransmittedLength uint64
|
|
Error error
|
|
}
|
|
|
|
func (state ConnectionState) String() string {
|
|
switch state.Type {
|
|
case TransmittingFile:
|
|
return fmt.Sprintf("Transmitting file: %s (%s/%s, %f%%)", state.CurrentFile.FileName(),
|
|
humanize.Bytes(state.TransmittedLength), humanize.Bytes(state.CurrentFile.Length()),
|
|
100*float64(state.TransmittedLength)/float64(state.CurrentFile.Length()))
|
|
default:
|
|
return state.Type.String()
|
|
}
|
|
}
|