Remove config.GetConfig()
MODIFY
app/app.go
app/app.go
@@ -64,7 +64,7 @@ func Run() {
discord, readyEvent := attemptLogin(loginScreen, "", configuration)
- config.GetConfig().Token = discord.Token
+ config.Current.Token = discord.Token
persistError := config.PersistConfig()
if persistError != nil {

MODIFY
commands/commandimpls/account.go
commands/commandimpls/account.go
@@ -98,7 +98,7 @@ func (account *Account) printAccountAddHelp(writer io.Writer) {
func (account *Account) addAcount(writer io.Writer, parameters []string) {
newName := strings.ToLower(parameters[0])
- for _, acc := range config.GetConfig().Accounts {
+ for _, acc := range config.Current.Accounts {
if acc.Name == newName {
fmt.Fprintf(writer, "["+tviewutil.ColorToHex(config.GetTheme().ErrorColor)+"]The name '%s' is already in use.\n", acc.Name)
return
@@ -109,7 +109,7 @@ func (account *Account) addAcount(writer io.Writer, parameters []string) {
Name: newName,
Token: parameters[1],
}
- config.GetConfig().Accounts = append(config.GetConfig().Accounts, newAccount)
+ config.Current.Accounts = append(config.Current.Accounts, newAccount)
config.PersistConfig()
fmt.Fprintf(writer, "The account '%s' has been created successfully.\n", newName)
@@ -123,7 +123,7 @@ func deleteAccount(writer io.Writer, account string) {
var deletionSuccessful bool
newAccounts := make([]*config.Account, 0)
- for _, acc := range config.GetConfig().Accounts {
+ for _, acc := range config.Current.Accounts {
if acc.Name != account {
newAccounts = append(newAccounts, acc)
} else {
@@ -133,7 +133,7 @@ func deleteAccount(writer io.Writer, account string) {
if deletionSuccessful {
fmt.Fprintf(writer, "Account '%s' has been deleted.\n", account)
- config.GetConfig().Accounts = newAccounts
+ config.Current.Accounts = newAccounts
config.PersistConfig()
} else {
fmt.Fprintf(writer, "["+tviewutil.ColorToHex(config.GetTheme().ErrorColor)+"]Account '%s' could not be found.\n", account)
@@ -146,13 +146,13 @@ func (account *Account) printAccountSwitchHelp(writer io.Writer) {
}
func (account *Account) switchAccount(writer io.Writer, accountName string) {
- for _, acc := range config.GetConfig().Accounts {
+ for _, acc := range config.Current.Accounts {
if acc.Name == accountName {
- oldToken := config.GetConfig().Token
- config.GetConfig().Token = acc.Token
+ oldToken := config.Current.Token
+ config.Current.Token = acc.Token
persistError := account.saveAndRestart(writer)
if persistError != nil {
- config.GetConfig().Token = oldToken
+ config.Current.Token = oldToken
commands.PrintError(writer, "Error switching accounts", persistError.Error())
}
return
@@ -163,11 +163,11 @@ func (account *Account) switchAccount(writer io.Writer, accountName string) {
}
func (account *Account) logout(writer io.Writer) {
- oldToken := config.GetConfig().Token
- config.GetConfig().Token = ""
+ oldToken := config.Current.Token
+ config.Current.Token = ""
err := account.saveAndRestart(writer)
if err != nil {
- config.GetConfig().Token = oldToken
+ config.Current.Token = oldToken
fmt.Fprintf(writer, "["+tviewutil.ColorToHex(config.GetTheme().ErrorColor)+"]Error logging you out '%s'.\n", err.Error())
}
}
@@ -191,7 +191,7 @@ func (account *Account) printAccountListHelp(writer io.Writer) {
func (account *Account) listAccounts(writer io.Writer) {
fmt.Fprintln(writer, "Available accounts:")
- for _, acc := range config.GetConfig().Accounts {
+ for _, acc := range config.Current.Accounts {
fmt.Fprintln(writer, " * "+acc.Name)
}
}
@@ -206,8 +206,8 @@ func (account *Account) printAccountLogoutHelp(writer io.Writer) {
func (account *Account) currentAccount(writer io.Writer) {
var currentAccount *config.Account
- for _, acc := range config.GetConfig().Accounts {
- if acc.Token == config.GetConfig().Token {
+ for _, acc := range config.Current.Accounts {
+ if acc.Token == config.Current.Token {
currentAccount = acc
break
}
@@ -225,7 +225,7 @@ func (account *Account) printAccountAddCurrentHelp(writer io.Writer) {
}
func (account *Account) addCurrentAccount(writer io.Writer, name string) {
- account.addAcount(writer, []string{name, config.GetConfig().Token})
+ account.addAcount(writer, []string{name, config.Current.Token})
}
func (account *Account) Name() string {

MODIFY
commands/commandimpls/fixlayout.go
commands/commandimpls/fixlayout.go
@@ -43,7 +43,7 @@ func (fixLayout *FixLayout) Execute(writer io.Writer, parameters []string) {
return
}
- config.GetConfig().UseFixedLayout = choice
+ config.Current.UseFixedLayout = choice
fixLayout.window.RefreshLayout()
persistError := config.PersistConfig()
@@ -74,10 +74,10 @@ func (fixLayout *FixLayout) Execute(writer io.Writer, parameters []string) {
var successOutput string
subCommand := parameters[0]
if subCommand == "left" {
- config.GetConfig().FixedSizeLeft = int(size)
+ config.Current.FixedSizeLeft = int(size)
successOutput = fmt.Sprintf("The left side of the layout was set to %d", int(size))
} else if subCommand == "right" {
- config.GetConfig().FixedSizeRight = int(size)
+ config.Current.FixedSizeRight = int(size)
successOutput = fmt.Sprintf("The right side of the layout was set to %d", int(size))
} else {
fmt.Fprintf(writer, "The subcommand '%s' does not exist\n", subCommand)

MODIFY
config/config.go
config/config.go
@@ -35,7 +35,7 @@ const (
)
var (
- currentConfig = &Config{
+ Current = &Config{
Times: HourMinuteAndSeconds,
UseRandomUserColors: false,
ShowUserContainer: true,
@@ -188,11 +188,6 @@ func GetConfigDirectory() (string, error) {
return cachedConfigDir, nil
}
-//GetConfig returns the currently loaded configuration.
-func GetConfig() *Config {
- return currentConfig
-}
-
//LoadConfig loads the configuration initially and returns it.
func LoadConfig() (*Config, error) {
configFilePath, configError := GetConfigFile()
@@ -203,7 +198,7 @@ func LoadConfig() (*Config, error) {
configFile, openError := os.Open(configFilePath)
if os.IsNotExist(openError) {
- return GetConfig(), nil
+ return Current, nil
}
if openError != nil {
@@ -212,22 +207,22 @@ func LoadConfig() (*Config, error) {
defer configFile.Close()
decoder := json.NewDecoder(configFile)
- configLoadError := decoder.Decode(currentConfig)
+ configLoadError := decoder.Decode(Current)
//io.EOF would mean empty, therefore we use defaults.
if configLoadError != nil && configLoadError != io.EOF {
return nil, configLoadError
}
- return GetConfig(), nil
+ return Current, nil
}
// UpdateCurrentToken updates the current token and all accounts where the
// token was also used.
func UpdateCurrentToken(newToken string) {
- oldToken := currentConfig.Token
- currentConfig.Token = newToken
- for _, account := range currentConfig.Accounts {
+ oldToken := Current.Token
+ Current.Token = newToken
+ for _, account := range Current.Accounts {
if account.Token == oldToken {
account.Token = newToken
}
@@ -241,7 +236,7 @@ func PersistConfig() error {
return configError
}
- configAsJSON, jsonError := json.MarshalIndent(currentConfig, "", " ")
+ configAsJSON, jsonError := json.MarshalIndent(Current, "", " ")
if jsonError != nil {
return jsonError
}

MODIFY
times/times.go
times/times.go
@@ -18,12 +18,12 @@ func TimeToLocalString(time *time.Time) string {
// TimeToString formats a time to a string depending on the users settings.
func TimeToString(time *time.Time) string {
- if config.GetConfig().Times == config.NoTime {
+ if config.Current.Times == config.NoTime {
return ""
}
- if config.GetConfig().Times == config.HourMinuteAndSeconds {
+ if config.Current.Times == config.HourMinuteAndSeconds {
return fmt.Sprintf("%02d:%02d:%02d", time.Hour(), time.Minute(), time.Second())
- } else if config.GetConfig().Times == config.HourAndMinute {
+ } else if config.Current.Times == config.HourAndMinute {
return fmt.Sprintf("%02d:%02d", time.Hour(), time.Minute())
}
return ""

MODIFY
ui/channeltree.go
ui/channeltree.go
@@ -46,7 +46,7 @@ func NewChannelTree(state *discordgo.State) *ChannelTree {
}
channelTree.
- SetVimBindingsEnabled(config.GetConfig().OnTypeInListBehaviour == config.DoNothingOnTypeInList).
+ SetVimBindingsEnabled(config.Current.OnTypeInListBehaviour == config.DoNothingOnTypeInList).
SetCycleSelection(true).
SetTopLevel(1).
SetBorder(true).
@@ -170,7 +170,7 @@ func createChannelNode(channel *discordgo.Channel) *tview.TreeNode {
}
// Adds a padlock prefix if the channel if not readable by the everyone group
- if config.GetConfig().IndicateChannelAccessRestriction {
+ if config.Current.IndicateChannelAccessRestriction {
for _, permission := range channel.PermissionOverwrites {
if permission.Type == "role" && permission.ID == channel.GuildID && permission.Deny&discordgo.PermissionReadMessages == discordgo.PermissionReadMessages {
prefixes += tviewutil.Escape("\U0001F512")

MODIFY
ui/chatview.go
ui/chatview.go
@@ -78,13 +78,13 @@ func NewChatView(state *discordgo.State, ownUserID string) *ChatView {
bufferSize: 100,
selectionMode: false,
showSpoilerContent: make(map[string]bool),
- shortenLinks: config.GetConfig().ShortenLinks,
+ shortenLinks: config.Current.ShortenLinks,
formattedMessages: make(map[string]string),
mutex: &sync.Mutex{},
}
if chatView.shortenLinks {
- chatView.shortener = linkshortener.NewShortener(config.GetConfig().ShortenerPort)
+ chatView.shortener = linkshortener.NewShortener(config.Current.ShortenerPort)
go func() {
shortenerError := chatView.shortener.Start()
if shortenerError != nil {
@@ -281,7 +281,7 @@ func (chatView *ChatView) ClearViewAndCache() {
func (chatView *ChatView) addMessageInternal(message *discordgo.Message) {
isBlocked := discordutil.IsBlocked(chatView.state, message.Author)
- if !config.GetConfig().ShowPlaceholderForBlockedMessages && isBlocked {
+ if !config.Current.ShowPlaceholderForBlockedMessages && isBlocked {
return
}
@@ -433,7 +433,7 @@ func (chatView *ChatView) formatMessageAuthor(message *discordgo.Message) string
messageAuthor = discordutil.GetUserName(message.Author)
}
- if config.GetConfig().UseRandomUserColors {
+ if config.Current.UseRandomUserColors {
return "[" + discordutil.GetUserColor(message.Author) + "]" + messageAuthor
}

MODIFY
ui/guildlist.go
ui/guildlist.go
@@ -21,7 +21,7 @@ func NewGuildList(guilds []*discordgo.Guild, window *Window) *GuildList {
}
guildList.
- SetVimBindingsEnabled(config.GetConfig().OnTypeInListBehaviour == config.DoNothingOnTypeInList).
+ SetVimBindingsEnabled(config.Current.OnTypeInListBehaviour == config.DoNothingOnTypeInList).
SetCycleSelection(true).
SetTopLevel(1).
SetBorder(true).

MODIFY
ui/privatechats.go
ui/privatechats.go
@@ -48,7 +48,7 @@ func NewPrivateChatList(state *discordgo.State) *PrivateChatList {
}
privateList.internalTreeView.
- SetVimBindingsEnabled(config.GetConfig().OnTypeInListBehaviour == config.DoNothingOnTypeInList).
+ SetVimBindingsEnabled(config.Current.OnTypeInListBehaviour == config.DoNothingOnTypeInList).
SetRoot(tview.NewTreeNode("")).
SetTopLevel(1).
SetCycleSelection(true).

MODIFY
ui/usertree.go
ui/usertree.go
@@ -35,7 +35,7 @@ func NewUserTree(state *discordgo.State) *UserTree {
}
userTree.internalTreeView.
- SetVimBindingsEnabled(config.GetConfig().OnTypeInListBehaviour == config.DoNothingOnTypeInList).
+ SetVimBindingsEnabled(config.Current.OnTypeInListBehaviour == config.DoNothingOnTypeInList).
SetRoot(userTree.rootNode).
SetTopLevel(1).
SetCycleSelection(true)
@@ -142,7 +142,7 @@ func (userTree *UserTree) loadGuildRoles(guildID string) ([]*discordgo.Role, err
// already part of the tree, in that case the nodes name is updated.
func (userTree *UserTree) AddOrUpdateMember(member *discordgo.Member) {
nameToUse := discordutil.GetMemberName(member)
- if config.GetConfig().UseRandomUserColors {
+ if config.Current.UseRandomUserColors {
nameToUse = "[" + discordutil.GetUserColor(member.User) + "]" + nameToUse
}
@@ -172,7 +172,7 @@ func (userTree *UserTree) AddOrUpdateMember(member *discordgo.Member) {
// in that case the users node gets updated.
func (userTree *UserTree) AddOrUpdateUser(user *discordgo.User) {
nameToUse := discordutil.GetUserName(user)
- if config.GetConfig().UseRandomUserColors {
+ if config.Current.UseRandomUserColors {
nameToUse = "[" + discordutil.GetUserColor(user) + "]" + nameToUse
}

MODIFY
ui/window.go
ui/window.go
@@ -188,7 +188,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
if channelLoadError != nil {
window.ShowErrorDialog(channelLoadError.Error())
} else {
- if config.GetConfig().FocusChannelAfterGuildSelection {
+ if config.Current.FocusChannelAfterGuildSelection {
app.SetFocus(window.channelTree)
}
}
@@ -206,7 +206,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.registerGuildHandlers()
window.registerGuildMemberHandlers()
- if config.GetConfig().MouseEnabled {
+ if config.Current.MouseEnabled {
switchToPrivateButton := tview.NewButton("Show PMs")
switchToPrivateButton.SetBorderColor(config.GetTheme().PrimitiveBackgroundColor)
switchToPrivateButton.SetSelectedFunc(func() {
@@ -228,7 +228,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.privateList.Load()
window.registerPrivateChatsHandler()
- if config.GetConfig().MouseEnabled {
+ if config.Current.MouseEnabled {
privatePage := tview.NewFlex().SetDirection(tview.FlexRow)
switchToGuildButton := tview.NewButton("Show Guilds")
switchToGuildButton.SetBorderColor(config.GetTheme().PrimitiveBackgroundColor)
@@ -503,12 +503,12 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.userList = NewUserTree(window.session.State)
- if config.GetConfig().OnTypeInListBehaviour == config.SearchOnTypeInList {
+ if config.Current.OnTypeInListBehaviour == config.SearchOnTypeInList {
guildList.SetSearchOnTypeEnabled(true)
channelTree.SetSearchOnTypeEnabled(true)
window.userList.internalTreeView.SetSearchOnTypeEnabled(true)
window.privateList.internalTreeView.SetSearchOnTypeEnabled(true)
- } else if config.GetConfig().OnTypeInListBehaviour == config.FocusMessageInputOnTypeInList {
+ } else if config.Current.OnTypeInListBehaviour == config.FocusMessageInputOnTypeInList {
guildList.SetInputCapture(tviewutil.CreateFocusTextViewOnTypeInputHandler(
window.app, window.messageInput.internalTextView))
channelTree.SetInputCapture(tviewutil.CreateFocusTextViewOnTypeInputHandler(
@@ -836,12 +836,10 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.currentContainer = window.rootContainer
app.SetInputCapture(window.handleGlobalShortcuts)
- conf := config.GetConfig()
-
- if conf.UseFixedLayout {
- window.middleContainer.AddItem(window.leftArea, conf.FixedSizeLeft, 0, true)
+ if config.Current.UseFixedLayout {
+ window.middleContainer.AddItem(window.leftArea, config.Current.FixedSizeLeft, 0, true)
window.middleContainer.AddItem(window.chatArea, 0, 1, false)
- window.middleContainer.AddItem(window.userList.internalTreeView, conf.FixedSizeRight, 0, false)
+ window.middleContainer.AddItem(window.userList.internalTreeView, config.Current.FixedSizeRight, 0, false)
} else {
window.middleContainer.AddItem(window.leftArea, 0, 7, true)
window.middleContainer.AddItem(window.chatArea, 0, 20, false)
@@ -1582,7 +1580,7 @@ func (window *Window) startMessageHandlerRoutines(input, edit, delete chan *disc
if window.selectedChannel == nil || message.ChannelID != window.selectedChannel.ID {
mentionsCurrentUser := discordutil.MentionsCurrentUserExplicitly(window.session.State, message)
- if !window.userActive && config.GetConfig().DesktopNotifications {
+ if !window.userActive && config.Current.DesktopNotifications {
if mentionsCurrentUser ||
//Always show notification for private messages
channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM {
@@ -1997,10 +1995,9 @@ func (window *Window) handleGlobalShortcuts(event *tcell.EventKey) *tcell.EventK
window.app.SetFocus(window.commandView.commandInput.internalTextView)
} else if shortcuts.ToggleUserContainer.Equals(event) {
- conf := config.GetConfig()
- conf.ShowUserContainer = !conf.ShowUserContainer
+ config.Current.ShowUserContainer = !config.Current.ShowUserContainer
- if !conf.ShowUserContainer && window.app.GetFocus() == window.userList.internalTreeView {
+ if !config.Current.ShowUserContainer && window.app.GetFocus() == window.userList.internalTreeView {
window.app.SetFocus(window.messageInput.GetPrimitive())
}
@@ -2270,15 +2267,13 @@ func (window *Window) SwitchToPreviousChannel() error {
//RefreshLayout removes and adds the main parts of the layout
//so that the ones that are disabled by settings do not show up.
func (window *Window) RefreshLayout() {
- conf := config.GetConfig()
-
- window.userList.internalTreeView.SetVisible(conf.ShowUserContainer && (window.selectedGuild != nil ||
+ window.userList.internalTreeView.SetVisible(config.Current.ShowUserContainer && (window.selectedGuild != nil ||
(window.selectedChannel != nil && window.selectedChannel.Type == discordgo.ChannelTypeGroupDM)))
- if conf.UseFixedLayout {
- window.middleContainer.ResizeItem(window.leftArea, conf.FixedSizeLeft, 7)
+ if config.Current.UseFixedLayout {
+ window.middleContainer.ResizeItem(window.leftArea, config.Current.FixedSizeLeft, 7)
window.middleContainer.ResizeItem(window.chatArea, 0, 1)
- window.middleContainer.ResizeItem(window.userList.internalTreeView, conf.FixedSizeRight, 6)
+ window.middleContainer.ResizeItem(window.userList.internalTreeView, config.Current.FixedSizeRight, 6)
} else {
window.middleContainer.ResizeItem(window.leftArea, 0, 7)
window.middleContainer.ResizeItem(window.chatArea, 0, 20)
@@ -2344,7 +2339,7 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
window.exitMessageEditModeAndKeepText()
- if config.GetConfig().FocusMessageInputAfterChannelSelection {
+ if config.Current.FocusMessageInputAfterChannelSelection {
window.app.SetFocus(window.messageInput.internalTextView)
}
@@ -2406,7 +2401,6 @@ func (window *Window) RegisterCommand(command commands.Command) {
// GetRegisteredCommands returns the map of all registered commands.
func (window *Window) GetRegisteredCommands() []commands.Command {
- //FIXME eh, should this be a copy?
return window.commands
}
@@ -2481,7 +2475,7 @@ func (window *Window) Run() error {
// Shutdown disconnects from the discord API and stops the tview application.
func (window *Window) Shutdown() {
- if config.GetConfig().ShortenLinks {
+ if config.Current.ShortenLinks {
window.chatView.shortener.Close()
}
window.session.Close()

Remove config.GetConfig()