Add first versiom of user-update
MODIFY
app/app.go
app/app.go
@@ -100,6 +100,7 @@ func Run() {
window.RegisterCommand(commandimpls.NewFixLayoutCommand(window))
window.RegisterCommand(commandimpls.NewChatHeaderCommand(window))
window.RegisterCommand(commandimpls.NewFriendsCommand(discord))
+ window.RegisterCommand(commandimpls.NewUserUpdateCommand(window, discord))
})
}()

ADD
commands/commandimpls/userupdate.go
+package commandimpls
+
+import (
+ "fmt"
+ "io"
+
+ "github.com/Bios-Marcel/cordless/ui"
+ "github.com/Bios-Marcel/discordgo"
+)
+
+type UserUpdate struct {
+ window *ui.Window
+ session *discordgo.Session
+}
+
+func NewUserUpdateCommand(window *ui.Window, session *discordgo.Session) *UserUpdate {
+ return &UserUpdate{window, session}
+}
+
+func (nick *UserUpdate) Execute(writer io.Writer, parameters []string) {
+ if len(parameters) == 0 {
+ fmt.Println()
+ return
+ }
+
+ var newName, newPassword, newEmail string
+ for index, param := range parameters {
+ switch param {
+ case "-n", "--name", "--nick", "-u", "--username":
+ if index != len(parameters)-1 {
+ newName = parameters[index+1]
+ }
+ case "-e", "--email", "--e-mail", "--mail":
+ if index != len(parameters)-1 {
+ newEmail = parameters[index+1]
+ }
+ case "--new-password", "-np":
+ if index != len(parameters)-1 {
+ newPassword = parameters[index+1]
+ }
+ }
+ }
+
+ go func() {
+ currentPassword := nick.window.PromptSecretInput("Updating your user information", "Please enter your current password.")
+ if currentPassword != "" {
+ //TODO Error
+ } else {
+ _, err := nick.session.UserUpdate(newEmail, currentPassword, newName, nick.session.State.User.Avatar, newPassword)
+ if err != nil {
+ fmt.Fprintln(writer, err)
+ } else {
+ fmt.Fprintln(writer, "Your user has been updated.")
+ }
+ }
+
+ nick.window.ForceRedraw()
+ }()
+}
+
+func (nick *UserUpdate) Name() string {
+ return "user-update"
+}
+
+func (nick *UserUpdate) PrintHelp(writer io.Writer) {
+ panic("not implemented")
+}

MODIFY
ui/window.go
ui/window.go
@@ -1669,11 +1669,69 @@ func (window *Window) RegisterCommand(command commands.Command) {
window.commands[command.Name()] = command
}
-// GetRegisteredCommands returns the map of all registered commands
+// GetRegisteredCommands returns the map of all registered commands.
func (window *Window) GetRegisteredCommands() map[string]commands.Command {
return window.commands
}
+// GetSelectedGuild returns a reference to the currently selected UserGuild.
+func (window *Window) GetSelectedGuild() *discordgo.UserGuild {
+ return window.selectedGuild
+}
+
+// PromptSecretInput shows an input dialog that masks the user input. The
+// returned value will either be empty or what the user has entered.
+func (window *Window) PromptSecretInput(title, message string) string {
+ waitChannel := make(chan struct{})
+ var output string
+ var previousFocus tview.Primitive
+ window.app.QueueUpdateDraw(func() {
+ previousFocus = window.app.GetFocus()
+ inputField := tview.NewInputField()
+ inputField.SetMaskCharacter('*')
+ inputField.SetDoneFunc(func(key tcell.Key) {
+ if key == tcell.KeyEnter {
+ output = inputField.GetText()
+ waitChannel <- struct{}{}
+ } else if key == tcell.KeyEscape {
+ waitChannel <- struct{}{}
+ }
+ })
+ inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
+ if event.Key() == tcell.KeyCtrlV {
+ content, clipError := clipboard.ReadAll()
+ if clipError == nil {
+ inputField.SetText(content)
+ }
+ return nil
+ }
+
+ return event
+ })
+ frame := tview.NewFrame(inputField)
+ frame.SetTitle(title)
+ frame.SetBorder(true)
+ frame.AddText(message, true, tview.AlignLeft, tcell.ColorDefault)
+ window.app.SetRoot(frame, true)
+ window.currentContainer = frame
+ })
+ <-waitChannel
+ window.app.QueueUpdateDraw(func() {
+ window.app.SetRoot(window.rootContainer, true)
+ window.currentContainer = window.rootContainer
+ window.app.SetFocus(previousFocus)
+ waitChannel <- struct{}{}
+ })
+ <-waitChannel
+ return output
+}
+
+// ForceRedraw triggers ForceDraw on the underlying tview application, causing
+// it to redraw all currently shown components.
+func (window *Window) ForceRedraw() {
+ window.app.ForceDraw()
+}
+
//Run Shows the window optionally returning an error.
func (window *Window) Run() error {
return window.app.Run()

Add first versiom of user-update