-
Hello, I want to have something similar to the exec example, but having the altscreen active from the beginning, like this (I only changed package main
import (
"fmt"
"os"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
)
type editorFinishedMsg struct{ err error }
func openEditor() tea.Cmd {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
c := exec.Command(editor) //nolint:gosec
return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}
type model struct {
altscreenActive bool
err error
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "a":
m.altscreenActive = !m.altscreenActive
cmd := tea.EnterAltScreen
if !m.altscreenActive {
cmd = tea.ExitAltScreen
}
return m, cmd
case "e":
return m, openEditor()
case "ctrl+c", "q":
return m, tea.Quit
}
case editorFinishedMsg:
if msg.err != nil {
m.err = msg.err
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.err != nil {
return "Error: " + m.err.Error() + "\n"
}
return "Press 'e' to open your EDITOR.\nPress 'a' to toggle the altscreen\nPress 'q' to quit.\n"
}
func main() {
m := model{altscreenActive: true}
if err := tea.NewProgram(m, tea.WithAltScreen()).Start(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
} However, when I run this program and press
gets printed outside of the altscreen. And also, just after pressing How can I fix this so that after exiting nothing gets printed? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey @zd4y, Basically what you're looking to do is have a field in your model for when you're quitting your application so that |
Beta Was this translation helpful? Give feedback.
Hey @zd4y,
you can check out the Gist I made with the functionality you're looking for here.
Basically what you're looking to do is have a field in your model for when you're quitting your application so that
View
can output an empty string as the final render to the terminal. In this case with the editor, you have to temporarily set this as we have to exit the alt screen when the editor opens, then when we quit the editor we want to see the prompt again. You'll see all of that written as code in the example provided. Let me know if this answers your question!