mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:26:05 +00:00 
			
		
		
		
	Add a way to append or prepend to a format if the length has been limited.
This commit is contained in:
		
							
								
								
									
										48
									
								
								format.c
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								format.c
									
									
									
									
									
								
							@@ -755,6 +755,16 @@ format_merge(struct format_tree *ft, struct format_tree *from)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Add item bits to tree. */
 | 
			
		||||
static void
 | 
			
		||||
format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
 | 
			
		||||
{
 | 
			
		||||
	if (item->cmd != NULL)
 | 
			
		||||
		format_add(ft, "command", "%s", item->cmd->entry->name);
 | 
			
		||||
	if (item->shared != NULL && item->shared->formats != NULL)
 | 
			
		||||
		format_merge(ft, item->shared->formats);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Create a new tree. */
 | 
			
		||||
struct format_tree *
 | 
			
		||||
format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
 | 
			
		||||
@@ -800,12 +810,8 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
 | 
			
		||||
	format_add(ft, "window_menu", "%s", DEFAULT_WINDOW_MENU);
 | 
			
		||||
	format_add(ft, "pane_menu", "%s", DEFAULT_PANE_MENU);
 | 
			
		||||
 | 
			
		||||
	if (item != NULL) {
 | 
			
		||||
		if (item->cmd != NULL)
 | 
			
		||||
			format_add(ft, "command", "%s", item->cmd->entry->name);
 | 
			
		||||
		if (item->shared != NULL && item->shared->formats != NULL)
 | 
			
		||||
			format_merge(ft, item->shared->formats);
 | 
			
		||||
	}
 | 
			
		||||
	if (item != NULL)
 | 
			
		||||
		format_create_add_item(ft, item);
 | 
			
		||||
 | 
			
		||||
	return (ft);
 | 
			
		||||
}
 | 
			
		||||
@@ -1408,7 +1414,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
 | 
			
		||||
    char **buf, size_t *len, size_t *off)
 | 
			
		||||
{
 | 
			
		||||
	struct window_pane	*wp = ft->wp;
 | 
			
		||||
	const char		*errptr, *copy, *cp;
 | 
			
		||||
	const char		*errptr, *copy, *cp, *marker;
 | 
			
		||||
	char			*copy0, *condition, *found, *new;
 | 
			
		||||
	char			*value, *left, *right;
 | 
			
		||||
	size_t			 valuelen;
 | 
			
		||||
@@ -1448,12 +1454,16 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
 | 
			
		||||
				sub = fm;
 | 
			
		||||
				break;
 | 
			
		||||
			case '=':
 | 
			
		||||
				if (fm->argc != 1)
 | 
			
		||||
				if (fm->argc != 1 && fm->argc != 2)
 | 
			
		||||
					break;
 | 
			
		||||
				limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
 | 
			
		||||
				    &errptr);
 | 
			
		||||
				if (errptr != NULL)
 | 
			
		||||
					limit = 0;
 | 
			
		||||
				if (fm->argc == 2 && fm->argv[1] != NULL)
 | 
			
		||||
					marker = fm->argv[1];
 | 
			
		||||
				else
 | 
			
		||||
					marker = NULL;
 | 
			
		||||
				break;
 | 
			
		||||
			case 'l':
 | 
			
		||||
				modifiers |= FORMAT_LITERAL;
 | 
			
		||||
@@ -1668,14 +1678,24 @@ done:
 | 
			
		||||
	/* Truncate the value if needed. */
 | 
			
		||||
	if (limit > 0) {
 | 
			
		||||
		new = format_trim_left(value, limit);
 | 
			
		||||
		format_log(ft, "applied length limit %d: %s", limit, new);
 | 
			
		||||
		free(value);
 | 
			
		||||
		value = new;
 | 
			
		||||
		if (marker != NULL && strcmp(new, value) != 0) {
 | 
			
		||||
			free(value);
 | 
			
		||||
			xasprintf(&value, "%s%s", new, marker);
 | 
			
		||||
		} else {
 | 
			
		||||
			free(value);
 | 
			
		||||
			value = new;
 | 
			
		||||
		}
 | 
			
		||||
		format_log(ft, "applied length limit %d: %s", limit, value);
 | 
			
		||||
	} else if (limit < 0) {
 | 
			
		||||
		new = format_trim_right(value, -limit);
 | 
			
		||||
		format_log(ft, "applied length limit %d: %s", limit, new);
 | 
			
		||||
		free(value);
 | 
			
		||||
		value = new;
 | 
			
		||||
		if (marker != NULL && strcmp(new, value) != 0) {
 | 
			
		||||
			free(value);
 | 
			
		||||
			xasprintf(&value, "%s%s", marker, new);
 | 
			
		||||
		} else {
 | 
			
		||||
			free(value);
 | 
			
		||||
			value = new;
 | 
			
		||||
		}
 | 
			
		||||
		format_log(ft, "applied length limit %d: %s", limit, value);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Expand the buffer and copy in the value. */
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								tmux.1
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								tmux.1
									
									
									
									
									
								
							@@ -3989,9 +3989,16 @@ a number and a colon.
 | 
			
		||||
Positive numbers count from the start of the string and negative from the end,
 | 
			
		||||
so
 | 
			
		||||
.Ql #{=5:pane_title}
 | 
			
		||||
will include at most the first 5 characters of the pane title, or
 | 
			
		||||
will include at most the first five characters of the pane title, or
 | 
			
		||||
.Ql #{=-5:pane_title}
 | 
			
		||||
the last 5 characters.
 | 
			
		||||
the last five characters.
 | 
			
		||||
A suffix or prefix may be given as a second argument - if provided then it is
 | 
			
		||||
appended or prepended to the string if the length has been trimmed, for example
 | 
			
		||||
.Ql #{=/5/...:pane_title}
 | 
			
		||||
will append
 | 
			
		||||
.Ql ...
 | 
			
		||||
if the pane title is more than five characters.
 | 
			
		||||
.Pp
 | 
			
		||||
Prefixing a time variable with
 | 
			
		||||
.Ql t:
 | 
			
		||||
will convert it to a string, so if
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user