mirror of
https://github.com/tmux/tmux.git
synced 2025-03-30 03:18:51 +00:00
Break code to convert an argument as a percentage into a common function.
This commit is contained in:
parent
5aeab5ab40
commit
8828b958f0
50
arguments.c
50
arguments.c
@ -344,3 +344,53 @@ args_strtonum(struct args *args, u_char ch, long long minval, long long maxval,
|
|||||||
*cause = NULL;
|
*cause = NULL;
|
||||||
return (ll);
|
return (ll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Convert an argument to a number which may be a percentage. */
|
||||||
|
long long
|
||||||
|
args_percentage(struct args *args, u_char ch, long long minval,
|
||||||
|
long long maxval, long long curval, char **cause)
|
||||||
|
{
|
||||||
|
const char *errstr;
|
||||||
|
long long ll;
|
||||||
|
struct args_entry *entry;
|
||||||
|
struct args_value *value;
|
||||||
|
size_t valuelen;
|
||||||
|
char *copy;
|
||||||
|
|
||||||
|
if ((entry = args_find(args, ch)) == NULL) {
|
||||||
|
*cause = xstrdup("missing");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
value = TAILQ_LAST(&entry->values, args_values);
|
||||||
|
valuelen = strlen(value->value);
|
||||||
|
|
||||||
|
if (value->value[valuelen - 1] == '%') {
|
||||||
|
copy = xstrdup(value->value);
|
||||||
|
copy[valuelen - 1] = '\0';
|
||||||
|
|
||||||
|
ll = strtonum(copy, 0, 100, &errstr);
|
||||||
|
free(copy);
|
||||||
|
if (errstr != NULL) {
|
||||||
|
*cause = xstrdup(errstr);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
ll = (curval * ll) / 100;
|
||||||
|
if (ll < minval) {
|
||||||
|
*cause = xstrdup("too large");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (ll > maxval) {
|
||||||
|
*cause = xstrdup("too small");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ll = strtonum(value->value, minval, maxval, &errstr);
|
||||||
|
if (errstr != NULL) {
|
||||||
|
*cause = xstrdup(errstr);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*cause = NULL;
|
||||||
|
return (ll);
|
||||||
|
}
|
||||||
|
@ -51,7 +51,7 @@ const struct cmd_entry cmd_move_pane_entry = {
|
|||||||
.alias = "movep",
|
.alias = "movep",
|
||||||
|
|
||||||
.args = { "bdhvp:l:s:t:", 0, 0 },
|
.args = { "bdhvp:l:s:t:", 0, 0 },
|
||||||
.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
|
.usage = "[-bdhv] [-l size] " CMD_SRCDST_PANE_USAGE,
|
||||||
|
|
||||||
.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
|
.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
|
||||||
.target = { 't', CMD_FIND_PANE, 0 },
|
.target = { 't', CMD_FIND_PANE, 0 },
|
||||||
@ -69,9 +69,7 @@ cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
|
|||||||
struct winlink *src_wl, *dst_wl;
|
struct winlink *src_wl, *dst_wl;
|
||||||
struct window *src_w, *dst_w;
|
struct window *src_w, *dst_w;
|
||||||
struct window_pane *src_wp, *dst_wp;
|
struct window_pane *src_wp, *dst_wp;
|
||||||
char *cause, *copy;
|
char *cause = NULL;
|
||||||
const char *errstr, *p;
|
|
||||||
size_t plen;
|
|
||||||
int size, percentage, dst_idx, not_same_window;
|
int size, percentage, dst_idx, not_same_window;
|
||||||
int flags;
|
int flags;
|
||||||
enum layout_type type;
|
enum layout_type type;
|
||||||
@ -108,40 +106,27 @@ cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
|
|||||||
type = LAYOUT_LEFTRIGHT;
|
type = LAYOUT_LEFTRIGHT;
|
||||||
|
|
||||||
size = -1;
|
size = -1;
|
||||||
if ((p = args_get(args, 'l')) != NULL) {
|
if (args_has(args, 'l')) {
|
||||||
plen = strlen(p);
|
if (type == LAYOUT_TOPBOTTOM) {
|
||||||
if (p[plen - 1] == '%') {
|
size = args_percentage(args, 'l', 0, INT_MAX,
|
||||||
copy = xstrdup(p);
|
dst_wp->sy, &cause);
|
||||||
copy[plen - 1] = '\0';
|
} else {
|
||||||
percentage = strtonum(copy, 0, INT_MAX, &errstr);
|
size = args_percentage(args, 'l', 0, INT_MAX,
|
||||||
free(copy);
|
dst_wp->sx, &cause);
|
||||||
if (errstr != NULL) {
|
}
|
||||||
cmdq_error(item, "percentage %s", errstr);
|
} else if (args_has(args, 'p')) {
|
||||||
return (CMD_RETURN_ERROR);
|
percentage = args_strtonum(args, 'p', 0, 100, &cause);
|
||||||
}
|
if (cause == NULL) {
|
||||||
if (type == LAYOUT_TOPBOTTOM)
|
if (type == LAYOUT_TOPBOTTOM)
|
||||||
size = (dst_wp->sy * percentage) / 100;
|
size = (dst_wp->sy * percentage) / 100;
|
||||||
else
|
else
|
||||||
size = (dst_wp->sx * percentage) / 100;
|
size = (dst_wp->sx * percentage) / 100;
|
||||||
} else {
|
|
||||||
size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
|
|
||||||
if (cause != NULL) {
|
|
||||||
cmdq_error(item, "size %s", cause);
|
|
||||||
free(cause);
|
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (args_has(args, 'p')) {
|
}
|
||||||
percentage = args_strtonum(args, 'p', 0, 100, &cause);
|
if (cause != NULL) {
|
||||||
if (cause != NULL) {
|
cmdq_error(item, "size %s", cause);
|
||||||
cmdq_error(item, "percentage %s", cause);
|
free(cause);
|
||||||
free(cause);
|
return (CMD_RETURN_ERROR);
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
if (type == LAYOUT_TOPBOTTOM)
|
|
||||||
size = (dst_wp->sy * percentage) / 100;
|
|
||||||
else
|
|
||||||
size = (dst_wp->sx * percentage) / 100;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags = 0;
|
flags = 0;
|
||||||
|
@ -56,11 +56,10 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
|
|||||||
struct window *w = wl->window;
|
struct window *w = wl->window;
|
||||||
struct client *c = item->client;
|
struct client *c = item->client;
|
||||||
struct session *s = item->target.s;
|
struct session *s = item->target.s;
|
||||||
const char *errstr, *p;
|
const char *errstr;
|
||||||
char *cause, *copy;
|
char *cause;
|
||||||
u_int adjust;
|
u_int adjust;
|
||||||
int x, y, percentage;
|
int x, y;
|
||||||
size_t plen;
|
|
||||||
|
|
||||||
if (args_has(args, 'M')) {
|
if (args_has(args, 'M')) {
|
||||||
if (cmd_mouse_window(&shared->mouse, &s) == NULL)
|
if (cmd_mouse_window(&shared->mouse, &s) == NULL)
|
||||||
@ -93,58 +92,21 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((p = args_get(args, 'x')) != NULL) {
|
if (args_has(args, 'x')) {
|
||||||
plen = strlen(p);
|
x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
|
||||||
if (p[plen - 1] == '%') {
|
if (cause != NULL) {
|
||||||
copy = xstrdup(p);
|
cmdq_error(item, "width %s", cause);
|
||||||
copy[plen - 1] = '\0';
|
free(cause);
|
||||||
percentage = strtonum(copy, 0, INT_MAX, &errstr);
|
return (CMD_RETURN_ERROR);
|
||||||
free(copy);
|
|
||||||
if (errstr != NULL) {
|
|
||||||
cmdq_error(item, "width %s", errstr);
|
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
x = (w->sx * percentage) / 100;
|
|
||||||
if (x < PANE_MINIMUM)
|
|
||||||
x = PANE_MINIMUM;
|
|
||||||
if (x > INT_MAX)
|
|
||||||
x = INT_MAX;
|
|
||||||
} else {
|
|
||||||
x = args_strtonum(args, 'x', PANE_MINIMUM, INT_MAX,
|
|
||||||
&cause);
|
|
||||||
if (cause != NULL) {
|
|
||||||
cmdq_error(item, "width %s", cause);
|
|
||||||
free(cause);
|
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
|
layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
|
||||||
}
|
}
|
||||||
if ((p = args_get(args, 'y')) != NULL) {
|
if (args_has(args, 'y')) {
|
||||||
plen = strlen(p);
|
y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
|
||||||
if (p[plen - 1] == '%') {
|
if (cause != NULL) {
|
||||||
copy = xstrdup(p);
|
cmdq_error(item, "width %s", cause);
|
||||||
copy[plen - 1] = '\0';
|
free(cause);
|
||||||
percentage = strtonum(copy, 0, INT_MAX, &errstr);
|
return (CMD_RETURN_ERROR);
|
||||||
free(copy);
|
|
||||||
if (errstr != NULL) {
|
|
||||||
cmdq_error(item, "height %s", errstr);
|
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
y = (w->sy * percentage) / 100;
|
|
||||||
if (y < PANE_MINIMUM)
|
|
||||||
y = PANE_MINIMUM;
|
|
||||||
if (y > INT_MAX)
|
|
||||||
y = INT_MAX;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
y = args_strtonum(args, 'y', PANE_MINIMUM, INT_MAX,
|
|
||||||
&cause);
|
|
||||||
if (cause != NULL) {
|
|
||||||
cmdq_error(item, "height %s", cause);
|
|
||||||
free(cause);
|
|
||||||
return (CMD_RETURN_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
|
layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
|
||||||
}
|
}
|
||||||
|
2
tmux.h
2
tmux.h
@ -2060,6 +2060,8 @@ const char *args_first_value(struct args *, u_char, struct args_value **);
|
|||||||
const char *args_next_value(struct args_value **);
|
const char *args_next_value(struct args_value **);
|
||||||
long long args_strtonum(struct args *, u_char, long long, long long,
|
long long args_strtonum(struct args *, u_char, long long, long long,
|
||||||
char **);
|
char **);
|
||||||
|
long long args_percentage(struct args *, u_char, long long,
|
||||||
|
long long, long long, char **);
|
||||||
|
|
||||||
/* cmd-find.c */
|
/* cmd-find.c */
|
||||||
int cmd_find_target(struct cmd_find_state *, struct cmdq_item *,
|
int cmd_find_target(struct cmd_find_state *, struct cmdq_item *,
|
||||||
|
Loading…
Reference in New Issue
Block a user