跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

window.go

164 行 · 4.1 KB历史下载
1package ui
2
3import (
4 "bytes"
5 "fmt"
6 "io"
7 "os"
8 "strings"
9 "unicode"
10
11 "golang.org/x/term"
12)
13
14// Window copies what is written to log and, on a terminal, shows only the
15// last lines, redrawn in place and cleared at Close; elsewhere it passes
16// every line through. Lines are cut to the terminal's width, since a wrapped
17// one would break the redraw, and colour codes are dropped, since a cut one
18// would leave the terminal coloured.
19type Window struct {
20 out io.Writer
21 log io.Writer
22 tty bool
23 width, height int
24 lines []string
25 drawn int
26 partial []byte
27}
28
29// IsTerminal reports whether f is a terminal.
30func IsTerminal(f *os.File) bool { return term.IsTerminal(int(f.Fd())) }
31
32// NewWindow writes to out, a terminal or not, and to log.
33func (u *UI) NewWindow(out io.Writer, log io.Writer) *Window {
34 w := &Window{out: out, log: log, width: 80, height: 10}
35 if f, ok := out.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
36 w.tty = true
37 if t, err := u.open(); err == nil {
38 if fd, ok := t.(interface{ Fd() uintptr }); ok {
39 if cols, rows, err := term.GetSize(int(fd.Fd())); err == nil {
40 if cols > 1 {
41 w.width = cols
42 }
43 if rows < w.height+2 {
44 w.height = 1
45 if rows > 3 {
46 w.height = rows - 2
47 }
48 }
49 }
50 }
51 t.Close()
52 }
53 }
54 return w
55}
56
57func (w *Window) Write(p []byte) (int, error) {
58 w.log.Write(p)
59 if !w.tty {
60 return w.out.Write(p)
61 }
62 w.partial = append(w.partial, p...)
63 for {
64 i := bytes.IndexByte(w.partial, '\n')
65 if i < 0 {
66 break
67 }
68 w.show(string(w.partial[:i]))
69 w.partial = w.partial[i+1:]
70 }
71 return len(p), nil
72}
73
74// show adds a line to the window and redraws it.
75func (w *Window) show(line string) {
76 // A progress bar redraws with \r: keep what the last redraw left.
77 line = strings.TrimSuffix(line, "\r")
78 if i := strings.LastIndexByte(line, '\r'); i >= 0 {
79 line = line[i+1:]
80 }
81 // Lima logs `time="…" level=info msg="…" key=value`: the message is
82 // what fits and what says something.
83 if strings.HasPrefix(line, "time=") {
84 if _, m, ok := strings.Cut(line, ` msg="`); ok {
85 line, _, _ = strings.Cut(m, `"`)
86 }
87 }
88 line = strings.ReplaceAll(plain(line), "\t", " ")
89 if r := []rune(line); len(r) > w.width-1 {
90 line = string(r[:w.width-1])
91 }
92 if len(w.lines) == w.height {
93 w.lines = w.lines[1:]
94 }
95 w.lines = append(w.lines, line)
96 if w.drawn > 0 {
97 fmt.Fprintf(w.out, "\033[%dA", w.drawn)
98 }
99 fmt.Fprintf(w.out, "\r\033[J\033[2m%s\033[0m", strings.Join(w.lines, "\n")+"\n")
100 w.drawn = len(w.lines)
101}
102
103// plain is s without what could drive a terminal: CSI sequences (stripCSI),
104// then every control character but tab, C1 included (U+009B is CSI to some
105// terminals); invalid UTF-8 becomes U+FFFD.
106func plain(s string) string {
107 return strings.Map(func(r rune) rune {
108 if r != '\t' && unicode.IsControl(r) {
109 return -1
110 }
111 return r
112 }, stripCSI(s))
113}
114
115// stripCSI drops each ESC, and the [...letter that follows it when there is
116// one.
117func stripCSI(s string) string {
118 for {
119 i := strings.IndexByte(s, 0x1b)
120 if i < 0 {
121 return s
122 }
123 rest := s[i+1:]
124 if strings.HasPrefix(rest, "[") {
125 if j := strings.IndexFunc(rest[1:], func(r rune) bool { return r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' }); j >= 0 {
126 rest = rest[j+2:]
127 }
128 }
129 s = s[:i] + rest
130 }
131}
132
133// Close shows a last line without a newline, then clears the window.
134func (w *Window) Close() {
135 if !w.tty {
136 return
137 }
138 if len(w.partial) > 0 {
139 w.show(string(w.partial))
140 w.partial = nil
141 }
142 if w.drawn > 0 {
143 fmt.Fprintf(w.out, "\033[%dA\r\033[J", w.drawn)
144 w.drawn = 0
145 }
146}
147
148// Tail is the last n lines of the file at p, for when a step failed and
149// its window is gone. Cleaned as the window cleans them: the log holds what
150// the guest printed, and printing it raw would let it drive the terminal.
151func Tail(p string, n int) string {
152 b, err := os.ReadFile(p)
153 if err != nil {
154 return ""
155 }
156 lines := strings.Split(strings.TrimSuffix(string(b), "\n"), "\n")
157 if len(lines) > n {
158 lines = lines[len(lines)-n:]
159 }
160 for i, l := range lines {
161 lines[i] = plain(l)
162 }
163 return strings.Join(lines, "\n") + "\n"
164}