diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index b797d21..4aa90d1 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -4,76 +4,11 @@ import ( "errors" "flag" "fmt" - "strings" - "unicode" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/config" - "golang.org/x/net/html" ) -var instanceDetailsFormat = `INSTANCE: - %s - %s - -DOMAIN: - %s - -VERSION: - Running GoToSocial %s - -CONTACT: - name: %s - username: %s - email: %s -` - -var accountDetailsFormat = ` -%s (@%s) - -ACCOUNT ID: - %s - -JOINED ON: - %s - -STATS: - Followers: %d - Following: %d - Statuses: %d - -BIOGRAPHY: - %s - -METADATA: %s - -ACCOUNT URL: - %s -` - -var statusDetailsFormat = ` -%s (@%s) - -CONTENT: - %s - -STATUS ID: - %s - -CREATED AT: - %s - -STATS: - Boosts: %d - Likes: %d - Replies: %d - -VISIBILITY: - %s - -URL: - %s -` - type showCommand struct { *flag.FlagSet myAccount bool @@ -126,16 +61,7 @@ func (c *showCommand) showInstance(gts *client.Client) error { return fmt.Errorf("unable to retrieve the instance details; %w", err) } - fmt.Printf( - instanceDetailsFormat, - instance.Title, - instance.Description, - instance.Domain, - instance.Version, - instance.Contact.Account.DisplayName, - instance.Contact.Account.Username, - instance.Contact.Email, - ) + fmt.Println(instance) return nil } @@ -163,25 +89,7 @@ func (c *showCommand) showAccount(gts *client.Client) error { return fmt.Errorf("unable to retrieve the account details; %w", err) } - metadata := "" - - for _, field := range account.Fields { - metadata += fmt.Sprintf("\n %s: %s", field.Name, stripHTMLTags(field.Value)) - } - - fmt.Printf( - accountDetailsFormat, - account.DisplayName, - account.Username, - account.ID, - account.CreatedAt.Format("02 Jan 2006"), - account.FollowersCount, - account.FollowingCount, - account.StatusCount, - wrapLine(stripHTMLTags(account.Note), "\n ", 80), - metadata, - account.URL, - ) + fmt.Println(account) return nil } @@ -196,58 +104,7 @@ func (c *showCommand) showStatus(gts *client.Client) error { return fmt.Errorf("unable to retrieve the status; %w", err) } - fmt.Printf( - statusDetailsFormat, - status.Account.DisplayName, - status.Account.Username, - stripHTMLTags(status.Content), - status.ID, - status.CreatedAt.Format("02 Jan 2006, 03:04"), - status.RebloggsCount, - status.FavouritesCount, - status.RepliesCount, - status.Visibility, - status.URL, - ) + fmt.Println(status) return nil } - -func stripHTMLTags(text string) string { - token := html.NewTokenizer(strings.NewReader(text)) - - var builder strings.Builder - - for { - tt := token.Next() - switch tt { - case html.ErrorToken: - return builder.String() - case html.TextToken: - builder.WriteString(token.Token().Data + " ") - } - } -} - -func wrapLine(line, separator string, charLimit int) string { - if len(line) <= charLimit { - return line - } - - leftcursor, rightcursor := 0, 0 - - var builder strings.Builder - - for rightcursor < (len(line) - charLimit) { - rightcursor += charLimit - for !unicode.IsSpace(rune(line[rightcursor-1])) { - rightcursor-- - } - builder.WriteString(line[leftcursor:rightcursor] + separator) - leftcursor = rightcursor - } - - builder.WriteString(line[rightcursor:]) - - return builder.String() -} diff --git a/internal/model/account.go b/internal/model/account.go index 6dc9b72..0ecd146 100644 --- a/internal/model/account.go +++ b/internal/model/account.go @@ -1,32 +1,37 @@ package model -import "time" +import ( + "fmt" + "time" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" +) type Account struct { Acct string `json:"acct"` Avatar string `json:"avatar"` AvatarStatic string `json:"avatar_static"` - Bot bool `json:"bot"` - CreatedAt time.Time `json:"created_at"` CustomCSS string `json:"custom_css"` - Discoverable bool `json:"discoverable"` - DisplayName string `json:"display_name"` - Emojis []Emoji `json:"emojis"` - EnableRSS bool `json:"enable_rss"` - Fields []Field `json:"fields"` - FollowersCount int `json:"followers_count"` - FollowingCount int `json:"following_count"` Header string `json:"header"` HeaderStatic string `json:"header_static"` ID string `json:"id"` LastStatusAt string `json:"last_status_at"` + DisplayName string `json:"display_name"` + Emojis []Emoji `json:"emojis"` + EnableRSS bool `json:"enable_rss"` + Bot bool `json:"bot"` Locked bool `json:"locked"` + Suspended bool `json:"suspended"` + Discoverable bool `json:"discoverable"` + Fields []Field `json:"fields"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + CreatedAt time.Time `json:"created_at"` MuteExpiresAt time.Time `json:"mute_expires_at"` Note string `json:"note"` Role AccountRole `json:"role"` Source Source `json:"source"` StatusCount int `json:"statuses_count"` - Suspended bool `json:"suspended"` URL string `json:"url"` Username string `json:"username"` } @@ -50,3 +55,51 @@ type Field struct { Value string `json:"value"` VerifiedAt string `json:"verified_at"` } + +func (a Account) String() string { + format := ` +%s (@%s) + +ACCOUNT ID: + %s + +JOINED ON: + %s + +STATS: + Followers: %d + Following: %d + Statuses: %d + +BIOGRAPHY: + %s + +METADATA: %s + +ACCOUNT URL: + %s +` + metadata := "" + + for _, field := range a.Fields { + metadata += fmt.Sprintf( + "\n %s: %s", + field.Name, + utilities.StripHTMLTags(field.Value), + ) + } + + return fmt.Sprintf( + format, + a.DisplayName, + a.Username, + a.ID, + a.CreatedAt.Format("02 Jan 2006"), + a.FollowersCount, + a.FollowingCount, + a.StatusCount, + utilities.WrapLine(utilities.StripHTMLTags(a.Note), "\n ", 80), + metadata, + a.URL, + ) +} diff --git a/internal/model/instance_v2.go b/internal/model/instance_v2.go index 7ade86a..47ebbad 100644 --- a/internal/model/instance_v2.go +++ b/internal/model/instance_v2.go @@ -1,5 +1,7 @@ package model +import "fmt" + type InstanceV2 struct { AccountDomain string `json:"account_domain"` Configuration InstanceConfiguration `json:"configuration"` @@ -104,3 +106,32 @@ type InstanceV2Usage struct { type InstanceV2Users struct { ActiveMonth int `json:"active_month"` } + +func (i InstanceV2) String() string { + format := ` +INSTANCE: + %s - %s + +DOMAIN: + %s + +VERSION: + Running GoToSocial %s + +CONTACT: + name: %s + username: %s + email: %s +` + + return fmt.Sprintf( + format, + i.Title, + i.Description, + i.Domain, + i.Version, + i.Contact.Account.DisplayName, + i.Contact.Account.Username, + i.Contact.Email, + ) +} diff --git a/internal/model/status.go b/internal/model/status.go index 99f3d34..b5826b7 100644 --- a/internal/model/status.go +++ b/internal/model/status.go @@ -1,6 +1,11 @@ package model -import "time" +import ( + "fmt" + "time" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" +) type Status struct { Account Account `json:"account"` @@ -146,3 +151,43 @@ type MediaDimensions struct { Height int `json:"height"` Width int `json:"width"` } + +func (s Status) String() string { + format := ` +%s (@%s) + +CONTENT: + %s + +STATUS ID: + %s + +CREATED AT: + %s + +STATS: + Boosts: %d + Likes: %d + Replies: %d + +VISIBILITY: + %s + +URL: + %s +` + + return fmt.Sprintf( + format, + s.Account.DisplayName, + s.Account.Username, + utilities.StripHTMLTags(s.Content), + s.ID, + s.CreatedAt.Format("02 Jan 2006, 03:04"), + s.RebloggsCount, + s.FavouritesCount, + s.RepliesCount, + s.Visibility, + s.URL, + ) +} diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go new file mode 100644 index 0000000..b5147b7 --- /dev/null +++ b/internal/utilities/utilities.go @@ -0,0 +1,47 @@ +package utilities + +import ( + "strings" + "unicode" + + "golang.org/x/net/html" +) + +func StripHTMLTags(text string) string { + token := html.NewTokenizer(strings.NewReader(text)) + + var builder strings.Builder + + for { + tt := token.Next() + switch tt { + case html.ErrorToken: + return builder.String() + case html.TextToken: + builder.WriteString(token.Token().Data + " ") + } + } +} + +func WrapLine(line, separator string, charLimit int) string { + if len(line) <= charLimit { + return line + } + + leftcursor, rightcursor := 0, 0 + + var builder strings.Builder + + for rightcursor < (len(line) - charLimit) { + rightcursor += charLimit + for !unicode.IsSpace(rune(line[rightcursor-1])) { + rightcursor-- + } + builder.WriteString(line[leftcursor:rightcursor] + separator) + leftcursor = rightcursor + } + + builder.WriteString(line[rightcursor:]) + + return builder.String() +}