enbas/internal/model/list.go
Dan Anglin ccdd8b6530
fix: add a new internal printer
Add a new internal printer package for printing resources to the screen
or pager.

With the new printer in place, most of the settings such as the pager
command, colour theme, whether or not colour output is disabled, etc
are defined in one place which saves us the trouble of passing an
increasing number of parameters to an increasing number of Display
methods throughout the code base.

The old Displayer interface and associated Display methods in the
model package are removed as this is now handled by the printer.

The format functions in the utilities package has essentially been
rewritten as methods to the Printer type.

Additional changes:

- All indentation when displaying information about resources (e.g.
  statuses, instance, accounts) are removed.
- The application's build information now has colour output.
2024-06-17 18:59:20 +01:00

107 lines
2.3 KiB
Go

// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package model
import (
"encoding/json"
"fmt"
)
type ListRepliesPolicy int
const (
ListRepliesPolicyFollowed ListRepliesPolicy = iota
ListRepliesPolicyList
ListRepliesPolicyNone
ListRepliesPolicyUnknown
)
const (
listRepliesPolicyFollowedValue = "followed"
listRepliesPolicyListValue = "list"
listRepliesPolicyNoneValue = "none"
)
func (l ListRepliesPolicy) String() string {
mapped := map[ListRepliesPolicy]string{
ListRepliesPolicyFollowed: listRepliesPolicyFollowedValue,
ListRepliesPolicyList: listRepliesPolicyListValue,
ListRepliesPolicyNone: listRepliesPolicyNoneValue,
}
output, ok := mapped[l]
if !ok {
return unknownValue
}
return output
}
func ParseListRepliesPolicy(value string) (ListRepliesPolicy, error) {
mapped := map[string]ListRepliesPolicy{
listRepliesPolicyFollowedValue: ListRepliesPolicyFollowed,
listRepliesPolicyListValue: ListRepliesPolicyList,
listRepliesPolicyNoneValue: ListRepliesPolicyNone,
}
output, ok := mapped[value]
if !ok {
return ListRepliesPolicyUnknown, InvalidListRepliesPolicyError{value}
}
return output, nil
}
func (l ListRepliesPolicy) MarshalJSON() ([]byte, error) {
value := l.String()
if value == unknownValue {
return nil, InvalidListRepliesPolicyError{value}
}
data, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("unable to encode %s to JSON: %w", value, err)
}
return data, nil
}
func (l *ListRepliesPolicy) UnmarshalJSON(data []byte) error {
var (
value string
err error
)
if err = json.Unmarshal(data, &value); err != nil {
return fmt.Errorf("unable to decode the JSON data: %w", err)
}
*l, err = ParseListRepliesPolicy(value)
if err != nil {
return err
}
return nil
}
type InvalidListRepliesPolicyError struct {
Value string
}
func (e InvalidListRepliesPolicyError) Error() string {
return "'" +
e.Value +
"' is not a valid list replies policy: valid values are " +
listRepliesPolicyFollowedValue + ", " +
listRepliesPolicyListValue + ", " +
listRepliesPolicyNoneValue
}
type List struct {
ID string `json:"id"`
RepliesPolicy ListRepliesPolicy `json:"replies_policy"`
Title string `json:"title"`
Accounts map[string]string
}