Skip to content

Terminal control characters are not always escaped safely #156

Open
@mafredri

Description

@mafredri

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.

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.go:18>	main	...
  "msg": test1
eird     w
2022-11-10 18:10:11.256 [INFO]	<main.go:19>	main	test2 ...
  "boop": hello
ell       b
2022-11-10 18:10:11.256 [INFO]	<main.go:20>	main	...	{"boop": "hello\r\nb\rell\u0007"}
  "msg": test3
est      t
2022-11-10 18:10:11.257 [INFO]	<main.go:21>	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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions