-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package config | ||
|
||
import "os" | ||
|
||
const defaultEditor string = "vim" | ||
|
||
func GetEditor() string { | ||
editor := os.Getenv("EDITOR") | ||
|
||
if editor == "" { | ||
return defaultEditor | ||
} | ||
return editor | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ui | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/bahner/go-ma-actor/config" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (ui *ChatUI) invokeEditor() ([]byte, error) { | ||
tmpfile, err := os.CreateTemp("", "edit") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer os.Remove(tmpfile.Name()) // clean up | ||
|
||
var editorErr error | ||
var contents []byte | ||
|
||
// Use Suspend to stop the TUI and run the external editor | ||
ui.app.Suspend(func() { | ||
// Launch external editor | ||
cmd := exec.Command(config.GetEditor(), tmpfile.Name()) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
editorErr = cmd.Run() // This will wait until the editor is closed | ||
|
||
if editorErr == nil { | ||
// Read the file only if there was no error with the editor | ||
contents, editorErr = os.ReadFile(tmpfile.Name()) | ||
} | ||
}) | ||
|
||
// Check if there was an error with the editor or reading the file | ||
if editorErr != nil { | ||
return nil, editorErr | ||
} | ||
|
||
// Remove newlies, We want this to be a single line | ||
// Trim right to remove trailing newlines | ||
contents = bytes.TrimRight(contents, "\n") | ||
// Replace all newlines with spaces (or another character if preferred) | ||
contents = bytes.ReplaceAll(contents, []byte("\n"), []byte(" ")) | ||
// Append a single newline at the end if desired | ||
contents = append(contents, '\n') | ||
|
||
return contents, nil | ||
} | ||
|
||
func (ui *ChatUI) handleEditCommand(args []string) { | ||
|
||
if log.GetLevel() < log.DebugLevel { | ||
ui.displaySystemMessage("Debug mode is off. Cannot edit messages") | ||
return | ||
} | ||
|
||
m, err := ui.invokeEditor() | ||
if err != nil { | ||
ui.displaySystemMessage("Error invoking editor: " + err.Error()) | ||
return | ||
} | ||
|
||
if len(m) == 0 { | ||
ui.displaySystemMessage("No changes made") | ||
return | ||
} | ||
|
||
log.Debugf("Editor returned: %s", m) | ||
|
||
ui.app.QueueUpdateDraw(func() { | ||
ui.inputField = ui.setupInputField() | ||
ui.updateRoot() | ||
ui.inputField.SetText(string(m)) | ||
ui.app.SetFocus(ui.inputField) | ||
}) | ||
|
||
log.Debug("Editor command handled") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ui | ||
|
||
import "github.com/spf13/viper" | ||
|
||
func (ui *ChatUI) handleNickCommand(args []string) { | ||
|
||
if len(args) == 2 { | ||
|
||
nick := args[1] | ||
|
||
viper.Set("actor.nick", nick) | ||
ui.inputField.SetLabel(nick + ": ") | ||
|
||
} else { | ||
ui.handleHelpNickCommand() | ||
} | ||
} | ||
|
||
func (ui *ChatUI) handleHelpNickCommand() { | ||
ui.displaySystemMessage("Usage: /nick NAME") | ||
ui.displaySystemMessage("Sets your nick.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters