Merge branches zj/decimal-width, zj/term-columns and jc/diff-stat-scaler
This commit is contained in:
commit
db65f0fc3b
1
cache.h
1
cache.h
@ -1176,6 +1176,7 @@ extern void setup_pager(void);
|
||||
extern const char *pager_program;
|
||||
extern int pager_in_use(void);
|
||||
extern int pager_use_color;
|
||||
extern int term_columns(void);
|
||||
extern int decimal_width(int);
|
||||
|
||||
extern const char *editor_program;
|
||||
|
27
diff.c
27
diff.c
@ -1276,13 +1276,15 @@ const char mime_boundary_leader[] = "------------";
|
||||
|
||||
static int scale_linear(int it, int width, int max_change)
|
||||
{
|
||||
if (!it)
|
||||
return 0;
|
||||
/*
|
||||
* make sure that at least one '-' is printed if there were deletions,
|
||||
* and likewise for '+'.
|
||||
* make sure that at least one '-' or '+' is printed if
|
||||
* there is any change to this path. The easiest way is to
|
||||
* scale linearly as if the alloted width is one column shorter
|
||||
* than it is, and then add 1 to the result.
|
||||
*/
|
||||
if (max_change < 2)
|
||||
return it;
|
||||
return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
|
||||
return 1 + (it * (width - 1) / max_change);
|
||||
}
|
||||
|
||||
static void show_name(FILE *file,
|
||||
@ -1449,8 +1451,19 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
|
||||
dels += del;
|
||||
|
||||
if (width <= max_change) {
|
||||
add = scale_linear(add, width, max_change);
|
||||
del = scale_linear(del, width, max_change);
|
||||
int total = add + del;
|
||||
|
||||
total = scale_linear(add + del, width, max_change);
|
||||
if (total < 2 && add && del)
|
||||
/* width >= 2 due to the sanity check */
|
||||
total = 2;
|
||||
if (add < del) {
|
||||
add = scale_linear(add, width, max_change);
|
||||
del = total - add;
|
||||
} else {
|
||||
del = scale_linear(del, width, max_change);
|
||||
add = total - del;
|
||||
}
|
||||
}
|
||||
fprintf(options->file, "%s", line_prefix);
|
||||
show_name(options->file, prefix, name, len);
|
||||
|
22
help.c
22
help.c
@ -5,28 +5,6 @@
|
||||
#include "help.h"
|
||||
#include "common-cmds.h"
|
||||
|
||||
/* most GUI terminals set COLUMNS (although some don't export it) */
|
||||
static int term_columns(void)
|
||||
{
|
||||
char *col_string = getenv("COLUMNS");
|
||||
int n_cols;
|
||||
|
||||
if (col_string && (n_cols = atoi(col_string)) > 0)
|
||||
return n_cols;
|
||||
|
||||
#ifdef TIOCGWINSZ
|
||||
{
|
||||
struct winsize ws;
|
||||
if (!ioctl(1, TIOCGWINSZ, &ws)) {
|
||||
if (ws.ws_col)
|
||||
return ws.ws_col;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return 80;
|
||||
}
|
||||
|
||||
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
|
||||
{
|
||||
struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
|
||||
|
37
pager.c
37
pager.c
@ -76,6 +76,12 @@ void setup_pager(void)
|
||||
if (!pager)
|
||||
return;
|
||||
|
||||
/*
|
||||
* force computing the width of the terminal before we redirect
|
||||
* the standard output to the pager.
|
||||
*/
|
||||
(void) term_columns();
|
||||
|
||||
setenv("GIT_PAGER_IN_USE", "true", 1);
|
||||
|
||||
/* spawn the pager */
|
||||
@ -111,6 +117,37 @@ int pager_in_use(void)
|
||||
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return cached value (if set) or $COLUMNS environment variable (if
|
||||
* set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
|
||||
* and default to 80 if all else fails.
|
||||
*/
|
||||
int term_columns(void)
|
||||
{
|
||||
static int term_columns_at_startup;
|
||||
|
||||
char *col_string;
|
||||
int n_cols;
|
||||
|
||||
if (term_columns_at_startup)
|
||||
return term_columns_at_startup;
|
||||
|
||||
term_columns_at_startup = 80;
|
||||
|
||||
col_string = getenv("COLUMNS");
|
||||
if (col_string && (n_cols = atoi(col_string)) > 0)
|
||||
term_columns_at_startup = n_cols;
|
||||
#ifdef TIOCGWINSZ
|
||||
else {
|
||||
struct winsize ws;
|
||||
if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
|
||||
term_columns_at_startup = ws.ws_col;
|
||||
}
|
||||
#endif
|
||||
|
||||
return term_columns_at_startup;
|
||||
}
|
||||
|
||||
/*
|
||||
* How many columns do we need to show this number in decimal?
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user