diff -urN mysql-5.0.41-orig/client/client_priv.h mysql-5.0.41/client/client_priv.h --- mysql-5.0.41-orig/client/client_priv.h 2007-05-21 12:31:39.000000000 -0700 +++ mysql-5.0.41/client/client_priv.h 2007-05-21 12:50:01.000000000 -0700 @@ -51,5 +51,6 @@ OPT_TRIGGERS, OPT_IGNORE_TABLE,OPT_INSERT_IGNORE,OPT_SHOW_WARNINGS,OPT_DROP_DATABASE, OPT_TZ_UTC, OPT_AUTO_CLOSE, OPT_SSL_VERIFY_SERVER_CERT, + OPT_AUTO_VERTICAL_OUTPUT, OPT_DEBUG_INFO }; diff -urN mysql-5.0.41-orig/client/mysql.cc mysql-5.0.41/client/mysql.cc --- mysql-5.0.41-orig/client/mysql.cc 2007-05-21 12:31:39.000000000 -0700 +++ mysql-5.0.41/client/mysql.cc 2007-05-21 12:53:17.000000000 -0700 @@ -138,6 +138,7 @@ tty_password= 0, opt_nobeep=0, opt_reconnect=1, default_charset_used= 0, opt_secure_auth= 0, default_pager_set= 0, opt_sigint_ignore= 0, + auto_vertical_output= 0, show_warnings= 0; static volatile int executing_query= 0, interrupted_query= 0; static ulong opt_max_allowed_packet, opt_net_buffer_length; @@ -173,6 +174,7 @@ static uint prompt_counter; static char delimiter[16]= DEFAULT_DELIMITER; static uint delimiter_length= 1; +unsigned short terminal_width= 80; #ifdef HAVE_SMEM static char *shared_memory_base_name=0; @@ -224,6 +226,8 @@ static char *get_arg(char *line, my_bool get_next_arg); static void init_username(); static void add_int_to_prompt(int toadd); +static int get_result_width(MYSQL_RES *res); +static int get_field_disp_length(MYSQL_FIELD * field); /* A structure which contains information on the commands this program can understand. */ @@ -343,7 +347,9 @@ static void nice_time(double sec,char *buff,bool part_second); static sig_handler mysql_end(int sig); static sig_handler mysql_sigint(int sig); - +#if defined(HAVE_TERMIOS_H) +static sig_handler window_resize(int sig); +#endif int main(int argc,char *argv[]) { @@ -430,8 +436,8 @@ if (sql_connect(current_host,current_db,current_user,opt_password, opt_silent)) { - quick=1; // Avoid history - status.exit_status=1; + quick= 1; // Avoid history + status.exit_status= 1; mysql_end(-1); } if (!status.batch) @@ -443,6 +449,13 @@ signal(SIGINT, mysql_sigint); // Catch SIGINT to clean up signal(SIGQUIT, mysql_end); // Catch SIGQUIT to clean up +#if defined(HAVE_TERMIOS_H) + //Readline will call this if it installs a handler + signal(SIGWINCH, window_resize); + //call the SIGWINCH handler to get the default term width + window_resize(0); +#endif + put_info("Welcome to the MySQL monitor. Commands end with ; or \\g.", INFO_INFO); sprintf((char*) glob_buffer.ptr(), @@ -569,6 +582,16 @@ } +#if defined(HAVE_TERMIOS_H) +sig_handler window_resize(int sig) +{ + struct winsize window_size; + + if (ioctl(fileno(stdin), TIOCGWINSZ, &window_size) == 0) + terminal_width= window_size.ws_col; +} +#endif + static struct my_option my_long_options[] = { {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, @@ -586,6 +609,9 @@ {"no-auto-rehash", 'A', "No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, + {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT, + "Automatically switch to vertical output mode if the result is wider than the terminal width.", + (gptr*) &auto_vertical_output, (gptr*) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"batch", 'B', "Don't use history file. Disable interactive behavior. (Enables --silent)", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, {"character-sets-dir", OPT_CHARSETS_DIR, @@ -2125,7 +2151,7 @@ print_table_data_html(result); else if (opt_xml) print_table_data_xml(result); - else if (vertical) + else if (vertical || (auto_vertical_output && (terminal_width < get_result_width(result)))) print_table_data_vertically(result); else if (opt_silent && verbose <= 2 && !output_tables) print_tab_data(result); @@ -2454,6 +2480,33 @@ my_afree((gptr) num_flag); } +static int get_field_disp_length(MYSQL_FIELD *field) +{ + uint length= column_names ? field->name_length : 0; + + if (quick) + length= max(length,field->length); + else + length= max(length,field->max_length); + + if (length < 4 && !IS_NOT_NULL(field->flags)) + length= 4; // Room for "NULL" + + return length; +} + +static int get_result_width(MYSQL_RES *result) +{ + unsigned int len= 0; + MYSQL_FIELD *field; + + while ((field= mysql_fetch_field(result)) != NULL) + len+= get_field_disp_length(field) + 3; + + (void)mysql_field_seek(result, 0); + + return len + 1; +} static void tee_print_sized_data(const char *data, unsigned int data_length, unsigned int total_bytes_to_send, bool right_justified)