Fall back to getline if ncurses cannot be initialized (eg. TERM not set).
This commit is contained in:
parent
06ebe954f1
commit
e4837a284a
@ -104,6 +104,7 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
|
||||
#if MPW_COLOR
|
||||
// Initialize a curses screen.
|
||||
SCREEN *screen = newterm( NULL, stderr, stdin );
|
||||
if (screen) {
|
||||
start_color();
|
||||
init_pair( 1, COLOR_WHITE, COLOR_BLUE );
|
||||
init_pair( 2, COLOR_BLACK, COLOR_WHITE );
|
||||
@ -135,7 +136,8 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
|
||||
noecho();
|
||||
result = mvgetnstr( rows / 2 + 1, (cols - 1) / 2, str, MPW_MAX_INPUT );
|
||||
echo();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
mvprintw( rows / 2 + 1, (cols - (MPW_MAX_INPUT + 2)) / 2, "%*s", MPW_MAX_INPUT + 2, "" );
|
||||
refresh();
|
||||
|
||||
@ -147,21 +149,22 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
|
||||
delscreen( screen );
|
||||
|
||||
return result == ERR? NULL: mpw_strndup( str, MPW_MAX_INPUT );
|
||||
#else
|
||||
}
|
||||
#endif
|
||||
|
||||
// Get password from terminal.
|
||||
fprintf( stderr, "%s ", prompt );
|
||||
|
||||
size_t bufSize = 0;
|
||||
ssize_t lineSize = getline( &answer, &bufSize, stdin );
|
||||
ssize_t lineSize = getline( (char **)&answer, &bufSize, stdin );
|
||||
if (lineSize <= 1) {
|
||||
mpw_free_string( &answer );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Remove trailing newline.
|
||||
answer[lineSize - 1] = '\0';
|
||||
mpw_replace_string( answer, mpw_strndup( answer, (size_t)lineSize - 1 ) );
|
||||
return answer;
|
||||
#endif
|
||||
}
|
||||
|
||||
const char *mpw_getline(const char *prompt) {
|
||||
|
Loading…
Reference in New Issue
Block a user