-3

It happens very often that output in terminals has ANSI control sequences that gives it better readability, different colors, bold, underline, hence I think it would be interesting to be able to benefit this in questions and answers.

Here's an example dmesg -H output, but of course it doesn't work right now and will be atrociously ugly :

[32m[1m[Aug22 16:11] [0m[33mpci 0000:01:00.0[0m: [10de:1198] type 00 class 0x030000
[32m[  +0.000036] [0m[33mpci 0000:01:00.0[0m: reg 0x10: [mem 0xf6000000-0xf6ffffff]
[32m[  +0.000017] [0m[33mpci 0000:01:00.0[0m: reg 0x14: [mem 0xe0000000-0xefffffff 64bit pref]
[32m[  +0.000016] [0m[33mpci 0000:01:00.0[0m: reg 0x1c: [mem 0xf0000000-0xf1ffffff 64bit pref]
[32m[  +0.000012] [0m[33mpci 0000:01:00.0[0m: reg 0x24: [io  0xe000-0xe07f]
[32m[  +0.000012] [0m[33mpci 0000:01:00.0[0m: reg 0x30: [mem 0xf7000000-0xf707ffff pref]
[32m[  +0.000052] [0m[33mpci 0000:01:00.0[0m: Enabling HDA controller
[32m[  +0.000087] [0m[33mpci 0000:01:00.0[0m: 32.000 Gb/s available PCIe bandwidth, limited by 2.5 GT/s x16 link at 0000:00:01.0 (capable of 126.016 Gb/s with 8 GT/s x16 link)
[32m[  +0.000486] [0m[33mpci 0000:01:00.0[0m: vgaarb: VGA device added: decodes=io+mem,owns=none,locks=none
[32m[  +0.000011] [0m[33mi915 0000:00:02.0[0m: vgaarb: changed VGA decodes: olddecodes=io+mem,decodes=none:owns=io+mem
[32m[  +0.000160] [0m[33mpci 0000:01:00.1[0m: [10de:0e0a] type 00 class 0x040300
[32m[  +0.000029] [0m[33mpci 0000:01:00.1[0m: reg 0x10: [mem 0x00000000-0x00003fff]
[32m[  +0.000057] [0m[33mpci 0000:01:00.1[0m: Max Payload Size set to 256 (was 128, max 256)
[32m[  +0.000382] [0m[33mpcieport 0000:00:01.0[0m[1m: ASPM: current common clock configuration is broken, reconfiguring[0m
[32m[  +0.009663] [0m[33mpci 0000:01:00.0[0m: BAR 1: assigned [mem 0xe0000000-0xefffffff 64bit pref]
[32m[  +0.000037] [0m[33mpci 0000:01:00.0[0m: BAR 3: assigned [mem 0xf0000000-0xf1ffffff 64bit pref]
[32m[  +0.000018] [0m[33mpci 0000:01:00.0[0m: BAR 0: assigned [mem 0xf6000000-0xf6ffffff]
[32m[  +0.000003] [0m[33mpci 0000:01:00.0[0m: BAR 6: assigned [mem 0xf7000000-0xf707ffff pref]
[32m[  +0.000002] [0m[33mpci 0000:01:00.1[0m: BAR 0: assigned [mem 0xf7080000-0xf7083fff]
[32m[  +0.000002] [0m[33mpci 0000:01:00.0[0m: BAR 5: assigned [io  0xe000-0xe07f]
[32m[  +0.000004] [0m[33mpcieport 0000:00:01.0[0m: PCI bridge to [bus 01]
[32m[  +0.000001] [0m[33mpcieport 0000:00:01.0[0m:   bridge window [io  0xe000-0xefff]
[32m[  +0.000003] [0m[33mpcieport 0000:00:01.0[0m:   bridge window [mem 0xf6000000-0xf70fffff]
[32m[  +0.000002] [0m[33mpcieport 0000:00:01.0[0m:   bridge window [mem 0xe0000000-0xf1ffffff 64bit pref]
[32m[  +0.000176] [0m[33mpci 0000:01:00.1[0m: D0 power state depends on 0000:01:00.0
[32m[  +0.000036] [0m[33msnd_hda_intel 0000:01:00.1[0m: enabling device (0000 -> 0002)
[32m[  +0.000057] [0m[33msnd_hda_intel 0000:01:00.1[0m: Disabling MSI
[32m[  +0.000006] [0m[33msnd_hda_intel 0000:01:00.1[0m: Handle vga_switcheroo audio client
[32m[  +0.041484] [0m[33mIPMI message handler[0m: version 39.2
[32m[  +0.016187] [0mipmi device interface
[32m[  +0.704043] [0m[33mnvidia[0m[1m: module license 'NVIDIA' taints kernel.[0m
[32m[  +0.000001] [0m[1mDisabling lock debugging due to kernel taint[0m
[32m[  +0.012267] [0m[33mnvidia-nvlink[0m: Nvlink Core is being initialized, major device number 237
[32m[  +0.000320] [0m[33mnvidia 0000:01:00.0[0m: enabling device (0006 -> 0007)
[32m[  +0.000078] [0m[33mnvidia 0000:01:00.0[0m: vgaarb: changed VGA decodes: olddecodes=io+mem,decodes=none:owns=none
[32m[  +0.099429] [0m[33mNVRM[0m[1m: loading NVIDIA UNIX x86_64 Kernel Module  440.100  Fri May 29 08:45:51 UTC 2020[0m
[32m[  +0.055239] [0m[33mnvidia-modeset[0m: Loading NVIDIA Kernel Mode Setting Driver for UNIX platforms  440.100  Fri May 29 08:14:04 UTC 2020
[32m[  +0.002640] [0m[drm] [nvidia-drm] [GPU ID 0x00000100] Loading driver
[32m[  +0.020768] [0m[33mACPI Warning[0m[1m: \_SB.PCI0.PEG0.PEGP._DSM: Argument #4 type mismatch - Found [Buffer], ACPI requires [Package] (20190816/nsarguments-59)[0m
[32m[ +30.855545] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x24:0x65:1185)[0m
[32m[  +0.000034] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +0.000492] [0m[31m[drm:nv_drm_load [nvidia_drm]] *ERROR* [nvidia-drm] [GPU ID 0x00000100] Failed to allocate NvKmsKapiDevice[0m
[32m[  +0.000122] [0m[31m[drm:nv_drm_probe_devices [nvidia_drm]] *ERROR* [nvidia-drm] [GPU ID 0x00000100] Failed to register device[0m
[32m[  +0.071852] [0m[33mnvidia-uvm[0m: Loaded the UVM driver, major device number 235.
[32m[1m[Aug22 16:14] [0m[33mLockdown[0m: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
[32m[  +3.384946] [0m[33mrfkill[0m: input handler enabled
[32m[  +9.980611] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000047] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.167869] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000075] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +0.099416] [0m[33mgnome-shell[6474][0m[2m[31m: segfault at 20 ip 00007f4d29a83356 sp 00007ffd43c62db0 error 4 in libnvidia-glsi.so.440.100[7f4d29a21000+95000][0m
[32m[  +0.000004] [0m[33mCode[0m: 48 8b 5c 24 08 48 8b 6c 24 10 48 83 c4 18 c3 0f 1f 44 00 00 48 8b 3d f1 31 24 00 e8 74 31 00 00 89 de 48 89 c7 e8 5a fe ff ff <48> 8b 78 20 e8 61 60 01 00 48 83 f8 01 48 89 45 00 19 c0 83 e0 0f
[32m[  +4.409548] [0m[33mLockdown[0m: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
[32m[  +0.281982] [0m[33mrfkill[0m: input handler disabled
[32m[ +10.468885] [0m[33mrfkill[0m: input handler enabled
[32m[1m[Aug22 16:15] [0m[33mLockdown[0m: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
[32m[  +0.355605] [0m[33mrfkill[0m: input handler disabled
[32m[  +8.056674] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000069] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.172351] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000030] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.171805] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000022] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.167969] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000022] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.171784] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000070] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.168094] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000023] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.171857] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000031] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[1m[Aug22 16:16] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000025] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.171680] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000023] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m
[32m[  +8.171964] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: RmInitAdapter failed! (0x26:0x65:1227)[0m
[32m[  +0.000039] [0m[33mNVRM[0m[1m: GPU 0000:01:00.0: rm_init_adapter failed, device minor number 0[0m

EDIT : Imho, it should work this way : the code block is started with ```ansi and finishes with ```, which is consistent with current usage, explicitly requesting ANSI escape sequences parsing.

Now, is it worth it in terms of development ? I'm pretty sure there's already much code in the open addressing the problem of ANSI sequences parsing, maybe even libraries. It's already possible to copy the output from the GNOME terminal as HTML with colour tags (that aren't recognised here afaik), hence much of the work has already been done somewhere.

So yes, imo it is worth it, the investment wouldn't be that big and the results of having coloured + emphasized console output posted instead of the current monochrome would be beneficial.

That being said, I'm not the one who would have to implement this, and only those people could say for sure.

6
  • just an aside, add --color=never to dmesg arguments and it removes all those control chars ;)
    – Thomas Ward Mod
    Aug 23, 2020 at 22:14
  • @ThomasWard In fact when redirecting output to a file/pipe, control chars are automatically removed, as they are when copied from a terminal window, so that's not the problem. I had to explicitly request the colors with -L always to get the above result, because I think it could add much readability if the ANSI escape sequences were interpreted.
    – NovHak
    Aug 23, 2020 at 22:24
  • with piped output yes, that's true, but if you're copy/pasting like you just did which gets the control chars, you can fix that by adding the 'never' to the colors and it won't include them.
    – Thomas Ward Mod
    Aug 23, 2020 at 22:38
  • I didn't copy-paste from the terminal, I did dmesg -H -L=always >dmesg.txt, then opened the file in Emacs, copied part of it in the clipboard and pasted here. I suppose you don't use the Gnome terminal, as I just tried to copy and paste into Emacs from a Gnome terminal window, and only got the text stripped of all ANSI sequences... or maybe you use a special configuration in Gnome terminal ?
    – NovHak
    Aug 23, 2020 at 22:55
  • When you copy out of GNOME Terminal it usually is smart enough to not copy the control characters. That's the difference between piping directly and such.
    – Thomas Ward Mod
    Aug 23, 2020 at 22:58
  • Ah my bad I misread your previous comment, we're on the same line then :)
    – NovHak
    Aug 23, 2020 at 23:02

1 Answer 1

4

tl;dr I don't believe it's worth implementing this.

If we just search for 0m* on main, you get 304 results. Keeping in mind that those 304 results including 0m are not supposed to be coloured outputs, still, 304 results is a tiny number when compared to the 358k questions and 450k answers Ask Ubuntu has.

From a functionality perspective, how would would it work?

  • Would the output be parsed to remove the colour codes so the text would be coloured?

    This would be a problem because the purpose of code blocks is to show the text as is without any parsing whatsoever.

  • Would the output stay as is but coloured?

    This would be a problem because the output gets cluttered with unnecessary information. So it will make it less readable

On a brighter note, a workaround these two problems could be to use a completely new formatting directive, for example, encapsulating log output with triple tildes (~~~) instead of triple backticks (```).

However, I still think it's not worth it and development work is better spent on other problems.

When a coloured output is required to help users fix an issue - if that ever happens - screenshots, gifs or even https://asciinema.org/ can be a solution.

* I chose 0m because that's part of the code used for to reset all formatting so it would be the most frequent code used when colouring is involved. https://www.growingwiththeweb.com/2015/05/colours-in-gnome-terminal.html

1
  • It's fortunate that you find only 304 results, since in the current situation those control characters are useless and indeed add unnecessary clutter here, hence should not be posted. The problem is not to address the 304 cases of people misposting ANSI escape sequences, but rather putting these sequences to good use. I updated my question regarding your other concerns.
    – NovHak
    Aug 24, 2020 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .