Truncate terminal output

Hello

Using BASH under Ubuntu 10.4 lts + XMonad

My script is almost perfect but i'm stuck at the last hurdle:
I need a sort of modified 'echo' which truncates its output to the width of the terminal

I can find the terminal's width with 'stty size' but i dont know how to then convert input lines to that printing length:

  • The lines may contain multiple 'randomly' placed tab characters
  • The lines may contain special colour formatting escape sequences which should not effect the 'length' of the line

Context:
This is a 'find in files' utility which interactively finds using 'find' and 'grep' regex patterns in the directory subtree, opens the file in my favourite editor at the line wit the match, and interactively modifies the search pattern and re-performs the search. Complete with pretty coloured output. Here i'm trying to prevent long lines that get found from spilling onto new lines in the terminal and disrupting the formatted output :slight_smile:

More context:
The actual formatted output is:

<tab><col>[0-9]*</col><tab><col>[0-9]*</col><tab><line />

where <col> are the invisible coloured terminal formatting bits and <line /> is the only part which is allowed to be truncated (we assume the terminal is never thin enough to not print the 2 numeric fields)
None, one, or both numeric fields may exceed tab width under normal operation normal

Maybe you can check the available options of your favourit editor and try to play with it
(wrapping, autoindent, ... ) ?

also if the fold command can help

Have you tried using man fmt (linux)? You would have to use it like:

echo very long line | fmt -w$width

Or as:

(
    lots-of-commands
    that-generate-longlines
    and-it-continues
) | fmt -w$width