restore terminal state when loading term view

This commit is contained in:
sawka
2024-05-29 00:28:25 -07:00
parent 30ef011338
commit 02cda396e8
3 changed files with 66 additions and 2 deletions
+20
View File
@@ -4,6 +4,7 @@
package blockcontroller
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
@@ -108,6 +109,21 @@ func (bc *BlockController) handleShellProcData(data []byte, seqNum int) error {
return nil
}
func (bc *BlockController) resetTerminalState() {
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
var buf bytes.Buffer
// buf.WriteString("\x1b[?1049l") // disable alternative buffer
buf.WriteString("\x1b[0m") // reset attributes
buf.WriteString("\x1b[?25h") // show cursor
buf.WriteString("\x1b[?1000l") // disable mouse tracking
buf.WriteString("\r\n\r\n(restored terminal state)\r\n\r\n")
err := blockstore.GBS.AppendData(ctx, bc.BlockId, "main", buf.Bytes())
if err != nil {
log.Printf("error appending to blockfile (terminal reset): %v\n", err)
}
}
func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts) error {
// create a circular blockfile for the output
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
@@ -116,6 +132,10 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts) error {
if err != nil && err != blockstore.ErrAlreadyExists {
return fmt.Errorf("error creating blockfile: %w", err)
}
if err == blockstore.ErrAlreadyExists {
// reset the terminal state
bc.resetTerminalState()
}
if bc.getShellProc() != nil {
return nil
}