-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk-reader.go
182 lines (159 loc) · 6.24 KB
/
chunk-reader.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"io"
"os"
"os/exec"
)
func StripANSI(input []byte) []byte {
output := make([]byte, 0, len(input))
inSequence := false
for _, b := range input {
if b == 0x1b { // ESC character
inSequence = true
continue
}
if inSequence {
if (b >= 0x40 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a) {
inSequence = false
}
continue
}
output = append(output, b)
}
return output
}
func main() {
buffer := make([]byte, 1024) // Adjust buffer size as needed
userInputMode := false
llmMsg := []byte{} //empty byte slice
llmMsg_size := 0
for {
n, err := os.Stdin.Read(buffer)
if err != nil {
if err == io.EOF {
break
}
fmt.Fprintf(os.Stderr, "Error reading from stdin: %v\n", err)
os.Exit(1)
}
/*
Our job is to append a newline to everything, so it gets instantly written out by batcat.
But new issue: if its instantly written out, it doens't have syntax highlighting context.
Since syntax highlighting is applied in chunks. Somehow. Or it depends on newlines.
*/
if n > 0 {
// fmt.Printf("Size: %d %02X",n,buffer[0])
// if (n>20) {
// fmt.Println()
// fmt.Println()
// }
// fmt.Printf("%02X\n",buffer[0]);
// Write the chunk
msg := buffer[:n]
//Inbetween or LLM
if ( /*(buffer[0] != 0x1B || n==4)*/ !userInputMode ) {
if ( n>20 ) {
if (llmMsg_size >0) {
//syntaxhighlight dump here
stripped := string(StripANSI(llmMsg[:llmMsg_size]))
language := os.Getenv("OL_LANG")
if (language == "") {
language = "fstab"
}
batCmd := exec.Command("/usr/bin/pipetty","/usr/bin/batcat", "-ppP", "-l"+language)
// batCmd := exec.Command("/usr/bin/xxd")
stdin, err := batCmd.StdinPipe()
if err != nil {
fmt.Println("Error obtaining stdin pipe:", err)
return
}
batStdout, err := batCmd.StdoutPipe()
if err != nil {
fmt.Println("Error obtaining stdbatStdoutout pipe:", err)
return
}
if err := batCmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
// Send input to the process
go func() {
defer stdin.Close()
io.WriteString(stdin, stripped)
}()
// Read the output from the process
batOutput, err := io.ReadAll(batStdout)
if err != nil {
fmt.Println("Error reading batStdout:", err)
return
}
if err := batCmd.Wait(); err != nil {
fmt.Println("Error waiting for command to finish:", err)
return
}
fmt.Println()
fmt.Println("==========================================")
fmt.Println()
fmt.Println("==========================================")
os.Stdout.Write(batOutput)
}
//--This is a inbetween message.--
//clear slice
llmMsg_size = 0
// llmMsg = nil
userInputMode = true
// we can't flush on each word, cos of syntax highlighting.
// we could flush every comma and full stop mb.
// fmt.Println("Writing Inbetween")
os.Stdout.Write(msg)
// fmt.Println()
// os.Stdout.Sync()
} else {
//----LLM Output----
os.Stdout.Write(msg)
//First output will be considered LLM, but isnt'. (>>>)
if (buffer[0] != 0x1B) {
// What if we save this and parse to batcat?
llmMsg = append(llmMsg[:llmMsg_size],msg...)
llmMsg_size += n
//fmt.Println()
//os.Stdout.Sync()
} else {
// fmt.Println("Writing >>>")
// for i, b := range msg {
// fmt.Printf("byte[%d] = %02X\n", i, b)
// }
}
}
} else {
//----User is typing----
if (n==1 ||n==4) {
os.Stdout.Write(msg)
// fmt.Println()
// os.Stdout.Sync()
if (buffer[0] == '\n') {
//Restore LLM mode
// fmt.Println("Restoring LLM detected newline")
userInputMode = false
}
} else {
os.Stdout.Write(msg)
/*
fmt.Println("Restoring LLM for unrecognised sequence")
//Restore LLM mode
os.Stdout.Write(msg)
userInputMode = false
*/
}
}
// if ( n > 20 ) {
// fmt.Println()
// } else {
// os.Stdout.WriteString("\x03")
// // Ensure it's flushed to stdout
// os.Stdout.Sync()
// }
}
}
}