Skip to content

Commit

Permalink
Implemented hashtag timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 4, 2021
1 parent d357728 commit e4b02be
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Leave **Command** mode (while in **Command** mode)
`local` \
`public` \
`notifications` \
`hashtag`*` tag [local]`* \
Switch between timelines

`t`*` content ...`* \
Expand Down Expand Up @@ -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!*

Expand All @@ -200,6 +211,12 @@ of shaving cream?*
`rt 11` \
Retoot/boost toot with ID *11*

`help` \
Show this help

`bye` \
Goodbye!


### CLI

Expand Down
2 changes: 1 addition & 1 deletion cli/timelineCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 35 additions & 5 deletions mast/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func CmdAvailable() ([]string) {
"local",
"public",
"notifications",
"hashtag",

"t",
"toot",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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):] == "~!!" {
Expand Down
22 changes: 20 additions & 2 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ 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,
}

tuiCore.App = tview.NewApplication()

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).
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e4b02be

Please sign in to comment.