forked from mikhailt/tabby
/
tools.go
101 lines (93 loc) · 1.73 KB
/
tools.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"os"
"os/exec"
"io"
"bytes"
"path/filepath"
"github.com/mattn/go-gtk/gtk"
)
func gofmt_all() {
for file, _ := range file_map {
gofmt(file)
}
file_tree_store()
}
func gofmt(file string) {
rec, _ := file_map[file]
var buf []byte
if file == cur_file {
buf = []byte(get_source())
} else {
buf = rec.buf
}
bin := os.Getenv("GOBIN")
if bin != "" {
bin = filepath.Join(bin, "gofmt")
} else {
bin, _ = exec.LookPath("gofmt")
}
std, error, e := get_output([]string{bin}, buf)
if e != nil {
tabby_log(e.Error())
return
}
if file == cur_file {
error_buf.SetText(string(error))
} else {
rec.error = error
}
if 0 != len(error) {
return
}
if file == cur_file {
if string(buf) != string(std) {
var be, en gtk.TextIter
source_buf.GetSelectionBounds(&be, &en)
sel_be := be.GetOffset()
source_buf.SetText(string(std))
source_buf.GetIterAtOffset(&be, sel_be)
move_focus_and_selection(&be, &be)
}
} else if string(rec.buf) != string(std) {
rec.buf = std
rec.modified = true
}
}
func get_output(args []string, input []byte) (std []byte, error []byte, e error) {
inpr, inpw, err := os.Pipe()
if err != nil {
return nil, nil, err
}
stdr, stdw, err := os.Pipe()
if err != nil {
return nil, nil, err
}
errr, errw, err := os.Pipe()
if err != nil {
return nil, nil, err
}
pid, err := os.StartProcess(args[0], args, &os.ProcAttr{
Dir: ".",
Env: os.Environ(),
Files: []*os.File{inpr, stdw, errw},
})
if err != nil {
return nil, nil, err
}
inpw.Write(input)
inpw.Close()
stdw.Close()
errw.Close()
var b bytes.Buffer
io.Copy(&b, stdr)
std = b.Bytes()
b.Reset()
io.Copy(&b, errr)
error = b.Bytes()
inpr.Close()
stdr.Close()
errr.Close()
pid.Wait()
return
}