Fall back to getline if ncurses cannot be initialized (eg. TERM not set).
This commit is contained in:
parent
06ebe954f1
commit
e4837a284a
@ -104,64 +104,67 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
|
|||||||
#if MPW_COLOR
|
#if MPW_COLOR
|
||||||
// Initialize a curses screen.
|
// Initialize a curses screen.
|
||||||
SCREEN *screen = newterm( NULL, stderr, stdin );
|
SCREEN *screen = newterm( NULL, stderr, stdin );
|
||||||
start_color();
|
if (screen) {
|
||||||
init_pair( 1, COLOR_WHITE, COLOR_BLUE );
|
start_color();
|
||||||
init_pair( 2, COLOR_BLACK, COLOR_WHITE );
|
init_pair( 1, COLOR_WHITE, COLOR_BLUE );
|
||||||
int rows, cols;
|
init_pair( 2, COLOR_BLACK, COLOR_WHITE );
|
||||||
getmaxyx( stdscr, rows, cols );
|
int rows, cols;
|
||||||
|
getmaxyx( stdscr, rows, cols );
|
||||||
|
|
||||||
// Display a dialog box.
|
// Display a dialog box.
|
||||||
int width = max( prompt? (int)strlen( prompt ): 0, MPW_MAX_INPUT ) + 6;
|
int width = max( prompt? (int)strlen( prompt ): 0, MPW_MAX_INPUT ) + 6;
|
||||||
char *version = "mpw v" stringify_def( MP_VERSION );
|
char *version = "mpw v" stringify_def( MP_VERSION );
|
||||||
mvprintw( rows - 1, (cols - (int)strlen( version )) / 2, "%s", version );
|
mvprintw( rows - 1, (cols - (int)strlen( version )) / 2, "%s", version );
|
||||||
attron( A_BOLD );
|
attron( A_BOLD );
|
||||||
color_set( 2, NULL );
|
color_set( 2, NULL );
|
||||||
mvprintw( rows / 2 - 1, (cols - width) / 2, "%s%*s%s", "*", width - 2, "", "*" );
|
mvprintw( rows / 2 - 1, (cols - width) / 2, "%s%*s%s", "*", width - 2, "", "*" );
|
||||||
mvprintw( rows / 2 - 1, (cols - (int)strlen( prompt )) / 2, "%s", prompt );
|
mvprintw( rows / 2 - 1, (cols - (int)strlen( prompt )) / 2, "%s", prompt );
|
||||||
color_set( 1, NULL );
|
color_set( 1, NULL );
|
||||||
mvprintw( rows / 2 + 0, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
mvprintw( rows / 2 + 0, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
||||||
mvprintw( rows / 2 + 1, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
mvprintw( rows / 2 + 1, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
||||||
mvprintw( rows / 2 + 2, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
mvprintw( rows / 2 + 2, (cols - width) / 2, "%s%*s%s", "|", width - 2, "", "|" );
|
||||||
|
|
||||||
// Read response.
|
// Read response.
|
||||||
color_set( 2, NULL );
|
color_set( 2, NULL );
|
||||||
attron( A_STANDOUT );
|
attron( A_STANDOUT );
|
||||||
int result = ERR;
|
int result = ERR;
|
||||||
char str[MPW_MAX_INPUT + 1];
|
char str[MPW_MAX_INPUT + 1];
|
||||||
if (silent) {
|
if (silent) {
|
||||||
mvprintw( rows / 2 + 1, (cols - 5) / 2, "[ * ]" );
|
mvprintw( rows / 2 + 1, (cols - 5) / 2, "[ * ]" );
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
noecho();
|
noecho();
|
||||||
result = mvgetnstr( rows / 2 + 1, (cols - 1) / 2, str, MPW_MAX_INPUT );
|
result = mvgetnstr( rows / 2 + 1, (cols - 1) / 2, str, MPW_MAX_INPUT );
|
||||||
echo();
|
echo();
|
||||||
} else {
|
}
|
||||||
mvprintw( rows / 2 + 1, (cols - (MPW_MAX_INPUT + 2)) / 2, "%*s", MPW_MAX_INPUT + 2, "" );
|
else {
|
||||||
refresh();
|
mvprintw( rows / 2 + 1, (cols - (MPW_MAX_INPUT + 2)) / 2, "%*s", MPW_MAX_INPUT + 2, "" );
|
||||||
|
refresh();
|
||||||
|
|
||||||
echo();
|
echo();
|
||||||
result = mvgetnstr( rows / 2 + 1, (cols - MPW_MAX_INPUT) / 2, str, MPW_MAX_INPUT );
|
result = mvgetnstr( rows / 2 + 1, (cols - MPW_MAX_INPUT) / 2, str, MPW_MAX_INPUT );
|
||||||
|
}
|
||||||
|
attrset( 0 );
|
||||||
|
endwin();
|
||||||
|
delscreen( screen );
|
||||||
|
|
||||||
|
return result == ERR? NULL: mpw_strndup( str, MPW_MAX_INPUT );
|
||||||
}
|
}
|
||||||
attrset( 0 );
|
#endif
|
||||||
endwin();
|
|
||||||
delscreen( screen );
|
|
||||||
|
|
||||||
return result == ERR? NULL: mpw_strndup( str, MPW_MAX_INPUT );
|
|
||||||
#else
|
|
||||||
// Get password from terminal.
|
// Get password from terminal.
|
||||||
fprintf( stderr, "%s ", prompt );
|
fprintf( stderr, "%s ", prompt );
|
||||||
|
|
||||||
size_t bufSize = 0;
|
size_t bufSize = 0;
|
||||||
ssize_t lineSize = getline( &answer, &bufSize, stdin );
|
ssize_t lineSize = getline( (char **)&answer, &bufSize, stdin );
|
||||||
if (lineSize <= 1) {
|
if (lineSize <= 1) {
|
||||||
mpw_free_string( &answer );
|
mpw_free_string( &answer );
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove trailing newline.
|
// Remove trailing newline.
|
||||||
answer[lineSize - 1] = '\0';
|
mpw_replace_string( answer, mpw_strndup( answer, (size_t)lineSize - 1 ) );
|
||||||
return answer;
|
return answer;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *mpw_getline(const char *prompt) {
|
const char *mpw_getline(const char *prompt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user