1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
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)
/*
ppid, err := GetPPID(pid)
if err != nil {
fmt.Println("Error getting PPID:", err)
return ""
}
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")
PathRun(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 PathRun(path, tmp)
}
|