Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions internal/commands/payouts/payouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ func listPayouts(ctx context.Context, cmd *cli.Command) error {

rows := make([][]attribute.Value, 0, len(*payoutList))
for _, payout := range *payoutList {
fee := attribute.OptionalValue(payout.Fee)
if payout.Fee != nil {
fee = attribute.ValueOf(fmt.Sprintf("%.2f", *payout.Fee))
}

rows = append(rows, []attribute.Value{
attribute.OptionalValue(payout.ID, func(v int) string { return fmt.Sprintf("%d", v) }),
attribute.OptionalValue(payout.Date, func(d datetime.Date) string { return d.String() }),
attribute.OptionalValue(payout.ID),
attribute.OptionalValue(payout.Date),
attribute.ValueOf(payoutAmount(payout)),
attribute.OptionalValue(payout.Fee, func(v float32) string { return fmt.Sprintf("%.2f", v) }),
attribute.OptionalValue(payout.Status, func(v payouts.FinancialPayoutStatus) string { return string(v) }),
attribute.OptionalValue(payout.Type, func(v payouts.FinancialPayoutType) string { return string(v) }),
fee,
attribute.OptionalValue(payout.Status),
attribute.OptionalValue(payout.Type),
attribute.OptionalStringValue(payout.Reference),
})
}
Expand Down
17 changes: 8 additions & 9 deletions internal/commands/readers/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,18 @@ func readerStatus(ctx context.Context, cmd *cli.Command) error {
}

data := response.Data
batteryTemp := attribute.OptionalValue(data.BatteryTemperature)
if data.BatteryTemperature != nil {
batteryTemp = attribute.ValueOf(fmt.Sprintf("%d°C", *data.BatteryTemperature))
}

details := []attribute.KeyValue{
attribute.ID(readerID),
attribute.Attribute("Status", attribute.Styled(string(data.Status))),
attribute.Optional("State", data.State, func(v readers.StatusResponseDataState) string {
return string(v)
}),
attribute.Optional("Connection", data.ConnectionType, func(v readers.StatusResponseDataConnectionType) string {
return string(v)
}),
attribute.Optional("State", data.State),
attribute.Optional("Connection", data.ConnectionType),
attribute.Attribute("Battery Level", attribute.Styled(readerStatusBatteryLevel(data.BatteryLevel))),
attribute.Optional("Battery Temp", data.BatteryTemperature, func(v int) string {
return fmt.Sprintf("%d°C", v)
}),
attribute.Attribute("Battery Temp", batteryTemp),
attribute.OptionalString("Firmware", data.FirmwareVersion),
attribute.Attribute("Last Activity", attribute.Styled(util.TimeOrDash(appCtx, data.LastActivity))),
}
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func listTransactions(ctx context.Context, cmd *cli.Command) error {
attribute.OptionalStringValue(tx.ID),
attribute.OptionalStringValue(tx.TransactionCode),
attribute.ValueOf(currency.FormatPointers(tx.Amount, tx.Currency)),
attribute.OptionalValue(tx.Status, func(v transactions.TransactionHistoryStatus) string { return string(v) }),
attribute.OptionalValue(tx.PaymentType, func(v shared.PaymentType) string { return string(v) }),
attribute.OptionalValue(tx.Status),
attribute.OptionalValue(tx.PaymentType),
attribute.ValueOf(util.TimeOrDash(appCtx, tx.Timestamp)),
})
}
Expand Down
62 changes: 17 additions & 45 deletions internal/display/attribute/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,14 @@ func (v Value) String() string {
return v.Style.Render(v.Text)
}

type styled[T any] struct {
V T
Style lipgloss.Style
}

func (s styled[T]) String() string {
return s.Style.Render(fmt.Sprintf("%v", s.V))
}

func (s styled[T]) toValue() Value {
return Value{
Text: fmt.Sprintf("%v", s.V),
Style: s.Style,
}
}

func Styled[T any](v T, style ...lipgloss.Style) styled[T] {
styl := lipgloss.NewStyle()
func Styled[T any](v T, style ...lipgloss.Style) Value {
s := lipgloss.NewStyle()
if len(style) > 0 {
styl = style[0]
s = style[0]
}
return styled[T]{
V: v,
Style: styl,
return Value{
Text: fmt.Sprintf("%v", v),
Style: s,
}
}

Expand All @@ -56,34 +40,22 @@ type KeyValue struct {
Value Value
}

func Attribute[T any](key string, value styled[T]) KeyValue {
func Attribute(key string, value Value) KeyValue {
return KeyValue{
Key: Styled(key, keyStyle).toValue(),
Value: value.toValue(),
Key: Styled(key, keyStyle),
Value: value,
}
}

func Int(key string, value styled[int]) KeyValue {
return Attribute(key, value)
}

func Bool(key string, value styled[bool]) KeyValue {
return Attribute(key, value)
}

func Stringer(key string, value styled[fmt.Stringer]) KeyValue {
return Attribute(key, value)
}

func ValueOf[T any](v T, style ...lipgloss.Style) Value {
return Styled(v, style...).toValue()
return Styled(v, style...)
}

func OptionalValue[T any](value *T, formatter func(T) string) Value {
func OptionalValue[T any](value *T) Value {
if value == nil {
return ValueOf("-")
}
return ValueOf(formatter(*value))
return ValueOf(*value)
}

func OptionalStringValue(value *string) Value {
Expand All @@ -93,18 +65,18 @@ func OptionalStringValue(value *string) Value {
return ValueOf(*value)
}

// Optional renders the provided pointer using formatter. Missing values are displayed as "-".
func Optional[T any](key string, value *T, formatter func(T) string) KeyValue {
// Optional renders the provided pointer. Missing values are displayed as "-".
func Optional[T any](key string, value *T) KeyValue {
return KeyValue{
Key: Styled(key, keyStyle).toValue(),
Value: OptionalValue(value, formatter),
Key: Styled(key, keyStyle),
Value: OptionalValue(value),
}
}

// OptionalString renders string pointers, treating nil or empty values as "-".
func OptionalString(key string, value *string) KeyValue {
return KeyValue{
Key: Styled(key, keyStyle).toValue(),
Key: Styled(key, keyStyle),
Value: OptionalStringValue(value),
}
}