libzypp 17.37.10

console.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9
13#include <unistd.h>
14#include <cstdlib>
15
16#include <fstream>
17#include <iostream>
18#include <readline/readline.h>
19#include <readline/history.h>
20
21 namespace ztui {
22
23// ----------------------------------------------------------------------------
24
25// Read a string. "\004" (^D) on EOF.
26std::string readline_getline()
27{
28 std::string ret;
29
30 //::rl_catch_signals = 0;
31 /* Get a line from the user. */
32 if ( char * line_read = ::readline( "zypper> " ) )
33 {
34 ret = line_read;
35 /* If the line has any text in it, save it on the history. */
36 if ( *line_read )
37 ::add_history( line_read );
38 ::free( line_read );
39 }
40 else
41 ret = "\004";
42
43 return ret;
44}
45
46// ----------------------------------------------------------------------------
47
48 unsigned get_screen_width()
49{
50 if ( !::isatty(STDOUT_FILENO) )
51 return -1; // no clipping
52
53 int width = 80;
54
55 const char *cols_env = getenv("COLUMNS");
56 if ( cols_env )
57 width = ::atoi( cols_env );
58 else
59 {
60 ::rl_initialize();
61 //::rl_reset_screen_size();
62 ::rl_get_screen_size( NULL, &width );
63 }
64
65 // safe default
66 if ( !width )
67 width = 80;
68
69 return width;
70}
71
72// ----------------------------------------------------------------------------
73
74 void clear_keyboard_buffer()
75{
76 // note: this will not clear characters typed after the last \n
77 std::ifstream stm( "/dev/tty" );
78 char s[8];
79 while (stm.good() && stm.readsome(s, 8));
80}
81
82}
ztui
Definition application.cc:13
ztui::readline_getline
std::string readline_getline()
Use readline to get line of input.
Definition console.cc:26
ztui::get_screen_width
unsigned get_screen_width()
Reads COLUMNS environment variable or gets the screen width from readline, in that order.
Definition console.cc:48
ztui::clear_keyboard_buffer
void clear_keyboard_buffer()
Clear the keyboard buffer.
Definition console.cc:74