diff --git a/internal/commands/payouts/payouts.go b/internal/commands/payouts/payouts.go index bcfc894..9ec9f06 100644 --- a/internal/commands/payouts/payouts.go +++ b/internal/commands/payouts/payouts.go @@ -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), }) } diff --git a/internal/commands/readers/readers.go b/internal/commands/readers/readers.go index 9c0bd0c..79ea7bb 100644 --- a/internal/commands/readers/readers.go +++ b/internal/commands/readers/readers.go @@ -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))), } diff --git a/internal/commands/transactions/transactions.go b/internal/commands/transactions/transactions.go index ab32067..0a6cfb5 100644 --- a/internal/commands/transactions/transactions.go +++ b/internal/commands/transactions/transactions.go @@ -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)), }) } diff --git a/internal/display/attribute/attribute.go b/internal/display/attribute/attribute.go index 10b158c..0cce108 100644 --- a/internal/display/attribute/attribute.go +++ b/internal/display/attribute/attribute.go @@ -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, } } @@ -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 { @@ -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), } }