An implementation of a scrolling selection box:
<?php
function ncurses_menu_select( $options, $values, $max_height = 7, $max_width = 20, $y = 2, $x = 2 ) {
$height = $max_height - 2;
$width = $max_width - 2;
$num_options = count( $options );
foreach( $options as $key => $value ) {
$options[ $key ] = substr( $value, 0, $width );
}
$menu_window = ncurses_newwin( $max_height, $max_width, $y, $x );
ncurses_wborder( $menu_window, 0, 0, 0, 0, 0, 0, 0, 0 );
$current = 0; $position = 1; $topitem = 0; for ( $a = 0; $a < min( $height, $num_options ); $a++ ) {
if ( $a == $current ) {
ncurses_wattron( $menu_window, NCURSES_A_REVERSE );
ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $options[ $a ] );
ncurses_wattroff( $menu_window, NCURSES_A_REVERSE );
} else {
ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $options[ $a ] );
}
}
ncurses_mvwaddstr( $menu_window, 1, 0, '*' );
ncurses_wrefresh( $menu_window );
while( ! in_array( $key = ncurses_getch( $menu_window ), array( 13, 10 ) ) ) {
if ( $key == NCURSES_KEY_UP && $current > 0 ) {
$move = -1;
} else if ( $key == NCURSES_KEY_DOWN && $current < $num_options - 1 ) {
$move = 1;
} else {
continue;
}
$current += $move;
$position += $move;
if ( $position < 1 || $position > $height ) {
if ( $position < 1 ) {
$position = 1;
} else {
$position = $height;
}
$topitem += $move;
for ( $a = 1; $a <= $height; $a++ ) {
ncurses_mvwaddstr( $menu_window, $a, 1, str_repeat( ' ', $width ) );
if ( $a == $position ) {
ncurses_wattron( $menu_window, NCURSES_A_REVERSE );
ncurses_mvwaddstr( $menu_window, $a, 1, $options[ $topitem + $a - 1 ] );
ncurses_wattroff( $menu_window, NCURSES_A_REVERSE );
} else {
ncurses_mvwaddstr( $menu_window, $a, 1, $options[ $topitem + $a - 1 ] );
}
}
} else { ncurses_wattron( $menu_window, NCURSES_A_REVERSE );
ncurses_mvwaddstr( $menu_window, $position, 1, $options[ $current ] );
ncurses_wattroff( $menu_window, NCURSES_A_REVERSE );
ncurses_mvwaddstr( $menu_window, $position - $move, 1, $options[ $current - $move ] );
}
ncurses_wborder( $menu_window, 0, 0, 0, 0, 0, 0, 0, 0 );
$dot_position = round ( ( $current / $num_options ) * ( $height - 1 ) );
ncurses_mvwaddstr( $menu_window, 1 + $dot_position, 0, '*' );
ncurses_wrefresh( $menu_window );
}
return $values[ $current ];
}
?>