diff options
| author | theimpostor <sahirhoda@gmail.com> | 2024-11-14 09:08:44 -0600 |
|---|---|---|
| committer | theimpostor <sahirhoda@gmail.com> | 2024-11-14 09:08:44 -0600 |
| commit | 809c1c44f55f9716493c936b104da402a7f53f82 (patch) | |
| tree | d3ff165c111cf9073c1992e65e39eebb54836a86 | |
| parent | 8a313c60d67353669509e9461d22b403df72b5e7 (diff) | |
Ignore initial metadata in paste response, and refactor terminal handling for improved readability
- Enhance code readability by introducing constants for escape sequences.
- Refactor relevant functions to utilize new constants for better maintainability.
- Improve error handling and processing logic through clearer comments.
| -rw-r--r-- | main.go | 47 |
1 files changed, 30 insertions, 17 deletions
@@ -20,9 +20,18 @@ import ( "github.com/spf13/cobra" ) +const ( + ESC = '\x1b' + BEL = '\a' + BS = '\\' + OSC = string(ESC) + "]52;" + DCS_OPEN = string(ESC) + "P" + DCS_CLOSE = string(ESC) + string(BS) +) + var ( - oscOpen string = "\x1b]52;c;" - oscClose string = "\a" + oscOpen string = OSC + "c;" + oscClose string = string(BEL) isScreen bool isTmux bool isZellij bool @@ -116,12 +125,12 @@ func identifyTerm() { if isScreen { debugLog.Println("Setting screen dcs passthrough") - oscOpen = "\x1bP" + oscOpen - oscClose = oscClose + "\x1b\\" + oscOpen = DCS_OPEN + oscOpen + oscClose = oscClose + DCS_CLOSE } else if isTmux { debugLog.Println("Setting tmux dcs passthrough") - oscOpen = "\x1bPtmux;\x1b" + oscOpen - oscClose = oscClose + "\x1b\\" + oscOpen = DCS_OPEN + "tmux;" + string(ESC) + oscOpen + oscClose = oscClose + DCS_CLOSE } } @@ -147,7 +156,7 @@ func (w *chunkingWriter) Write(p []byte) (n int, err error) { } else { bytesWritten, err = w.writer.Write(p[:nextChunkBoundary-w.bytesWritten]) if err == nil { - _, err = w.writer.Write([]byte("\x1b\\\x1bP")) + _, err = w.writer.Write([]byte(DCS_CLOSE + DCS_OPEN)) } } w.bytesWritten += int64(bytesWritten) @@ -271,26 +280,33 @@ func paste() error { fmt.Fprint(tty, oscOpen+"?"+oscClose) ttyReader := bufio.NewReader(tty) - buf := make([]byte, 0, 1024) // time out intial read - readChan := make(chan byte, 1) + readChan := make(chan []byte, 1) defer close(readChan) go func() { - if b, e := ttyReader.ReadByte(); e != nil { - debugLog.Println("Initial ReadByte error:", e) + if b, e := ttyReader.ReadSlice(';'); e != nil { + debugLog.Println("Initial ReadSlice error:", e) } else { readChan <- b } }() select { - case b := <-readChan: - buf = append(buf, b) + case <-readChan: + // TODO: check for osc header case <-time.After(timeout): debugLog.Println("tty read timeout") return nil, fmt.Errorf("tty read timeout") } + // ignore clipboard info + if _, e := ttyReader.ReadSlice(';'); e != nil { + errorLog.Println("ReadSlice:", e) + return nil, e + } + + buf := make([]byte, 0, 1024) + for { if b, e := ttyReader.ReadByte(); e != nil { errorLog.Println("ReadByte:", e) @@ -303,16 +319,13 @@ func paste() error { } buf = append(buf, b) // Skip initial 7 bytes of response - if len(buf) >= 9 && buf[len(buf)-2] == '\x1b' && buf[len(buf)-1] == '\\' { + if len(buf) >= 9 && buf[len(buf)-2] == ESC && buf[len(buf)-1] == BS { buf = buf[:len(buf)-2] break } } } - debugLog.Printf("buf[:7]: %q", buf[:7]) - buf = buf[7:] - decodedBuf := make([]byte, base64.StdEncoding.DecodedLen(len(buf))) n, err := base64.StdEncoding.Decode(decodedBuf, []byte(buf)) if err != nil { |
