From 7f17d1123ba52a49eac7754ae2058ce8139b5529 Mon Sep 17 00:00:00 2001 From: Sofian Brabez Date: Wed, 27 Feb 2013 14:17:04 +0100 Subject: [PATCH] add pkg_title function which send proper ansi escape code for the given term --- pkg/Makefile | 1 + pkg/event.c | 30 +++++++++++++----------- pkg/pkgcli.h | 3 +++ pkg/term.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 pkg/term.c diff --git a/pkg/Makefile b/pkg/Makefile index 7396e06..d3a6a4d 100644 --- a/pkg/Makefile +++ b/pkg/Makefile @@ -24,6 +24,7 @@ SRCS= add.c \ search.c \ set.c \ shlib.c \ + term.c \ updating.c \ utils.c \ version.c \ diff --git a/pkg/event.c b/pkg/event.c index ba49f62..99ece55 100644 --- a/pkg/event.c +++ b/pkg/event.c @@ -4,7 +4,7 @@ * Copyright (c) 2011 Will Andrews * Copyright (c) 2011-2012 Marin Atanasov Nikolov * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -14,7 +14,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -97,7 +97,7 @@ event_callback(void *data, struct pkg_event *ev) printf("[%d/%d] ", nbdone, nbactions); printf("Installing %s-%s...", name, version); /* print to the terminal title*/ - printf("%c]0;[%d/%d] Installing %s-%s%c", '\033', nbdone, nbactions, name, version, '\007'); + pkg_title(ev->e_install_begin.pkg, "Installing"); break; case PKG_EVENT_INSTALL_FINISHED: @@ -130,8 +130,8 @@ event_callback(void *data, struct pkg_event *ev) if (nbactions > 0) printf("[%d/%d] ", nbdone, nbactions); printf("Deleting %s-%s...", name, version); - printf("%c]0;[%d/%d] Deleting %s-%s%c", '\033', nbdone, - nbactions, name, version, '\007'); + pkg_title(ev->e_deinstall_begin.pkg, "Deleting"); + break; case PKG_EVENT_DEINSTALL_FINISHED: if (quiet) @@ -150,22 +150,26 @@ event_callback(void *data, struct pkg_event *ev) case 1: printf("Downgrading %s from %s to %s...", name, version, newversion); - printf("%c]0;[%d/%d] Downgrading %s from %s to %s%c", - '\033', nbdone, nbactions, name, version, - newversion, '\007'); + messages = sbuf_new_auto(); + sbuf_printf(messages, "Downgrading %s from %s to %s ", name, version, newversion); + pkg_title(ev->e_upgrade_begin.pkg, sbuf_data(messages)); + sbuf_delete(messages); + break; case 0: printf("Reinstalling %s-%s", name, version); - printf("%c]0;[%d/%d] Reinstalling %s-%s%c", '\033', - nbdone, nbactions, name, version, '\007'); + pkg_title(ev->e_upgrade_begin.pkg, "Reinstalling"); + break; case -1: printf("Upgrading %s from %s to %s...", name, version, newversion); - printf("%c]0;[%d/%d] Upgrading %s from %s to %s%c", - '\033', nbdone, nbactions, name, version, - newversion, '\007'); + messages = sbuf_new_auto(); + sbuf_printf(messages, "Upgrading %s from %s to %s ", name, version, newversion); + pkg_title(ev->e_upgrade_begin.pkg, sbuf_data(messages)); + sbuf_delete(messages); + break; } break; diff --git a/pkg/pkgcli.h b/pkg/pkgcli.h index 36fcccb..a84575d 100644 --- a/pkg/pkgcli.h +++ b/pkg/pkgcli.h @@ -162,6 +162,9 @@ void usage_which(void); int exec_convert(int, char **); void usage_convert(void); +/* pkg term */ +void pkg_title(struct pkg *, const char*); + /* utils */ /* These are the fields of the Full output, in order */ diff --git a/pkg/term.c b/pkg/term.c new file mode 100644 index 0000000..1d36077 --- /dev/null +++ b/pkg/term.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2013 Sofian Brabez + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include "pkgcli.h" + +#define TERM_COUNT 7 + +struct { + const char *name; + const char *escape_begin; /* hexadecimal or octal value see ascii(7) */ + const char *escape_end; /* hexadecimal or octal value see ascii(7) */ +} terms[TERM_COUNT] = { + {"xterm", "\033]0;", "\007"}, + {"eterm", "\033]0;", "\007"}, + {"aterm", "\033]0;", "\007"}, + {"kterm", "\033]0;", "\007"}, + {"rxvt", "\033]0;", "\007"}, + {"screen", "\x1bk", "\x1b\\"}, + {"tmux", "\x1bk", "\x1b\\"} +}; + +void +pkg_title(struct pkg *pkg, const char *message) { + int i; + char *term = getenv("TERM"); + char *pkgname, *pkgversion; + + pkg_get(pkg, PKG_NAME, &pkgname); + pkg_get(pkg, PKG_VERSION, &pkgversion); + + for (i=0; i