1

I'm building a CLI with Node.js and want to add less/more like pagination.

screenshot

I can just stdout the output and pipe it to less like this

enter image description here

but I'm not able to add the text highlight just like git log in the first picture.

I have searched about it but couldn't find any relevant info. Would really appreciate any help or references

Anurag Bhagsain
  • 363
  • 3
  • 10
  • Just use ` | less -R` – tink Nov 25 '22 at 21:18
  • @tink Yes I can pipe the output to less but I want some part of the output to be highlighted just like git log (also mentioned in the question) – Anurag Bhagsain Nov 25 '22 at 21:20
  • Have you actually **tried** it with the `-R`? `less` doesn't strip control characters with `-R` and things retain colour. – tink Nov 25 '22 at 21:22
  • Yes, I have tried it with `-R` flag. Could it be because I'm using a third-party package to highlight the output? It's a CLI built with Node.js and I'm using `chalk` to add color to the console. https://github.com/abhagsain/ai-cli – Anurag Bhagsain Nov 25 '22 at 21:24
  • Excellent question ... maybe that framework skips control characters when it writes to a pipe. If you e.g. try colourised `ls` output into `less -R` - does that work for you? Because on my Ubuntu install it sure does. – tink Nov 25 '22 at 21:28
  • No, `ls | less -R` not working highlighting text for me either I'm on Mac btw using the built-in terminal and also VSCode's integrated terminal – Anurag Bhagsain Nov 25 '22 at 21:32
  • That's weird ... when I try on the mac (Monterey, 12.6.1) I keep coloured output in `less` in both `terminal.app` and `iTerm2` (the letter being my preference). Does `ls` give colour for you normally? If not, try `ls -l --color=always| less -R` ... – tink Nov 25 '22 at 21:37
  • Yes, it shows color normally. I'm getting this error `ls: illegal option -- =` also I forgot to mention that I'm using `ohmyzsh` with power10k theme. I'll go through github's CLI source code to understand how they are doing it with `gh search prs query` https://github.com/cli/cli I don't know golang but let's see. Thank you so much for your time. – Anurag Bhagsain Nov 25 '22 at 21:53

0 Answers0