File: console.cc

package info (click to toggle)
snapper 0.10.6-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,072 kB
  • sloc: cpp: 24,846; ansic: 1,466; sh: 1,410; makefile: 514; python: 127; ruby: 90
file content (64 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*---------------------------------------------------------------------------*\
                          ____  _ _ __ _ __  ___ _ _
                         |_ / || | '_ \ '_ \/ -_) '_|
                         /__|\_, | .__/ .__/\___|_|
                             |__/|_|  |_|
\*---------------------------------------------------------------------------*/

/** \file console.cc
 * Miscellaneous console utilities.
 */


#include <stdlib.h>
#include <unistd.h>
#include <term.h>


namespace snapper
{


    unsigned
    get_screen_width_pure()
    {
	if (!isatty(STDOUT_FILENO))
	    return -1; // no clipping

	int width = 0;

	const char* cols_env = getenv("COLUMNS");
	if (cols_env)
	{
	    width = atoi(cols_env);
	}
	else
	{
	    // use terminfo from ncurses
	    setupterm(NULL, STDOUT_FILENO, NULL);
	    width = tigetnum("cols");

	    /*
	    // use readline
	    rl_initialize();
	    rl_get_screen_size(NULL, &width);
	    */
	}

	// safe default
	if (width <= 0)
	    width = 80;

	return width;
    }


    unsigned
    get_screen_width()
    {
	static unsigned width = get_screen_width_pure();

	return width;
    }

}