note 43303 added to ref.ncurses

From: Date: Thu, 17 Jun 2004 02:17:00 +0000
Subject: note 43303 added to ref.ncurses
Groups: php.notes 
Request: Send a blank email to php-notes+get-71182@lists.php.net to get a copy of this message
Here is a function that takes an associative array, presents a menu in a new window, allows the user to make a choice using up and down arrows and the enter key, and returns the value of the menu item. Limitations include: No way of scrolling a long list, either horiontally or vertically; No arguments for placement on screen, although this is easy to add; No multiple selection; Will produce all kinds of errors and warnings if the terminal is smaller than is necessary to create the window. I'm very new at using the ncurses library; Comments and improvements would be greatly appreciated! <?php /** * Create a simple selection menu * @param array Associative array; The value will be shown on the menu, while the key will be returned when the associated value is selected. * @return mixed */ function ncurses_menu_select( $menu ) { $keys = array_keys( $menu ); $values = array_values( $menu ); $height = $width = 0; $height = count( $menu ) + 2; foreach( $values as $value ) { $width = max( $width, strlen( $value ) + 2 ); } $menu_window = ncurses_newwin( $height, $width, 5, 5 ); ncurses_wborder( $menu_window, 0,0, 0,0, 0,0, 0,0 ); $current = 0; for( $a = 0; $a < count( $values ); $a++ ) { if ( $a == $current ) { ncurses_wattron( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $values[ $a ] ); ncurses_wattroff( $menu_window, NCURSES_A_REVERSE ); } else { ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $values[ $a ] ); } } ncurses_wrefresh( $menu_window ); while( ! in_array( $key = ncurses_getch( $menu_window ), array( 13, 10 ) ) ) { if ( $key == NCURSES_KEY_UP AND $current > 0 ) { $move = -1; } else if ( $key == NCURSES_KEY_DOWN and $current < count( $values ) - 1 ) { $move = 1; } else { $move = 0; } ncurses_mvwaddstr( $menu_window, 1 + $current, 1, $values[ $current ] ); $current += $move; ncurses_wattron( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, 1 + $current, 1, $values[ $current ] ); ncurses_wattroff( $menu_window, NCURSES_A_REVERSE ); ncurses_wrefresh( $menu_window ); } ncurses_delwin( $menu_window ); return $keys[ $current ]; } ?> Example Use: <?php // Initialie ncurses $ncurse = ncurses_init(); // A full screen window $fullscreen = ncurses_newwin ( 0, 0, 0, 0); // Add a pretty border ncurses_border(0,0, 0,0, 0,0, 0,0); // Draw everything so far ncurses_refresh(); // Set up menu array $menu_items = array( 'one' => 'Menu Item #1', 'two' => 'Menu Item #2', 'three' => 'Menu Item #3' ); // Display menu and return selection $selection = ncurses_menu_select( $menu_items ); // Print selection ncurses_mvaddstr( 1, 1, 'You selected ' . $menu_items[$selection] . ' with the value ' . $selection ); // Draw updates ncurses_refresh( $fullscreen ); // End ncurses_end(); ?> ---- Manual Page -- http://www.php.net/manual/en/ref.ncurses.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43303 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43303&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43303&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71182) next »