-
-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathtmux.go
85 lines (73 loc) · 1.83 KB
/
tmux.go
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
package core
import (
"fmt"
"strings"
"github.com/j3ssie/osmedeus/libs"
"github.com/j3ssie/osmedeus/utils"
)
type Tmux struct {
ApplyAll bool
SelectedWindow string
Exclude string
Limit int
Windows []string
}
func InitTmux(options libs.Options) (Tmux, error) {
cmd := "tmux ls"
var tmux Tmux
tmux.ApplyAll = options.Tmux.ApplyAll
tmux.SelectedWindow = options.Tmux.SelectedWindow
tmux.Exclude = options.Tmux.Exclude
tmux.Limit = options.Tmux.Limit
raw := utils.RunCmdWithOutput(cmd)
if strings.Contains(raw, "command not found") || !strings.Contains(raw, "\n") {
return tmux, fmt.Errorf("tmux program not installed")
}
stds := strings.Split(raw, "\n")
for _, line := range stds {
if strings.TrimSpace(line) == "" || !strings.Contains(line, " ") {
continue
}
data := strings.Split(line, " ")
tmux.Windows = append(tmux.Windows, strings.TrimRight(data[0], ":"))
}
return tmux, nil
}
func (t *Tmux) ListTmux() {
if len(t.Windows) == 0 {
fmt.Println("No tmux available")
return
}
fmt.Println(strings.Join(t.Windows, ", "))
}
func (t *Tmux) CatchSession() string {
var result string
if len(t.Windows) == 0 {
fmt.Println("No tmux available")
return result
}
for _, window := range t.Windows {
utils.DebugF("Get info of: %s", window)
if !t.ApplyAll {
if window != t.SelectedWindow {
continue
}
}
if strings.HasPrefix(window, t.Exclude) {
continue
}
utils.InforF("Get output of %s session", window)
cmd := fmt.Sprintf(`tmux capture-pane -pt "%s"`, window)
raw := utils.RunCmdWithOutput(cmd)
data := strings.Split(raw, "\n")
if t.Limit == 0 {
fmt.Println(raw)
} else if t.Limit < len(data) && t.Limit > 0 {
fmt.Println(strings.Join(data[len(data)-t.Limit:len(data)-1], "\n"))
} else {
fmt.Println(raw)
}
result += raw
}
return result
}