mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Add p format modifier for padding to width.
This commit is contained in:
		
							
								
								
									
										25
									
								
								format.c
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								format.c
									
									
									
									
									
								
							@@ -1299,7 +1299,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/* Now try single character with arguments. */
 | 
							/* Now try single character with arguments. */
 | 
				
			||||||
		if (strchr("mCs=", cp[0]) == NULL)
 | 
							if (strchr("mCs=p", cp[0]) == NULL)
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		c = cp[0];
 | 
							c = cp[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1565,7 +1565,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
 | 
				
			|||||||
	char			 *copy0, *condition, *found, *new;
 | 
						char			 *copy0, *condition, *found, *new;
 | 
				
			||||||
	char			 *value, *left, *right;
 | 
						char			 *value, *left, *right;
 | 
				
			||||||
	size_t			  valuelen;
 | 
						size_t			  valuelen;
 | 
				
			||||||
	int			  modifiers = 0, limit = 0, j;
 | 
						int			  modifiers = 0, limit = 0, width = 0, j;
 | 
				
			||||||
	struct format_modifier   *list, *fm, *cmp = NULL, *search = NULL;
 | 
						struct format_modifier   *list, *fm, *cmp = NULL, *search = NULL;
 | 
				
			||||||
	struct format_modifier	**sub = NULL;
 | 
						struct format_modifier	**sub = NULL;
 | 
				
			||||||
	u_int			  i, count, nsub = 0;
 | 
						u_int			  i, count, nsub = 0;
 | 
				
			||||||
@@ -1611,6 +1611,14 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
 | 
				
			|||||||
				if (fm->argc >= 2 && fm->argv[1] != NULL)
 | 
									if (fm->argc >= 2 && fm->argv[1] != NULL)
 | 
				
			||||||
					marker = fm->argv[1];
 | 
										marker = fm->argv[1];
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
 | 
								case 'p':
 | 
				
			||||||
 | 
									if (fm->argc < 1)
 | 
				
			||||||
 | 
										break;
 | 
				
			||||||
 | 
									width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
 | 
				
			||||||
 | 
									    &errptr);
 | 
				
			||||||
 | 
									if (errptr != NULL)
 | 
				
			||||||
 | 
										width = 0;
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
			case 'l':
 | 
								case 'l':
 | 
				
			||||||
				modifiers |= FORMAT_LITERAL;
 | 
									modifiers |= FORMAT_LITERAL;
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
@@ -1845,6 +1853,19 @@ done:
 | 
				
			|||||||
		format_log(ft, "applied length limit %d: %s", limit, value);
 | 
							format_log(ft, "applied length limit %d: %s", limit, value);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Pad the value if needed. */
 | 
				
			||||||
 | 
						if (width > 0) {
 | 
				
			||||||
 | 
							new = utf8_padcstr(value, width);
 | 
				
			||||||
 | 
							free(value);
 | 
				
			||||||
 | 
							value = new;
 | 
				
			||||||
 | 
							format_log(ft, "applied padding width %d: %s", width, value);
 | 
				
			||||||
 | 
						} else if (width < 0) {
 | 
				
			||||||
 | 
							new = utf8_rpadcstr(value, -width);
 | 
				
			||||||
 | 
							free(value);
 | 
				
			||||||
 | 
							value = new;
 | 
				
			||||||
 | 
							format_log(ft, "applied padding width %d: %s", width, value);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Expand the buffer and copy in the value. */
 | 
						/* Expand the buffer and copy in the value. */
 | 
				
			||||||
	valuelen = strlen(value);
 | 
						valuelen = strlen(value);
 | 
				
			||||||
	while (*len - *off < valuelen + 1) {
 | 
						while (*len - *off < valuelen + 1) {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								tmux.1
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								tmux.1
									
									
									
									
									
								
							@@ -4101,6 +4101,12 @@ appended or prepended to the string if the length has been trimmed, for example
 | 
				
			|||||||
will append
 | 
					will append
 | 
				
			||||||
.Ql ...
 | 
					.Ql ...
 | 
				
			||||||
if the pane title is more than five characters.
 | 
					if the pane title is more than five characters.
 | 
				
			||||||
 | 
					Similarly,
 | 
				
			||||||
 | 
					.Ql p
 | 
				
			||||||
 | 
					pads the string to a given width, for example
 | 
				
			||||||
 | 
					.Ql #{p10:pane_title}
 | 
				
			||||||
 | 
					will result in a width of at least 10 characters.
 | 
				
			||||||
 | 
					A positive width pads on the left, a negative on the right.
 | 
				
			||||||
.Pp
 | 
					.Pp
 | 
				
			||||||
Prefixing a time variable with
 | 
					Prefixing a time variable with
 | 
				
			||||||
.Ql t:\&
 | 
					.Ql t:\&
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								tmux.h
									
									
									
									
									
								
							@@ -2638,6 +2638,7 @@ struct utf8_data *utf8_fromcstr(const char *);
 | 
				
			|||||||
char		*utf8_tocstr(struct utf8_data *);
 | 
					char		*utf8_tocstr(struct utf8_data *);
 | 
				
			||||||
u_int		 utf8_cstrwidth(const char *);
 | 
					u_int		 utf8_cstrwidth(const char *);
 | 
				
			||||||
char		*utf8_padcstr(const char *, u_int);
 | 
					char		*utf8_padcstr(const char *, u_int);
 | 
				
			||||||
 | 
					char		*utf8_rpadcstr(const char *, u_int);
 | 
				
			||||||
int		 utf8_cstrhas(const char *, const struct utf8_data *);
 | 
					int		 utf8_cstrhas(const char *, const struct utf8_data *);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* procname.c */
 | 
					/* procname.c */
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								utf8.c
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								utf8.c
									
									
									
									
									
								
							@@ -390,7 +390,7 @@ utf8_cstrwidth(const char *s)
 | 
				
			|||||||
	return (width);
 | 
						return (width);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Pad UTF-8 string to width. Caller frees. */
 | 
					/* Pad UTF-8 string to width on the left. Caller frees. */
 | 
				
			||||||
char *
 | 
					char *
 | 
				
			||||||
utf8_padcstr(const char *s, u_int width)
 | 
					utf8_padcstr(const char *s, u_int width)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -411,6 +411,27 @@ utf8_padcstr(const char *s, u_int width)
 | 
				
			|||||||
	return (out);
 | 
						return (out);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Pad UTF-8 string to width on the right. Caller frees. */
 | 
				
			||||||
 | 
					char *
 | 
				
			||||||
 | 
					utf8_rpadcstr(const char *s, u_int width)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						size_t	 slen;
 | 
				
			||||||
 | 
						char	*out;
 | 
				
			||||||
 | 
						u_int	  n, i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						n = utf8_cstrwidth(s);
 | 
				
			||||||
 | 
						if (n >= width)
 | 
				
			||||||
 | 
							return (xstrdup(s));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						slen = strlen(s);
 | 
				
			||||||
 | 
						out = xmalloc(slen + 1 + (width - n));
 | 
				
			||||||
 | 
						for (i = 0; i < width - n; i++)
 | 
				
			||||||
 | 
							out[i] = ' ';
 | 
				
			||||||
 | 
						memcpy(out + i, s, slen);
 | 
				
			||||||
 | 
						out[i + slen] = '\0';
 | 
				
			||||||
 | 
						return (out);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
utf8_cstrhas(const char *s, const struct utf8_data *ud)
 | 
					utf8_cstrhas(const char *s, const struct utf8_data *ud)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user