17

As answered in Highlight the current date in cal

the current date in output form cal is automatically highlighted (reverse colors) if the output goes to terminal.

That's what I had always been getting.

However, with my current Debian GNU/Linux, it is not the case any more, and I'm wondering what the fix is.

enter image description here

$ echo $TERM
xterm

$ lsb_release -a 
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bullseye/sid
Release:        testing
Codename:       bullseye
xpt
  • 1,182
  • 2
  • 13
  • 32
  • 4
    The answer that you appear to be quoting was based on OpenSUSE, and the comment below it notes that there are several different implementations of `cal`. In particular, Debian based systems include `ncal` which may be what you have - in which case `ncal -b` or `ncal -C` should give you traditional `cal` layout plus highlighting. – steeldriver Aug 11 '21 at 22:00
  • 2
    It's one of several [known bugs](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=980489). On Debian cal==ncal – Thomas Dickey Aug 11 '21 at 22:19
  • 1
    @ThomasDickey in this case though, [it's as per design](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=904839), not a bug. – Stéphane Chazelas Aug 12 '21 at 06:35
  • 1
    not really: the manpage isn't "updated" to match the regression. – Thomas Dickey Aug 12 '21 at 07:42
  • @ThomasDickey, while I agree the manual could be improved, there's nothing in there that says that the current day is highlighted in the output of `cal`. The synopsis for `cal` clearly shows that `-h` is not one of the valid options. – Stéphane Chazelas Aug 12 '21 at 08:58

2 Answers2

15

I believe the correct "Answer" to this question is documented here on GitHub

To quote add

alias cal="if [ -t 1 ] ; then ncal -b ; else /usr/bin/cal ; fi"

into your shell rc file.

This is an extremely irritating change. Changing the behavior of a frequently used cli command for at least 17 years to make it "correct" is kind of insane. Now I understand why so many people hate Windows so much but are still reluctant to switch to Linux. I'm pretty sure almost all package maintainer who use cal (actually I think majority of them uses date anyway) are trained to use cal -h to turn off the highlight. Now the change even breaks compatibility with cal -h.

The change is documented here

A simpler hack to solve the "no highlight" is to alias cal to ncal -b, but it is not 100% correct with the package ncal maintainer's expectation.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Shi B.
  • 251
  • 2
  • 3
  • Note that although it is a Debian problem and I've accepted and upvoted this Debian-specific solution, but I was seeking for a more universal solution so I'd prefer the answer from https://unix.stackexchange.com/a/185230/374303 myself instead. – xpt Sep 12 '21 at 15:45
  • @xpt I tried the solution proposed, but if the day of month is 1 or 2 there will be multiple 1x and 2x days get highlighted. Not sure if it is just me. The grep color is indeed quite good effect though. – Shi B. Sep 13 '21 at 16:58
  • 1
    ~~which command did you try? Make sure it is `"\b$(date +%e | sed "s/ //g")"`, i.e., with the `sed` part~~. Oh, I got you, there is an obvious flaw in that answer (I fixed it on my end without thinking much to it). Check my comment I just put there. – xpt Sep 13 '21 at 20:19
  • Thanks @xpt, your suggestion in the other thread worked really well. – Shi B. Sep 29 '21 at 14:21
  • 2
    what does `[ -t 1 ]` is doing there? – AmadoC Aug 24 '22 at 05:50
  • 2
    @AmadoC it is unix voodoo magic. -t FD test if file descriptor (FD) is a file descriptor that is associated with a terminal. So basically it is checking if the shell is running in interactive mode or scripting mode (though not technically perfect in this explanation). In this case, it is testing stdout (fd 1). Feel free to replace it with stderr (2) or stdin (0). – Shi B. Oct 20 '22 at 14:17
4

cal -3 doesn't work after alias, this accepts options:

if [ -t 1 ] ; then alias cal="ncal -b" ; else alias cal="/usr/bin/cal" ; fi
marcohenry
  • 56
  • 1