Skip to content

Commit

Permalink
Change to suspend with SIGSTOP
Browse files Browse the repository at this point in the history
Change suspend from subshell to SIGSTOP suspend.
However, if Windows or OV_SUBSHELL is set, it will remain in subshell.

Solve #630.
  • Loading branch information
noborus committed Sep 29, 2024
1 parent 2c77138 commit ce280e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
36 changes: 26 additions & 10 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,38 @@ func specifyOnScreen(input string, max int) (int, error) {
// It will return when you exit the shell.
func (root *Root) suspend(context.Context) {
log.Println("Suspend")
shell := os.Getenv("SHELL")
if err := root.shellSuspendResume(shell); err != nil {
root.setMessageLog(err.Error())
}
log.Println("Resume")
}

func (root *Root) shellSuspendResume(shell string) error {
if err := root.Screen.Suspend(); err != nil {
return err
root.setMessageLog(err.Error())
return
}
defer func() error {
log.Println("Resume")
return root.Screen.Resume()
}()

fmt.Println("suspended ov")
subshell := os.Getenv("OV_SUBSHELL")
// If the OS is something other than Windows
// or if the environment variable Subshell is not set,
// suspend with sigstop.
if runtime.GOOS != "windows" && subshell != "1" {
fmt.Println("suspended ov (use 'fg' to resume)")
if err := suspendProcess(); err != nil {
root.setMessageLog(err.Error())
}
return
}

// If the OS is Windows,
// or if the environment variable Subshell is set,
// start the subshell.
fmt.Println("suspended ov (use 'exit' to resume)")
shell := os.Getenv("SHELL")
if err := root.subShell(shell); err != nil {
root.setMessageLog(err.Error())
}
}

func (root *Root) subShell(shell string) error {
if shell == "" {
if runtime.GOOS == "windows" {
shell = "CMD.EXE"
Expand Down
12 changes: 0 additions & 12 deletions oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oviewer

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -1286,17 +1285,6 @@ func TestRoot_modeConfig(t *testing.T) {
}
}

func TestRoot_suspend(t *testing.T) {
root := rootHelper(t)
shell := os.Getenv("SHELL")
if got := root.shellSuspendResume(shell); got != nil {
t.Errorf("shellSuspendResume() = %v, want %v", got, nil)
}
if got := root.shellSuspendResume(""); got != nil {
t.Errorf("shellSuspendResume() = %v, want %v", got, nil)
}
}

func TestRoot_follow(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
Expand Down
9 changes: 9 additions & 0 deletions oviewer/sigtstp.go → oviewer/suspend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ func registerSIGTSTP() chan os.Signal {
signal.Notify(sigSuspend, syscall.SIGTSTP)
return sigSuspend
}

// suspendProcess sends SIGSTOP signal to itself.
func suspendProcess() error {
pid := syscall.Getpid()
if err := syscall.Kill(pid, syscall.SIGSTOP); err != nil {
return err
}
return nil
}
5 changes: 5 additions & 0 deletions oviewer/sigtstp_windows.go → oviewer/suspend_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ func registerSIGTSTP() chan os.Signal {
sigSuspend := make(chan os.Signal, 1)
return sigSuspend
}

// suspendProcess is a dummy function.
func suspendProcess() error {
return nil
}

0 comments on commit ce280e4

Please sign in to comment.