Usernames can now be displayed with their respective role colors

This commit is contained in:
Marcel Schramm 2020-07-15 23:54:35 +02:00
parent 8644c422e5
commit 4982cff41a
No known key found for this signature in database
GPG Key ID: 05971054C70EEDC7
9 changed files with 106 additions and 51 deletions

View File

@ -5,8 +5,8 @@ import (
"log"
"os"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/tview"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/commands/commandimpls"
"github.com/Bios-Marcel/cordless/config"
@ -157,7 +157,7 @@ func Run() {
runError := app.Run()
if runError != nil {
log.Fatalf("Error launching View (%s).\n", runError.Error())
log.Fatalf("Error launching View (%v).\n", runError)
}
run := <-runNext

View File

@ -240,12 +240,18 @@ const configurationDocumentation = `[::b]TOPIC
Type: int
Default: NoTime (2)
[::b]UseRandomUserColors
Determines whether all usernames will have the same color or a color
randomly chosen from a pool of predefined colors.
Type: boolean
Default: false
[::b]UserColors
Determines how the color for a user is decided when rendering
a message author or displaying a user somewhere else.
This settings has four different possible values:
* "none"
* "single"
* "random"
* "role"
Type: string
Default: "single"
[::b]FocusChannelAfterGuildSelection
Determines whether the focus automatically jumps to the channeltree

View File

@ -32,13 +32,27 @@ const (
// FocusMessageInputOnTypeInList will automatically focus the message input
// component and transfer the typed character into it as well.
FocusMessageInputOnTypeInList = 2
NoColor UserColor = "none"
// SingleColor causes cordless to take the color specified in the theme
SingleColor UserColor = "single"
// RandomColor causes cordless to take a random color from the theme
// specified pool of usable random colors.
RandomColor UserColor = "random"
// RoleColor attempts to use the first colored role it finds for a user.
RoleColor UserColor = "role"
)
// UserColor represents available configurations for rendering a users color.
type UserColor string
var (
//Current is the currently loaded configuration. The values here are the
//defaults which can / will be overwritten by loading the config file.
Current = &Config{
Autocomplete: true,
Times: HourMinuteAndSeconds,
UseRandomUserColors: false,
UserColors: SingleColor,
ShowUserContainer: true,
UseFixedLayout: false,
FixedSizeLeft: 12,
@ -73,9 +87,9 @@ type Config struct {
//Times decides on the time format (none, short and long).
Times int
//UseRandomUserColors decides whether the users get assigned a random color
//out of a pool for the current session.
UseRandomUserColors bool
//UserColors decides how cordless determines in which color it displays
//a user in the chat or the user tree.
UserColors UserColor
//FocusChannelAfterGuildSelection will cause the widget focus to move over
//to the channel tree after selecting a guild.

View File

@ -4,8 +4,8 @@ import (
"math/rand"
"sort"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/tview"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/config"
"github.com/Bios-Marcel/cordless/ui/tviewutil"
@ -21,28 +21,56 @@ var (
randColorLength = len(config.GetTheme().RandomUserColors)
)
// GetUserColor gets the users color for this session. If no color can be found
// a new color will be a generated and cached.
// GetMemberColor gets the members color according the members role.
// If role colors aren't enabled, or the member is a bot, we fallthrough
// to GetUserColor.
func GetMemberColor(state *discordgo.State, member *discordgo.Member) string {
if len(member.Roles) > 0 && !member.User.Bot &&
config.Current.UserColors == config.RoleColor {
for _, memberRole := range member.Roles {
role, _ := state.Role(member.GuildID, memberRole)
if color := GetRoleColor(role); color != "" {
return color
}
}
}
return GetUserColor(member.User)
}
// GetUserColor gers a user color according to the configuration.
// If "random" is the setting, then a new random color is retrieved
// and cached for this session and this user.
func GetUserColor(user *discordgo.User) string {
//Despite user settings, bots always get a color.
if user.Bot {
return tviewutil.ColorToHex(config.GetTheme().BotColor)
}
//Avoid unnecessarily retrieving and caching colors
if !config.Current.UseRandomUserColors || randColorLength == 0 {
switch config.Current.UserColors {
case config.RandomColor:
//Avoid unnecessarily retrieving and caching colors and fallthrough
//to using single color instead.
if randColorLength != 0 {
color, ok := userColorCache[user.ID]
if ok {
return color
}
newColor := getRandomColorString()
userColorCache[user.ID] = newColor
return newColor
}
fallthrough
case config.SingleColor:
return tviewutil.ColorToHex(config.GetTheme().DefaultUserColor)
} else if randColorLength == 1 {
return tviewutil.ColorToHex(config.GetTheme().RandomUserColors[0])
case config.NoColor:
fallthrough
default:
return tviewutil.ColorToHex(config.GetTheme().PrimaryTextColor)
}
color, ok := userColorCache[user.ID]
if ok {
return color
}
newColor := getRandomColorString()
userColorCache[user.ID] = newColor
return newColor
}
func getRandomColorString() string {

View File

@ -12,7 +12,7 @@ import (
)
func TestGetUserColor(t *testing.T) {
config.Current.UseRandomUserColors = true
config.Current.UserColors = config.RandomColor
tests := []struct {
name string
user *discordgo.User

View File

@ -1,4 +1,4 @@
//+build go1.12,debug
//+build debug
package main

View File

@ -19,8 +19,8 @@ import (
"github.com/Bios-Marcel/cordless/times"
"github.com/Bios-Marcel/cordless/ui/tviewutil"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/tview"
"github.com/Bios-Marcel/discordgo"
// Blank import for initializing the tview formatter
_ "github.com/Bios-Marcel/cordless/syntax"
@ -426,19 +426,23 @@ func (chatView *ChatView) formatMessage(message *discordgo.Message) string {
}
func (chatView *ChatView) formatMessageAuthor(message *discordgo.Message) string {
var messageAuthor string
var member *discordgo.Member
if message.GuildID != "" {
member, cacheError := chatView.state.Member(message.GuildID, message.Author.ID)
if cacheError == nil {
messageAuthor = discordutil.GetMemberName(member)
}
member, _ = chatView.state.Member(message.GuildID, message.Author.ID)
}
var messageAuthor string
var userColor string
if member != nil {
messageAuthor = discordutil.GetMemberName(member)
userColor = discordutil.GetMemberColor(chatView.state, member)
}
if messageAuthor == "" {
messageAuthor = discordutil.GetUserName(message.Author)
userColor = discordutil.GetUserColor(message.Author)
}
return "[" + discordutil.GetUserColor(message.Author) + "]" + messageAuthor
return "[::b][" + userColor + "]" + messageAuthor + "[::-]"
}
func (chatView *ChatView) formatMessageText(message *discordgo.Message) string {

View File

@ -8,8 +8,8 @@ import (
"github.com/Bios-Marcel/cordless/discordutil"
"github.com/Bios-Marcel/cordless/ui/tviewutil"
"github.com/Bios-Marcel/discordgo"
"github.com/Bios-Marcel/cordless/tview"
"github.com/Bios-Marcel/discordgo"
"github.com/gdamore/tcell"
)
@ -152,7 +152,8 @@ func (userTree *UserTree) loadGuildRoles(guildID string) ([]*discordgo.Role, err
for _, role := range guildRoles {
if role.Hoist {
roleNode := tview.NewTreeNode(tviewutil.Escape(role.Name))
roleNode := tview.NewTreeNode("[" + discordutil.GetRoleColor(role) +
"]" + tviewutil.Escape(role.Name))
roleNode.SetSelectable(false)
userTree.roleNodes[role.ID] = roleNode
userTree.rootNode.AddChild(roleNode)
@ -174,10 +175,8 @@ func (userTree *UserTree) AddOrUpdateMember(member *discordgo.Member) {
}
func (userTree *UserTree) addOrUpdateMember(member *discordgo.Member) {
nameToUse := discordutil.GetMemberName(member)
if config.Current.UseRandomUserColors {
nameToUse = "[" + discordutil.GetUserColor(member.User) + "]" + nameToUse
}
nameToUse := "[" + discordutil.GetMemberColor(userTree.state, member) +
"]" + discordutil.GetMemberName(member)
userNode, contains := userTree.userNodes[member.User.ID]
if contains && userNode != nil {
@ -213,10 +212,8 @@ func (userTree *UserTree) AddOrUpdateUser(user *discordgo.User) {
}
func (userTree *UserTree) addOrUpdateUser(user *discordgo.User) {
nameToUse := discordutil.GetUserName(user)
if config.Current.UseRandomUserColors {
nameToUse = "[" + discordutil.GetUserColor(user) + "]" + nameToUse
}
nameToUse := "[" + discordutil.GetUserColor(user) +
"]" + discordutil.GetUserName(user)
userNode, contains := userTree.userNodes[user.ID]
if contains && userNode != nil {

View File

@ -745,10 +745,11 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
}
window.messageInput.SetInputCapture(captureFunc)
messageInputChan := make(chan *discordgo.Message, 200)
messageDeleteChan := make(chan *discordgo.Message, 50)
messageEditChan := make(chan *discordgo.Message, 50)
messageBulkDeleteChan := make(chan *discordgo.MessageDeleteBulk, 50)
//FIXME Buffering might just be retarded, as the event handlers are launched in separate routines either way.
messageInputChan := make(chan *discordgo.Message)
messageDeleteChan := make(chan *discordgo.Message)
messageEditChan := make(chan *discordgo.Message)
messageBulkDeleteChan := make(chan *discordgo.MessageDeleteBulk)
window.registerMessageEventHandler(messageInputChan, messageEditChan, messageDeleteChan, messageBulkDeleteChan)
window.startMessageHandlerRoutines(messageInputChan, messageEditChan, messageDeleteChan, messageBulkDeleteChan)
@ -1166,6 +1167,11 @@ important changes of the last two versions officially released.
- Features
- Notifications for servers and DMs are now displayed in the containers header row
- Embeds can now be rendered
- Usernames can now be rendered with their respective role color.
Bots however can't have colors, to avoid confusion with real users.
The default is set to "single", meaning it uses the default user
color from the specified theme. The setting "UseRandomUserColors" has
been removed.
- Changes
- The button to switch between DMs and servers is gone. Instead you can
click the containers, since the header row is always visible now