> From:Ludovic Courtès <l...@gnu.org>
> How about having a per-REPL setting that’s automatically set to the
> terminal’s width by default?
> 
> What’s the exact call to get the width?

The lightweight way is to just check for the COLUMNS environment variable.
Most of the xterm-like terminals set COLUMNS when a program is executed.
That will cover most situations when running in X.

Beyond that, you'd need to look at the TERM variable and guess columns
from that.  For this you'd usually query the terminfo database, which means
that you probably end up with curses as a dependency.  It usually already
is, since readline depends on it.

Below please find a program that uses curses to find the terminal's
width.  It needs to be linked -lcurses.

Alternately you could execute "tput cols".

Thanks,

-Mike

#include <stdio.h>
#include <curses.h>
#include <term.h>
#define STDOUT_FILENO 1
#define DEFAULT_COLUMNS (80)
int main (int argc, char ** argv)
{
  int ret, err, cols;
  setupterm ((char *) 0, STDOUT_FILENO, &err);
  if (ret == OK || err == 1)
    {
      cols = tigetnum ("cols");
      if (cols < 0)
        cols = DEFAULT_COLUMNS;
    }
  else
    cols = DEFAULT_COLUMNS;
  printf("columns %d\n", cols);
  return 0;
}


Reply via email to