1 /** 2 * Copyright (c) 2014, Timothy Stack 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * * Neither the name of Timothy Stack nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * @file term_extra.hh 30 */ 31 32 #ifndef term_extra_hh 33 #define term_extra_hh 34 35 #include <string.h> 36 #include <sys/types.h> 37 #include <pwd.h> 38 #include <unistd.h> 39 #include <sys/param.h> 40 41 #include <string> 42 43 #include "logfile.hh" 44 #include "log_format.hh" 45 #include "listview_curses.hh" 46 47 class term_extra { 48 public: term_extra()49 term_extra() { 50 const char *term_name = getenv("TERM"); 51 52 this->te_enabled = (term_name != nullptr && strstr(term_name, "xterm") != nullptr); 53 54 if (getenv("SSH_CONNECTION") != nullptr) { 55 char hostname[MAXHOSTNAMELEN] = "UNKNOWN"; 56 struct passwd *userent; 57 58 gethostname(hostname, sizeof(hostname)); 59 this->te_prefix = hostname; 60 if ((userent = getpwuid(getuid())) != nullptr) { 61 this->te_prefix = std::string(userent->pw_name) + "@" + this->te_prefix; 62 } 63 this->te_prefix += ":"; 64 } 65 }; 66 update_title(listview_curses * lc)67 void update_title(listview_curses *lc) { 68 static const char *xterm_title_fmt = "\033]0;%s\007"; 69 70 if (!this->te_enabled) { 71 return; 72 } 73 74 if (lc->get_inner_height() > 0) { 75 string_attrs_t::const_iterator line_attr; 76 std::vector<attr_line_t> rows(1); 77 78 lc->get_data_source()->listview_value_for_rows(*lc, lc->get_top(), rows); 79 string_attrs_t &sa = rows[0].get_attrs(); 80 line_attr = find_string_attr(sa, &logline::L_FILE); 81 if (line_attr != sa.end()) { 82 logfile *lf = (logfile *)line_attr->sa_value.sav_ptr; 83 const std::string &filename = lf->get_unique_path(); 84 85 if (filename != this->te_last_title) { 86 std::string title = this->te_prefix + filename; 87 88 printf(xterm_title_fmt, title.c_str()); 89 fflush(stdout); 90 91 this->te_last_title = filename; 92 } 93 return; 94 } 95 } 96 97 const std::string &view_title = lc->get_title(); 98 99 if (view_title != this->te_last_title) { 100 std::string title = this->te_prefix + view_title; 101 102 printf(xterm_title_fmt, title.c_str()); 103 fflush(stdout); 104 105 this->te_last_title = view_title; 106 } 107 }; 108 109 private: 110 bool te_enabled; 111 std::string te_prefix; 112 std::string te_last_title; 113 }; 114 115 #endif 116