mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-22 20:34:26 +00:00
ascii to str
This commit is contained in:
parent
70a264ed00
commit
8a7fa9ecb9
65
utils/log.go
65
utils/log.go
@ -1,7 +1,35 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
func FormatWritesLog(line *[]byte) (log string) {
|
import "fmt"
|
||||||
ascii := map[byte]string{
|
|
||||||
|
func FormatWritesLog(codes []byte, line *string) {
|
||||||
|
n := len(codes)
|
||||||
|
str := ""
|
||||||
|
exist := false
|
||||||
|
if n == 3 {
|
||||||
|
if str, exist = ASCIIGroupToStr(fmt.Sprintf("%X", codes)); exist {
|
||||||
|
*line += str
|
||||||
|
codes = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// sh line prefix
|
||||||
|
if n >= 6 {
|
||||||
|
if str, exist = ASCIIGroupToStr(fmt.Sprintf("%X", []byte{codes[0], codes[1], codes[n-1]})); exist {
|
||||||
|
*line += str
|
||||||
|
codes = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if codes != nil {
|
||||||
|
str = ASCIIToStr(codes)
|
||||||
|
*line += str
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ASCIIToStr(codes []byte) string {
|
||||||
|
control := map[byte]string{
|
||||||
0: "NUL",
|
0: "NUL",
|
||||||
1: "SOH",
|
1: "SOH",
|
||||||
2: "STX",
|
2: "STX",
|
||||||
@ -36,13 +64,32 @@ func FormatWritesLog(line *[]byte) (log string) {
|
|||||||
31: "US",
|
31: "US",
|
||||||
127: "DEL",
|
127: "DEL",
|
||||||
}
|
}
|
||||||
for _, word := range *line {
|
|
||||||
if value, ok := ascii[word]; ok {
|
|
||||||
log += value
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log += string(word)
|
str := ""
|
||||||
|
for _, code := range codes {
|
||||||
|
if value, ok := control[code]; ok {
|
||||||
|
str += value
|
||||||
|
} else {
|
||||||
|
str += string(code)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func ASCIIGroupToStr(sum string) (string, bool) {
|
||||||
|
group := map[string]string{
|
||||||
|
"1B5B41": "UP",
|
||||||
|
"1B5B42": "DOWN",
|
||||||
|
"1B5B43": "RIGHT",
|
||||||
|
"1B5B44": "LEFT",
|
||||||
|
// sh line prefix: codes[0]codes[1]codes[5]
|
||||||
|
// eg. "ESC[ 1;5 R"
|
||||||
|
"1B5B52": "",
|
||||||
|
}
|
||||||
|
if value, ok := group[sum]; ok {
|
||||||
|
return value, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
|
@ -91,14 +91,14 @@ func (wt *WebTTY) Run(ctx context.Context) error {
|
|||||||
go func() {
|
go func() {
|
||||||
errs <- func() error {
|
errs <- func() error {
|
||||||
buffer := make([]byte, wt.bufferSize)
|
buffer := make([]byte, wt.bufferSize)
|
||||||
lineBuffer := make([]byte, 1)
|
line := ""
|
||||||
for {
|
for {
|
||||||
n, err := wt.masterConn.Read(buffer)
|
n, err := wt.masterConn.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrMasterClosed
|
return ErrMasterClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wt.handleMasterReadEvent(buffer[:n], &lineBuffer)
|
err = wt.handleMasterReadEvent(buffer[:n], &line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ func (wt *WebTTY) masterWrite(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error {
|
func (wt *WebTTY) handleMasterReadEvent(data []byte, line *string) error {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return errors.New("unexpected zero length read from master")
|
return errors.New("unexpected zero length read from master")
|
||||||
}
|
}
|
||||||
@ -188,15 +188,15 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error {
|
|||||||
return errors.Wrapf(err, "failed to decode received data")
|
return errors.Wrapf(err, "failed to decode received data")
|
||||||
}
|
}
|
||||||
|
|
||||||
*line = append(*line, decodedBuffer[:n]...)
|
// log.Printf("[wlog] %v %X\n", decodedBuffer[:n], decodedBuffer[:n])
|
||||||
|
utils.FormatWritesLog(decodedBuffer[:n], line)
|
||||||
if decodedBuffer[n-1] == 13 {
|
if decodedBuffer[n-1] == 13 {
|
||||||
argumentsByte, err := json.Marshal(wt.arguments)
|
argumentsByte, err := json.Marshal(wt.arguments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to marshal arguments map")
|
return errors.Wrapf(err, "failed to marshal arguments map")
|
||||||
}
|
}
|
||||||
//log.Printf("[wlog] %v\n", line)
|
log.Printf("[wlog] %s %s\n", *line, string(argumentsByte))
|
||||||
log.Printf("[wlog] %s %s\n", utils.FormatWritesLog(line), string(argumentsByte))
|
*line = ""
|
||||||
*line = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = wt.slave.Write(decodedBuffer[:n])
|
_, err = wt.slave.Write(decodedBuffer[:n])
|
||||||
|
Loading…
Reference in New Issue
Block a user