Skip to content

Cut potentially colored output of a program to a visible number of columns.

Notifications You must be signed in to change notification settings

fhars/color-cut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

color-cut

This utility is a line oriented filter that cuts the input lines to a given length. On pure ASCII text, color-cut 20 will behave the same as the standard unix utility cut called with cut -c 1-20. Unlike cut, color-cut does not assume that every byte in the input is a character. Instead, it assumes that the input contains valid utf-8 text interspersed with ANSI control sequences. It will pass color control seqences to the output, and count double width unicode characters as two columns.

Installation

This is a very simple rust programm. If you have cargo installed, you can install it locally with

cargo install --path .

in the root of the cloned repository.

Motivation

Some time ago, I gave a talk on the datamodel behind git, see https://media.ccc.de/v/DiVOC-5-ein_kurzer_blick_in_git, and I wanted to display the effects on the content of the .git subdirectory while going through some common sequenences of git commands in a repository. I was already running tmux in the shell window for the presentation to be able to script all the shell commands, so I could just split the window in a large pane für the shell session and a smaller sidebar displaying something like watch -tc tree -C -I"logs|hooks|info" .git and I was almost done jusing just some standard tools. The only problem was that some of the files in that directory have quite long names and there was some ugly line wrapping. Of course, there is a default tool for that, too, cut. But cut works on bytes and knows nothing about multibyte characters or ANSI color control sequences, both of which can thoroughly confuse your layout if cut off improperly, for example it the sequence to reset the colors to default is just cut off. For that talk I found a line length for the sidebar where this didn't matter (36, in case you wonder), but such a line length does not exist in general. So I decided to write a more layout aware version of cut, in case I need it again.

For comparision:

cut color-cut
$ tree -C .git | cut -c 1-30
.git
├── branches
OMMIT_EDITMSG
├── config
├── description
├── HEAD
├── index
├── objects
│   ├── 2c
6
│   ├── 33
5f
│   ├── 7f
6
│   ├── 8f

bc 4 │   └── pack └── refs ├── heads aster └── tags elease-0.1

11 directories, 12 files $ tput sgr0 $

$ tree -C .git | color-cut 30
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── index
├── objects
│   ├── 2c
│   │   └── 92f6632693da1df414
│   ├── 33
│   │   └── 9b5f3603264ee532b6
│   ├── 7f
│   │   └── 80a63029a034dbe500
│   ├── 8f
│   │   └── 3761e066d5f3c0d3fa
│   ├── bc
│   │   └── 56c4d89448a963d0b6
│   └── pack
└── refs
    ├── heads
    │   └── master
    └── tags
        └── release-0.1

11 directories, 12 files
$

About

Cut potentially colored output of a program to a visible number of columns.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages