shell/xterm.go

141 lines
2.9 KiB
Go
Raw Normal View History

package shell
import (
"fmt"
"os"
"strings"
"go.wit.com/log"
)
func scanToParent(pid int) (bool, string) {
ppid, err := GetPPID(pid)
if err != nil {
fmt.Println("Error getting PPID:", err)
return true, ""
}
comm, err := GetComm(ppid)
if err != nil {
fmt.Println("Error getting comm:", err)
return true, ""
}
log.Info("scanToParent() found:", comm)
// if this is an xterm
switch comm {
case "xterm":
return true, "xterm"
case "mate-terminal":
return true, "mate-terminal"
case "bash":
// keep digging for the parent xterm
return scanToParent(ppid)
case "make":
// keep digging for the parent xterm
return scanToParent(ppid)
default:
return false, comm
}
if comm == "bash" {
}
ok, better := scanToParent(ppid)
if ok {
return ok, better
}
if better == "" {
// if the parent was blank, at least return the something that we have
// since something is better than nothing
return false, comm
}
return ok, better
}
// returns a string of the xterm the user is using
// for example, "xterm" or "mate-terminal"
func Terminal() string {
pid := os.Getpid()
_, comm := scanToParent(pid)
/*
2024-02-23 22:07:57 -06:00
ppid, err := GetPPID(pid)
if err != nil {
fmt.Println("Error getting PPID:", err)
return ""
}
2024-02-23 22:07:57 -06:00
comm, err := GetComm(ppid)
if err != nil {
fmt.Println("Error getting comm:", err)
return ""
}
*/
return comm
}
// returns a string of the shell the user is using
func Shell() string {
envsh := os.Getenv("SHELL")
switch envsh {
case "/bin/bash":
return "bash"
default:
return envsh
}
return envsh
}
func getXtermCmd(cmd []string) []string {
var term string
// doesn't work yet
// term = Terminal()
term = "xterm"
switch term {
case "mate-terminal":
newcmd := []string{"mate-terminal", "-e"}
newcmd = append(newcmd, cmd...)
return newcmd
default:
// unknown terminal. use xterm
newcmd := []string{"xterm", "-geometry", "140x32", "-e", "bash", "-c"}
// fix/shell escape sequence this for quote chars, etc
// tmp := "\"" + strings.Join(cmd, " ") + ";bash\""
newcmd = append(newcmd, cmd...)
// newcmd = append(newcmd, cmd...)
return newcmd
}
}
// spawns an xterm with something you can run at a command line
func XtermCmd(path string, cmd []string) {
go XtermCmdWait(path, cmd)
}
// runs an xterm
// waits until xterm exits
func XtermCmdWait(path string, cmd []string) {
var argsXterm = getXtermCmd(cmd)
log.Info("XtermCmd() path =", path, "cmd =", argsXterm)
// keeps git diff from exiting on small diffs
os.Setenv("LESS", "-+F -+X -R")
RunCmdRun(path, argsXterm)
}
// spawns an xterm with something you can run at a command line
// then executes bash
func XtermCmdBash(path string, cmd []string) {
var tmp []string
var argsXterm = getXtermCmd(cmd)
bash := "\"-c '"
bash += strings.Join(cmd, " ")
bash += "'; bash\""
tmp = append(argsXterm, "bash", bash)
log.Info("XtermCmd() path =", path, "cmd =", tmp)
go RunCmd(path, tmp)
}