Jelajahi Sumber

feat(cmd): append channel topic to messages list title

ayn2op 9 bulan lalu
induk
melakukan
137c55ab9f
3 mengubah file dengan 43 tambahan dan 32 penghapusan
  1. 3 32
      cmd/guilds_tree.go
  2. 9 0
      cmd/messages_list.go
  3. 31 0
      internal/ui/util.go

+ 3 - 32
cmd/guilds_tree.go

@@ -94,40 +94,12 @@ func (gt *guildsTree) createGuildNode(n *tview.TreeNode, guild discord.Guild) {
 	n.AddChild(guildNode)
 }
 
-func (gt *guildsTree) channelToString(channel discord.Channel) string {
-	switch channel.Type {
-	case discord.DirectMessage, discord.GroupDM:
-		if channel.Name != "" {
-			return channel.Name
-		}
-
-		recipients := make([]string, len(channel.DMRecipients))
-		for i, r := range channel.DMRecipients {
-			recipients[i] = r.DisplayOrUsername()
-		}
-
-		return strings.Join(recipients, ", ")
-	case discord.GuildText:
-		return "#" + channel.Name
-	case discord.GuildVoice, discord.GuildStageVoice:
-		return "v-" + channel.Name
-	case discord.GuildAnnouncement:
-		return "a-" + channel.Name
-	case discord.GuildStore:
-		return "s-" + channel.Name
-	case discord.GuildForum:
-		return "f-" + channel.Name
-	default:
-		return channel.Name
-	}
-}
-
 func (gt *guildsTree) createChannelNode(node *tview.TreeNode, channel discord.Channel) {
 	if channel.Type != discord.DirectMessage && channel.Type != discord.GroupDM && !discordState.HasPermissions(channel.ID, discord.PermissionViewChannel) {
 		return
 	}
 
-	channelNode := tview.NewTreeNode(gt.channelToString(channel)).
+	channelNode := tview.NewTreeNode(ui.ChannelToString(channel)).
 		SetReference(channel.ID).
 		SetTextStyle(gt.getChannelNodeStyle(channel.ID))
 	node.AddChild(channelNode)
@@ -214,10 +186,9 @@ func (gt *guildsTree) onSelected(node *tview.TreeNode) {
 		}
 
 		app.messagesList.reset()
+		app.messagesList.setTitle(*channel)
 		app.messagesList.drawMessages(messages)
-		app.messagesList.
-			ScrollToEnd().
-			SetTitle(gt.channelToString(*channel))
+		app.messagesList.ScrollToEnd()
 		app.messageInput.SetDisabled(!discordState.HasPermissions(channel.ID, discord.PermissionSendMessages))
 
 		gt.selectedChannelID = channel.ID

+ 9 - 0
cmd/messages_list.go

@@ -70,6 +70,15 @@ func (ml *messagesList) reset() {
 		SetTitle("")
 }
 
+func (ml *messagesList) setTitle(channel discord.Channel) {
+	title := ui.ChannelToString(channel)
+	if topic := channel.Topic; topic != "" {
+		title += " - " + topic
+	}
+
+	app.messagesList.SetTitle(title)
+}
+
 func (ml *messagesList) drawMessages(messages []discord.Message) {
 	for _, m := range slices.Backward(messages) {
 		ml.drawMessage(m)

+ 31 - 0
internal/ui/util.go

@@ -1,8 +1,11 @@
 package ui
 
 import (
+	"strings"
+
 	"github.com/ayn2op/discordo/internal/config"
 	"github.com/ayn2op/tview"
+	"github.com/diamondburned/arikawa/discord"
 )
 
 // ConfigureBox configures the provided box according to the provided theme.
@@ -46,3 +49,31 @@ func Centered(p tview.Primitive, width, height int) tview.Primitive {
 		SetRows(0, height, 0).
 		AddItem(p, 1, 1, 1, 1, 0, 0, true)
 }
+
+func ChannelToString(channel discord.Channel) string {
+	switch channel.Type {
+	case discord.DirectMessage, discord.GroupDM:
+		if channel.Name != "" {
+			return channel.Name
+		}
+
+		recipients := make([]string, len(channel.DMRecipients))
+		for i, r := range channel.DMRecipients {
+			recipients[i] = r.DisplayOrUsername()
+		}
+
+		return strings.Join(recipients, ", ")
+	case discord.GuildText:
+		return "#" + channel.Name
+	case discord.GuildVoice, discord.GuildStageVoice:
+		return "v-" + channel.Name
+	case discord.GuildAnnouncement:
+		return "a-" + channel.Name
+	case discord.GuildStore:
+		return "s-" + channel.Name
+	case discord.GuildForum:
+		return "f-" + channel.Name
+	default:
+		return channel.Name
+	}
+}