From 0e0105e55931ab5483668dee9c972e6ec4cec279 Mon Sep 17 00:00:00 2001 From: Boris Faure Date: Sat, 30 Jul 2016 16:31:56 +0200 Subject: [PATCH] make ty* fail nicely when in tmux/screen --- src/bin/Makefile.am | 14 ++++++++++---- src/bin/tyalpha.c | 6 +++++- src/bin/tybg.c | 7 ++++++- src/bin/tycat.c | 5 ++++- src/bin/tycommon.c | 21 +++++++++++++++++++++ src/bin/tycommon.h | 18 ++++++++++++++++++ src/bin/tyls.c | 6 ++++-- src/bin/typop.c | 8 ++++++-- src/bin/tyq.c | 8 ++++++-- 9 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 src/bin/tycommon.c create mode 100644 src/bin/tycommon.h diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am index f34b1687..8d600dc7 100644 --- a/src/bin/Makefile.am +++ b/src/bin/Makefile.am @@ -63,43 +63,48 @@ gravatar.c gravatar.h \ tty_keys.h tybg_SOURCES = \ +tycommon.c \ tybg.c tybg_CPPFLAGS = -I. \ -DPACKAGE_BIN_DIR=\"$(bindir)\" -DPACKAGE_LIB_DIR=\"$(libdir)\" \ --DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" +-DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" @TERMINOLOGY_CFLAGS@ tybg_LDADD = tyalpha_SOURCES = \ +tycommon.c \ tyalpha.c tyalpha_CPPFLAGS = -I. \ -DPACKAGE_BIN_DIR=\"$(bindir)\" -DPACKAGE_LIB_DIR=\"$(libdir)\" \ --DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" +-DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" @TERMINOLOGY_CFLAGS@ tyalpha_LDADD = typop_SOURCES = \ +tycommon.c \ typop.c typop_CPPFLAGS = -I. \ -DPACKAGE_BIN_DIR=\"$(bindir)\" -DPACKAGE_LIB_DIR=\"$(libdir)\" \ --DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" +-DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" @TERMINOLOGY_CFLAGS@ typop_LDADD = tyq_SOURCES = \ +tycommon.c \ tyq.c tyq_CPPFLAGS = -I. \ -DPACKAGE_BIN_DIR=\"$(bindir)\" -DPACKAGE_LIB_DIR=\"$(libdir)\" \ --DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" +-DPACKAGE_DATA_DIR=\"$(pkgdatadir)\" @TERMINOLOGY_CFLAGS@ tyq_LDADD = tycat_SOURCES = \ tycat.c \ +tycommon.c \ extns.c extns.h tycat_CPPFLAGS = -I. \ @@ -110,6 +115,7 @@ tycat_LDADD = @TERMINOLOGY_LIBS@ tyls_SOURCES = \ tyls.c \ +tycommon.c \ extns.h tyls_CPPFLAGS = -I. \ diff --git a/src/bin/tyalpha.c b/src/bin/tyalpha.c index ac189d7a..7b55b7a2 100644 --- a/src/bin/tyalpha.c +++ b/src/bin/tyalpha.c @@ -4,12 +4,16 @@ #include #include +#include +#include "tycommon.h" + int main(int argc, char **argv) { int i, perm = 0; - if (!getenv("TERMINOLOGY")) return 0; + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if (argc <= 1) { printf("Usage: %s [-p] on|off|\n" diff --git a/src/bin/tybg.c b/src/bin/tybg.c index 722c5983..cc3df433 100644 --- a/src/bin/tybg.c +++ b/src/bin/tybg.c @@ -4,12 +4,17 @@ #include #include +#include +#include "tycommon.h" + + int main(int argc, char **argv) { int i, perm = 0; - if (!getenv("TERMINOLOGY")) return 0; + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) { diff --git a/src/bin/tycat.c b/src/bin/tycat.c index 1dec71ce..709640dc 100644 --- a/src/bin/tycat.c +++ b/src/bin/tycat.c @@ -11,6 +11,8 @@ #include #include +#include "tycommon.h" + enum { CENTER, FILL, @@ -308,7 +310,8 @@ main(int argc, char **argv) char *rp; Eina_List *file_q = NULL; - if (!getenv("TERMINOLOGY")) return 0; + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if (argc <= 1) { print_usage(argv[0]); diff --git a/src/bin/tycommon.c b/src/bin/tycommon.c new file mode 100644 index 00000000..6ae6efb0 --- /dev/null +++ b/src/bin/tycommon.c @@ -0,0 +1,21 @@ +#include +#include +#include "tycommon.h" + + +Eina_Bool +is_running_in_terminology(void) +{ + if (!getenv("TERMINOLOGY")) + return EINA_FALSE; + + // Terminology's escape codes do not got through tmux + if (getenv("TMUX")) + return EINA_FALSE; + + // Terminology's escape codes do not got through screen + if (getenv("STY")) + return EINA_FALSE; + + return EINA_TRUE; +} diff --git a/src/bin/tycommon.h b/src/bin/tycommon.h new file mode 100644 index 00000000..f29b2474 --- /dev/null +++ b/src/bin/tycommon.h @@ -0,0 +1,18 @@ +#ifndef _TY_COMMON_H__ +#define _TY_COMMON_H__ 1 + +Eina_Bool is_running_in_terminology(void); + +#define ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1() \ + do \ + { \ + if (!is_running_in_terminology()) \ + { \ + fprintf(stderr, "not running in terminology\n"); \ + exit(1); \ + } \ + } \ + while (0) + + +#endif diff --git a/src/bin/tyls.c b/src/bin/tyls.c index 8469ff7d..9bdd3054 100644 --- a/src/bin/tyls.c +++ b/src/bin/tyls.c @@ -11,6 +11,7 @@ #include #include #include +#include "tycommon.h" // this code sucks. just letting you know... in advance... in case you // might be tempted to think otherwise... :) @@ -725,8 +726,9 @@ main(int argc, char **argv) char *path; Eina_List *dirs = NULL; Tyls_Options options = {SMALL, EINA_FALSE}; - - if (!getenv("TERMINOLOGY")) return 0; + + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if ((argc == 2) && (!strcmp(argv[1], "-h"))) { printf("Usage: %s [-a] [-s|-m] FILE1 [FILE2 ...]\n" diff --git a/src/bin/typop.c b/src/bin/typop.c index d777d3a5..569ade5d 100644 --- a/src/bin/typop.c +++ b/src/bin/typop.c @@ -4,12 +4,16 @@ #include #include +#include +#include "tycommon.h" + int main(int argc, char **argv) { int i; - - if (!getenv("TERMINOLOGY")) return 0; + + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if (argc <= 1) { printf("Usage: %s FILE1 [FILE2 ...]\n" diff --git a/src/bin/tyq.c b/src/bin/tyq.c index 1a73c4e5..f78fcc0d 100644 --- a/src/bin/tyq.c +++ b/src/bin/tyq.c @@ -4,12 +4,16 @@ #include #include +#include +#include "tycommon.h" + int main(int argc, char **argv) { int i; - - if (!getenv("TERMINOLOGY")) return 0; + + ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1(); + if (argc <= 1) { printf("Usage: %s FILE1 [FILE2 ...]\n"