[systemd-devel] [patch] job: print colored status message at the right spot

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed Dec 21 06:08:14 PST 2011


On 12/21/2011 02:23 PM, Jan Engelhardt wrote:
> parent 8f7f7a1bd3a26f501b2d6546cce1c669b59dcc87 (v37-96-g8f7f7a1)
> commit 3b27d8497a0abb1403bc1c77c576641293dcea40
> Author: Jan Engelhardt<jengelh at medozas.de>
> Date:   Wed Dec 21 14:15:19 2011 +0100
>
> job: print colored status message at the right spot
>
> Instead of always hardcoding 80 cols, do what SUSE did in rc.status
> and determine the actual terminal width.
> ---
>   src/unit.c |   16 ++++++----------
>   src/util.c |   23 +++++++++++++++++++++++
>   src/util.h |    1 +
>   3 files changed, 30 insertions(+), 10 deletions(-)
>
> diff --git a/src/unit.c b/src/unit.c
> index 03c90f5..2e7e519 100644
> --- a/src/unit.c
> +++ b/src/unit.c
> @@ -2441,9 +2441,9 @@ int unit_coldplug(Unit *u) {
>
>   void unit_status_printf(Unit *u, const char *status, const char *format, ...) {
>           va_list ap;
> -        char *s, *e;
> +        char *e;
>           int err;
> -        const unsigned emax = status ? 80 - (sizeof("[  OK  ]")-1) : 80;
> +        unsigned int emax;
>
>           assert(u);
>           assert(format);
> @@ -2458,18 +2458,14 @@ void unit_status_printf(Unit *u, const char *status, const char *format, ...) {
>                   return;
>
>           va_start(ap, format);
> -        err = vasprintf(&s, format, ap);
> +        err = vasprintf(&e, format, ap);
>           va_end(ap);
>           if (err<  0)
>                   return;
>
> -        e = ellipsize(s, emax, 100);
> -        free(s);
> -        if (!e)
> -                return;
> -
> -        if (status)
> -                status_printf("%s%*s[%s]\n", e, emax - strlen(e), "", status);
> +        emax = console_width();
> +        if (status != NULL)
> +                status_printf("%s\r\x1b[%dC\x1b[10D%s\n", e, emax, status);
>           else
>                   status_printf("%s\n", e);
>           free(e);
> diff --git a/src/util.c b/src/util.c
> index da71e4d..edfccd4 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -2539,6 +2539,29 @@ int open_terminal(const char *name, int mode) {
>           return fd;
>   }
>
> +unsigned int console_width(void)
> +{
> +	struct winsize winsize;
> +	unsigned int width;
> +	const char *s;
> +	int fd;
> +
> +	s = getenv("COLUMNS");
> +	if (s != NULL) {
> +		width = strtoul(s, NULL, 0);
> +		if (width != 0)
> +			return width;
> +	}
> +	width = 80;
> +	fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
> +	if (fd<  0)
> +		return width;
> +	if (ioctl(fd, TIOCGWINSZ,&winsize) == 0)
> +		width = winsize.ws_col;
> +	close(fd);
> +	return width;
> +}
> +

Hi,
there already is columns() in src/util.c :)

Best,
Zbyszek


>   int flush_fd(int fd) {
>           struct pollfd pollfd;
>
> diff --git a/src/util.h b/src/util.h
> index a71a297..1071b7f 100644
> --- a/src/util.h
> +++ b/src/util.h
> @@ -326,6 +326,7 @@ int ask(char *ret, const char *replies, const char *text, ...);
>
>   int reset_terminal_fd(int fd);
>   int reset_terminal(const char *name);
> +unsigned int console_width(void);
>
>   int open_terminal(const char *name, int mode);
>   int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);



More information about the systemd-devel mailing list