ITADN

Improve compatibility with `less -R`

#95Closedme-and 创建于 2024-11-26
M
me-andcommented
`nix-diff --color always` is really useful for being able to pipe large diffs through a pager that can handle colour escapes. Historically, I've used `less -R` for this purpose. Unfortunately, from the `less` man page: > Color escape sequences are only supported when the color is changed within one line, not across lines. In other words, the beginning of each line is assumed to be normal (non-colored), regardless of any escape sequences in previous lines. I've seen this result in misleading output when using `less -R` with `nix-diff`, because differences that should be highlighted aren't. As a simple example, consider the following file: ```nix let pkgs = import <nixpkgs> {}; in { a = pkgs.writeTextFile { name = "alpha"; text = '' one three five ''; }; b = pkgs.writeTextFile { name = "alpha"; text = '' one two three ''; }; } ``` Running `nix-diff --character-oriented --color always "$(nix-instantiate -A a test.nix)" "$(nix-instantiate -A b test.nix)"` has everything coloured correctly. Running `nix-diff --character-oriented --color always "$(nix-instantiate -A a test.nix)" "$(nix-instantiate -A b test.nix)" | less -R`, however, gets a bunch of the colours wrong. My Haskell is *painfully* rusty, so I've not tried to work out how difficult this would actually be, but if `nix-diff` were to repeat the current colour escape sequence at the start of each line, that would mean `less` could display everything correctly. And hopefully that's not something that's too difficult to add...
关闭于 2024-12-20 0 条评论