Skip to content

Commit

Permalink
MacOS catalog terminal reset (#2984)
Browse files Browse the repository at this point in the history
* MacOS terminal reset commands

* Cleanup

* Add handling of raw message type

* MacOS terminal cleanup

* Imports update

* Updated terminal reset steps

* Cleanup
  • Loading branch information
denis256 committed Mar 8, 2024
1 parent c1a9d1c commit 98d3988
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
12 changes: 10 additions & 2 deletions cli/commands/catalog/tui/models/list/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package list

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -69,8 +70,7 @@ func (model Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case tea.QuitMsg:
// handle quit message
os.Exit(0)
return model, tea.Quit
return model, tea.Sequence(page.Cmd(page.ClearScreen()), tea.Quit)

case tea.KeyMsg:
// Don't match any of the keys below if we're actively filtering.
Expand All @@ -96,6 +96,14 @@ func (model Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
}
rawMsg := fmt.Sprintf("%T", msg)
// handle special case for Exit alt screen
if rawMsg == "tea.execMsg" {
defer func() {
os.Exit(0)
}()
return model, tea.Sequence(page.Cmd(page.ClearScreenCmd()), tea.Quit)
}

newModel, cmd := model.Model.Update(msg)
model.Model = &newModel
Expand Down
53 changes: 45 additions & 8 deletions cli/commands/catalog/tui/models/page/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package page

import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

Expand Down Expand Up @@ -79,9 +81,10 @@ func NewModel(module *module.Module, width, height int, previousModel tea.Model,
NewButton(ScaffoldButtonName, func(msg tea.Msg) tea.Cmd {
quitFn := func(err error) tea.Msg {
quitFn(err)
return clearScreen()
return ClearScreen()
}
return tea.Exec(command.NewScaffold(opts, module), quitFn)
result := tea.Exec(command.NewScaffold(opts, module), quitFn)
return tea.Sequence(result, ClearScreenCmd())
}),
NewButton(ViewInBrowserButtonName, func(msg tea.Msg) tea.Cmd {
if err := browser.OpenURL(module.URL()); err != nil {
Expand Down Expand Up @@ -150,6 +153,15 @@ func (model Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
model.viewport.Height = msg.Height - lipgloss.Height(model.footerView())
}

rawMsg := fmt.Sprintf("%T", msg)
// handle special case for Exit alt screen
if rawMsg == "tea.execMsg" {
defer func() {
os.Exit(0)
}()
return model, tea.Sequence(Cmd(ClearScreenCmd()), tea.Quit)
}

var viewport viewport.Model
viewport, cmd = model.viewport.Update(msg)

Expand Down Expand Up @@ -180,12 +192,37 @@ func (model Model) footerView() string {
return lipgloss.JoinVertical(lipgloss.Left, info, model.Buttons.View(), model.keys.View())
}

// clearScreen - explicit clear screen to avoid terminal hanging
func clearScreen() tea.Msg {
// ClearScreen - explicit clear screen to avoid terminal hanging
func ClearScreen() tea.Msg {
ansiTerminalReset()
if runtime.GOOS == "darwin" {
// Clear screen for macOS with ANSI commands
// https://www.unix.com/os-x-apple-/279401-means-clearing-scroll-buffer-osx-terminal.html
fmt.Print("\033[H\033[2J\033[3J")
cmd := exec.Command("stty", "sane")
_ = cmd.Run()
}
if runtime.GOOS == "linux" {
cmd := exec.Command("reset")
_ = cmd.Run()
}
return tea.Sequence(Cmd(tea.ExitAltScreen()), Cmd(tea.ClearScreen()), Cmd(tea.ClearScrollArea()), tea.Quit)
}

func ansiTerminalReset() {
// https://www.unix.com/os-x-apple-/279401-means-clearing-scroll-buffer-osx-terminal.html
fmt.Print("\033c") // Reset the terminal
fmt.Print("\033[2J") // Clear the screen
fmt.Print("\033[3J") // Clear buffer
fmt.Print("\033[H") // Move the cursor to the home position
fmt.Print("\033[0m") // Reset all terminal attributes to their defaults
}

// ClearScreenCmd - command to clear the screen
func ClearScreenCmd() tea.Cmd {
return ClearScreen
}

// Cmd - wrap a message in a command
func Cmd(msg tea.Msg) tea.Cmd {
return func() tea.Msg {
return msg
}
return tea.Quit()
}
5 changes: 5 additions & 0 deletions cli/commands/catalog/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"context"
"os"

tea "github.com/charmbracelet/bubbletea"
"github.com/gruntwork-io/terragrunt/cli/commands/catalog/module"
Expand All @@ -13,6 +14,10 @@ func Run(ctx context.Context, modules module.Modules, opts *options.TerragruntOp
ctx, cancel := context.WithCancelCause(ctx)
quitFn := func(err error) {
go cancel(err)
if err != nil {
// explicit exit from application
os.Exit(1)
}
}

list := list.NewModel(modules, quitFn, opts)
Expand Down

0 comments on commit 98d3988

Please sign in to comment.