coder / coder/slog

Terminal control characters are not always escaped safely

Open
#156 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
351
Forks
23
PR merge metrics
No merged PRs in 30d

Description

Not sure whether or not this is considered a feature or a bug, but in certain scenarios, terminal escape chars are interpreted as-is, and in other they're escaped into string form.

```go
package main

import (
"context"
"os"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
)

func main() {
ctx := context.Background()
log := slog.Make(sloghuman.Sink(os.Stderr))
log.Info(ctx, "test1\r\nw\reird\a")
log.Info(ctx, "test2", slog.F("boop", "hello\r\nb\rell\a"))
log.Info(ctx, "test3\r\nt\rest", slog.F("boop", "hello\r\nb\rell\a"))
log.Info(ctx, "test4", slog.F("clipboard", "\033]52;c;surprise!\a"))
}

// Output:
2022-11-10 18:10:11.256 [INFO] main ...
"msg": test1
eird w
2022-11-10 18:10:11.256 [INFO] main test2 ...
"boop": hello
ell b
2022-11-10 18:10:11.256 [INFO] main ... {"boop": "hello\r\nb\rell\u0007"}
"msg": test3
est t
2022-11-10 18:10:11.257 [INFO] main test4 {"clipboard": "\u001b]52;c;surprise!\u0007"}
```

Pretty printing `\r\n` seems intended and like a nice feature (since it's aligned), but stray `\r` and also `\a` are interpreted as-is. The latter (`\a`) will also ring the terminal bell which I would venture is never the expectation.

Out of these 4 cases, only the clipboard case behaved as I would expect.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.