I would like to display as much from a file as possible which still fits in the terminal window, i.e. like a head
with a dynamic number of lines.
If there are no lines which wrap to multiple lines and the prompt takes a single line then I can achieve that with head -n $(($(tput lines)-1))
.
Is there a solution that doesn't require the assumptions above?
Edit: The solution should be non-interactive, so e.g. less
doesn't work as far as I know.
Edit2: The solution should preferably also handle properly non-printing characters like ANSI escape sequences for text coloring.
fold
andhead
works nicely expect when prompt is using more than one line. Is there a way to determine that from e.g.$PS1
? Thepr
solution didn't work properly with lines which wrap to multiple lines. – Samuli Asmala Jul 08 '19 at 09:38PS1
the way the shell expands it completely. If in bash, you could do something likefoo=${PS1@P}; echo ${#foo}
to get the length of the prompt, and then compare it with the width to see if it goes to multiple lines. (or maybe count lines in the expanded prompt) – muru Jul 08 '19 at 09:52\[\033[0;32m\]✔\[\033[0;0m\] \[\033[0;33m\]\w\[\033[0;0m\] [\[\033[0;35m\]${GIT_BRANCH}\[\033[0;0m\] ↑·2\[\033[0;0m\]|\[\033[0;34m\]✚ 1\[\033[0;0m\]\[\033[1;34m\]⚑ 2\[\033[0;0m\]\[\033[0;0m\]] \n\[\033[0;37m\]$(date +%H:%M)\[\033[0;0m\] $
– Samuli Asmala Jul 08 '19 at 13:52fold
solution doesn't work if there are non-printing characters like ANSI escape sequences for text coloring. There is a related question about Wrap text accounting for non-printing characters which is still unanswered. – Samuli Asmala Jul 08 '19 at 13:57