Skip to content

Commit

Permalink
Strip tmux window
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Sep 28, 2021
1 parent edcf518 commit 0b0cde0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
11 changes: 3 additions & 8 deletions bot/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ const (
)

var (
// consoleCodeRegex is a regex describing console escape sequences that we're stripping out. This regex
// only matches ECMA-48 CSI sequences (ESC [ ... <char>), which is enough since, we're using tmux's capture-pane.
// See https://man7.org/linux/man-pages/man4/console_codes.4.html
consoleCodeRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)

// sendKeysMapping is a translation table that translates input commands "!<command>" to something that can be
// send via tmux's send-keys command, see https://man7.org/linux/man-pages/man1/tmux.1.html#KEY_BINDINGS
sendKeysMapping = map[string]string{
Expand Down Expand Up @@ -399,7 +394,7 @@ func (s *session) commandOutputLoop() error {
select {
case <-s.ctx.Done():
if lastID != "" {
_ = s.conn.Update(s.conf.terminal, lastID, util.FormatMarkdownCode(addExitedMessage(sanitizeWindow(last)))) // Show "(REPL exited.)" in terminal
_ = s.conn.Update(s.conf.terminal, lastID, util.FormatMarkdownCode(addExitedMessage(sanitizeWindow(removeTmuxBorder(last))))) // Show "(REPL exited.)" in terminal
}
return errExit
case <-s.forceResend:
Expand All @@ -420,11 +415,11 @@ func (s *session) maybeRefreshTerminal(last, lastID string) (string, string, err
current, err := s.tmux.Capture()
if err != nil {
if lastID != "" {
_ = s.conn.Update(s.conf.terminal, lastID, util.FormatMarkdownCode(addExitedMessage(sanitizeWindow(last)))) // Show "(REPL exited.)" in terminal
_ = s.conn.Update(s.conf.terminal, lastID, util.FormatMarkdownCode(addExitedMessage(sanitizeWindow(removeTmuxBorder(last))))) // Show "(REPL exited.)" in terminal
}
return "", "", errExit // The command may have ended, gracefully exit
}
current = s.maybeAddCursor(s.maybeTrimWindow(sanitizeWindow(current)))
current = s.maybeAddCursor(s.maybeTrimWindow(sanitizeWindow(removeTmuxBorder(current))))
if current == last {
return last, lastID, nil
}
Expand Down
16 changes: 16 additions & 0 deletions bot/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ import (
"os"
"regexp"
"strings"
"unicode"
)

var (
// consoleCodeRegex is a regex describing console escape sequences that we're stripping out. This regex
// only matches ECMA-48 CSI sequences (ESC [ ... <char>), which is enough since, we're using tmux's capture-pane.
// See https://man7.org/linux/man-pages/man4/console_codes.4.html
consoleCodeRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)

unquoteReplacer = strings.NewReplacer(
"\\n", "\n", // new line
"\\r", "\r", // line feed
"\\t", "\t", // tab
"\\b", "\b", // backspace
)
unquoteHexCharRegex = regexp.MustCompile(`\\x[a-fA-F0-9]{2}`)

tmuxWindowRegex = regexp.MustCompile(`│·*$|─+$|─*┘·*$|·+$|·*\(size \d+x\d+ from a smaller client\)\s*$`)
)

func addCursor(window string, x, y int) string {
Expand Down Expand Up @@ -99,6 +107,14 @@ func sanitizeWindow(window string) string {
return sanitized
}

func removeTmuxBorder(window string) string {
lines := strings.Split(window, "\n")
for i := range lines {
lines[i] = strings.TrimRightFunc(tmuxWindowRegex.ReplaceAllString(lines[i], ""), unicode.IsSpace)
}
return strings.Join(lines, "\n")
}

func zipAppendFile(zw *zip.Writer, name string, filename string) error {
f, err := os.Open(filename)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions bot/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,60 @@ func TestCropWindow(t *testing.T) {
actual := cropWindow(before, 500)
assert.Equal(t, expected, actual)
}

func TestRemoveTmuxBorder(t *testing.T) {
before := `
pheckel@plep ~/Code/replbot(main*) » │·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
│·····
──────────────────────────────────────────────────────────────────────────┘·····
················································································
················································································
················································································
················································································
··············································(size 74x18 from a smaller client)
`
expected := `
pheckel@plep ~/Code/replbot(main*) »
`
actual := removeTmuxBorder(before)
assert.Equal(t, expected, actual)
}

0 comments on commit 0b0cde0

Please sign in to comment.