Re: [Powertop] [PATCH] tuninig: resize scaling support (v1)

Sergey Senozhatsky <sergey.senozhatsky at gmail.com> Thu, 23 Aug 2012 12:12:45 +0300
Newsgroups dev.linux.lists.powertop
Message-ID <[email protected]>
On (08/23/12 01:47), Sergey Senozhatsky wrote:
> tuninig: resize scaling support v1
> 
> Initial version of terminal window resize support for tuning tab.
> Supports both X and Y scalings:
> -- X scaling: truncation (*)
> * tunable result and description strings are now stored in dynamic
> array, limited in size to current window X (mod MAX_LEN).
> 
> -- Y scaling: paging and scrolling
> 
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>
> 
> ---
> 
>  src/tuning/tuning.cpp | 87 +++++++++++++++++++++++++++++++++++----------------
>  1 file changed, 60 insertions(+), 27 deletions(-)
> 
> diff --git a/src/tuning/tuning.cpp b/src/tuning/tuning.cpp
> index 4893597..541e9e0 100644
> --- a/src/tuning/tuning.cpp
> +++ b/src/tuning/tuning.cpp
> @@ -28,7 +28,7 @@
>  #include <stdio.h>
>  #include <string.h>
>  #include <ncurses.h>
> -
> +#include <math.h>
>  
>  #include "tuning.h"
>  #include "tuningsysfs.h"
> @@ -79,48 +79,81 @@ void initialize_tuning(void)
>  	create_tab("Tunables", _("Tunables"), w, _(" <ESC> Exit | <Enter> Toggle tunable | <r> Window refresh"));
>  
>  	init_tuning();
> -
>  	w->cursor_max = all_tunables.size() - 1;
>  }
>  
> +#define XINDENT	3
> +#define YINDENT	4
> +#define MAX_LEN	4096
>  
> +static void redraw_window(WINDOW *win, int begin, int end, int cursor_pos)
> +{
> +	int x, y, i;
> +	getmaxyx(stdscr, y, x);
> +	
> +	wclrtoeol(win);
> +	wmove(win, 1, 0);
> +	x %= MAX_LEN;
> +
> +	for (y = 0, i = begin; i < end; i++, y++) {
> +		char truncate = 0;
> +		size_t sz = 0;
> +		char line[x + 1];
> +		if ((int)(y) != cursor_pos) {
> +			wattrset(win, A_NORMAL);
> +			sz = snprintf(line, x, "   ");
> +		} else {
> +			wattrset(win, A_REVERSE);
> +			sz = snprintf(line, x, ">> ");
> +		}
> +		sz += snprintf(line + sz, x, "%s", all_tunables[i]->result_string());


Hm, I guess the correct way is

	_(all_tunables[i]->result_string())

> +		while (sz < 12)
> +			sz += snprintf(line + sz, x, " ");
> +		sz += snprintf(line + sz, x, "%s", all_tunables[i]->description());

the same

	_(all_tunables[i]->description())

> +		if ((int)sz > x - XINDENT)
> +			truncate = 1;
> +		while ((int)sz < x - XINDENT)
> +			sz += snprintf(line + sz, x, " ");
> +		if (truncate)
> +			sz += snprintf(line + x - XINDENT - 1, x, "~\n");
> +		else
> +			sz += snprintf(line + sz, x, "\n");
> +
> +		waddnstr(win, line, sz);
> +	}
> +}
>  
>  static void __tuning_update_display(int cursor_pos)
>  {
> +	static int last_frame_nr = 0;
> +	int x, y, begin, end, frame_nr;
>  	WINDOW *win;
> -	unsigned int i;
> -
> +        
>  	win = get_ncurses_win("Tunables");
> -
>  	if (!win)
>  		return;
> +	getmaxyx(stdscr, y, x);
> +
> +	y -= YINDENT;
> +	frame_nr = ceil(cursor_pos / y);
> +	end = (frame_nr + 1) * y;
> +	if (end > (int)all_tunables.size())
> +		end = all_tunables.size();
> +
> +	begin = frame_nr * y;
> +	/* actual cursor position is frame-dependent */
> +	cursor_pos -= frame_nr * y;
> +	if (frame_nr != last_frame_nr)
> +		should_clear = TRUE;
> +	last_frame_nr = frame_nr;
>  
>  	if (should_clear) {
> -		should_clear = false;
> +		should_clear = FALSE;
>  		wclear(win);
>  	}
>  
> -	wmove(win, 2,0);
> -
> -	for (i = 0; i < all_tunables.size(); i++) {
> -		char res[128];
> -		char desc[4096];
> -		strcpy(res, all_tunables[i]->result_string());
> -		strcpy(desc, all_tunables[i]->description());
> -		while (strlen(res) < 12)
> -			strcat(res, " ");
> -
> -		while (strlen(desc) < 103)
> -			strcat(desc, " ");
> -		if ((int)i != cursor_pos) {
> -			wattrset(win, A_NORMAL);
> -			wprintw(win, "   ");
> -		} else {
> -			wattrset(win, A_REVERSE);
> -			wprintw(win, ">> ");
> -		}
> -		wprintw(win, "%s  %s\n", _(res), _(desc));
> -	}
> +	redraw_window(win, begin, end, cursor_pos);
> +	wnoutrefresh(win);
>  }
>  
>  void tuning_update_display(void)
>