From e4b02beb83d3890e00db84e5c8a3e9ff0a36d310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sun, 4 Apr 2021 16:26:42 -0500 Subject: [PATCH] Implemented hashtag timeline --- README.md | 17 +++++++++++++++++ cli/timelineCmd.go | 2 +- mast/cmd.go | 40 +++++++++++++++++++++++++++++++++++----- mast/timeline.go | 22 ++++++++++++++++++++-- tui/tui.go | 11 ++++++----- 5 files changed, 79 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e14d66c..94417d2 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Leave **Command** mode (while in **Command** mode) `local` \ `public` \ `notifications` \ +`hashtag`*` tag [local]`* \ Switch between timelines `t`*` content ...`* \ @@ -182,6 +183,16 @@ Add media by file path to toot; can be specified multiple times ##### Usage Examples +`home` \ +Switch to the home timeline + +`hashtag theStudio local` \ +Switch to the hashtag timeline and search for the hashtag *#theStudio* on only +the *local* instance + +`hashtag lol` \ +Switch to the hashtag timeline and search for the hashtag *#lol* globally + `t Hello World!` \ Publish a new public toot that says *Hello World!* @@ -200,6 +211,12 @@ of shaving cream?* `rt 11` \ Retoot/boost toot with ID *11* +`help` \ +Show this help + +`bye` \ +Goodbye! + ### CLI diff --git a/cli/timelineCmd.go b/cli/timelineCmd.go index 9624ffb..b60276e 100644 --- a/cli/timelineCmd.go +++ b/cli/timelineCmd.go @@ -16,7 +16,7 @@ var timelineCmd = &cobra.Command{ Long: "Display different timelines.", Run: func(cmd *cobra.Command, args []string) { timeline := mast.NewTimeline(MastodonClient) - timeline.Switch(mast.TimelineHome) + timeline.Switch(mast.TimelineHome, nil) timeline.Load() output, err := tui.RenderTimeline(&timeline, 72, flagShowImages) if err != nil { diff --git a/mast/cmd.go b/mast/cmd.go index 7964c14..b0d9a8e 100644 --- a/mast/cmd.go +++ b/mast/cmd.go @@ -30,6 +30,7 @@ func CmdAvailable() ([]string) { "local", "public", "notifications", + "hashtag", "t", "toot", @@ -125,16 +126,29 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { switch cmd { case "home": - timeline.Switch(TimelineHome) + timeline.Switch(TimelineHome, nil) return CodeOk case "local": - timeline.Switch(TimelineLocal) + timeline.Switch(TimelineLocal, nil) return CodeOk case "public": - timeline.Switch(TimelinePublic) + timeline.Switch(TimelinePublic, nil) return CodeOk case "notifications": - timeline.Switch(TimelineNotifications) + timeline.Switch(TimelineNotifications, nil) + return CodeOk + case "hashtag": + hashtag, isLocal, err := CmdHelperGetHashtagParams(args) + if err != nil { + return CodeNotOk + } + + timelineOptions := TimelineOptions{ + Hashtag: hashtag, + IsLocal: isLocal, + } + + timeline.Switch(TimelineHashtag, &timelineOptions) return CodeOk case "t", "toot": return CmdToot(timeline, args, -1, VisibilityPublic) @@ -223,6 +237,23 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CodeCommandNotFound } +func CmdHelperGetHashtagParams(args string) (string, bool, error) { + splitArgs := strings.SplitN(args, " ", 2) + + if len(splitArgs) < 2 { + return args, false, nil + } + + hashtag := splitArgs[0] + isLocal := false + + if strings.ToLower(splitArgs[1]) == "local" { + isLocal = true + } + + return hashtag, isLocal, nil +} + func CmdHelperGetTootIDFromString(s string) (int, error) { tootId, err := strconv.Atoi(s) if err != nil { @@ -275,7 +306,6 @@ func CmdToot( var sensitive bool = false var filesToUpload []string - // this is a ~#[sample] ~:[string] with ~!! special words tokens := CmdContentRegex.FindAllStringSubmatch(content, -1) for _, token := range tokens { if len(token[0]) >= 3 && token[0][(len(token[0])-3):] == "~!!" { diff --git a/mast/timeline.go b/mast/timeline.go index 715f695..f020016 100644 --- a/mast/timeline.go +++ b/mast/timeline.go @@ -12,12 +12,19 @@ const ( TimelineLocal = 1 TimelinePublic = 2 TimelineNotifications = 3 - TimelineEnd = 4 + TimelineHashtag = 4 + TimelineEnd = 5 ) +type TimelineOptions struct { + IsLocal bool + Hashtag string +} + type Timeline struct { client *mastodon.Client timelineType TimelineType + timelineOptions TimelineOptions LastRenderedIndex int @@ -39,9 +46,12 @@ func NewTimeline(mastodonClient *mastodon.Client) Timeline { return timeline } -func (timeline *Timeline) Switch(timelineType TimelineType) { +func (timeline *Timeline) Switch(timelineType TimelineType, options *TimelineOptions) { if timeline.timelineType != timelineType { timeline.timelineType = timelineType + if options != nil { + timeline.timelineOptions = *options + } timeline.Toots = []Toot{} timeline.TootIndexStatusIDMappings = make(map[string]int) timeline.LastRenderedIndex = -1 @@ -83,6 +93,14 @@ func (timeline *Timeline) Load() (error) { for _, notification := range notifications { statuses = append(statuses, notification.Status) } + case TimelineHashtag: + statuses, err = + timeline.client.GetTimelineHashtag( + context.Background(), + timeline.timelineOptions.Hashtag, + timeline.timelineOptions.IsLocal, + nil, + ) } if err != nil { diff --git a/tui/tui.go b/tui/tui.go index fbe4084..9e25a5f 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -46,14 +46,14 @@ func TUI(tuiCore TUICore) { tview.Styles = tview.Theme{ PrimitiveBackgroundColor: tcell.ColorDefault, ContrastBackgroundColor: tcell.ColorBlue, - MoreContrastBackgroundColor: tcell.ColorGreen, + MoreContrastBackgroundColor: tcell.ColorBlack, BorderColor: tcell.ColorWhite, TitleColor: tcell.ColorWhite, GraphicsColor: tcell.ColorWhite, PrimaryTextColor: tcell.ColorWhite, - SecondaryTextColor: tcell.ColorYellow, + SecondaryTextColor: tcell.ColorBlue, TertiaryTextColor: tcell.ColorGreen, - InverseTextColor: tcell.ColorBlue, + InverseTextColor: tcell.ColorBlack, ContrastSecondaryTextColor: tcell.ColorDarkCyan, } @@ -61,7 +61,7 @@ func TUI(tuiCore TUICore) { tuiCore.Timeline = mast.NewTimeline(tuiCore.Client) tuiCore.RenderedTimelineType = mast.TimelineHome - tuiCore.Timeline.Switch(mast.TimelineHome) + tuiCore.Timeline.Switch(mast.TimelineHome, nil) tuiCore.CmdLine = tview.NewInputField(). SetLabelColor(tcell.ColorDefault). @@ -198,7 +198,8 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool { } currentTimelineType := tuiCore.Timeline.GetCurrentType() - if tuiCore.RenderedTimelineType != currentTimelineType { + if tuiCore.RenderedTimelineType != currentTimelineType || + currentTimelineType == mast.TimelineHashtag { tuiCore.Stream.Clear() tuiCore.RenderedTimelineType = currentTimelineType }