home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  8.4 KB  |  339 lines

  1. /* status.c -- a simple status line management file. */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #ifdef HAVE_UTSNAME
  35. #include <sys/utsname.h>
  36. #endif
  37.  
  38. #ifdef TIME_WITH_SYS_TIME
  39. #include <sys/time.h>
  40. #include <time.h>
  41. #else
  42. #ifdef HAVE_SYS_TIME_H
  43. #include <sys/time.h>
  44. #else
  45. #include <time.h>
  46. #endif
  47. #endif
  48.  
  49. #include "xstring.h"
  50. #include "xmalloc.h"
  51. #include "window.h"
  52. #include "status.h"
  53. #include "configure.h"
  54. #include "tty.h"
  55. #include "misc.h"
  56.  
  57.  
  58. extern int  AnsiColorSequences;
  59.  
  60.  
  61. static window_t *status_win = NULL;
  62. static char *stemp;
  63. static int columns;
  64. static int line;
  65. static int UseLastScreenChar;
  66. static char *default_msg;
  67.  
  68. #ifdef HAVE_UTSNAME
  69. static struct utsname u;
  70. #endif
  71.  
  72.  
  73. #define STATUSBAR_FIELDS        9
  74.  
  75. static char *StatusBarFields[STATUSBAR_FIELDS] =
  76. {
  77.     "StatusBarForeground",
  78.     "StatusBarBackground",
  79.     "StatusBarBrightness",
  80.     "StatusBarWarningForeground",
  81.     "StatusBarWarningBackground",
  82.     "StatusBarWarningBrightness",
  83.     "StatusBarErrorForeground",
  84.     "StatusBarErrorBackground",
  85.     "StatusBarErrorBrightness"
  86. };
  87.  
  88. #ifdef HAVE_LINUX
  89. static int StatusBarColors[STATUSBAR_FIELDS] =
  90. {
  91.     BLACK, CYAN, OFF, BLACK, WHITE, OFF, WHITE, RED, ON
  92. };
  93. #else   /* !HAVE_LINUX */
  94. static int StatusBarColors[STATUSBAR_FIELDS] =
  95. {
  96.     BLACK, WHITE, OFF, BLACK, WHITE, OFF, BLACK, WHITE, ON
  97. };
  98. #endif  /* !HAVE_LINUX */
  99.  
  100. #define StatusBarForeground             StatusBarColors[0]
  101. #define StatusBarBackground             StatusBarColors[1]
  102. #define StatusBarBrightness             StatusBarColors[2]
  103. #define StatusBarWarningForeground      StatusBarColors[3]
  104. #define StatusBarWarningBackground      StatusBarColors[4]
  105. #define StatusBarWarningBrightness      StatusBarColors[5]
  106. #define StatusBarErrorForeground        StatusBarColors[6]
  107. #define StatusBarErrorBackground        StatusBarColors[7]
  108. #define StatusBarErrorBrightness        StatusBarColors[8]
  109.  
  110.  
  111. void
  112. status_init(_columns, _begin_y, def_msg)
  113.     int _columns, _begin_y;
  114.     char *def_msg;
  115. {
  116.     use_section("[Setup]");
  117.  
  118.     UseLastScreenChar = get_flag_var("UseLastScreenChar", OFF);
  119.  
  120.  
  121.     use_section(AnsiColorSequences ? cSection : bwSection);
  122.  
  123.     get_colorset_var(StatusBarColors, StatusBarFields, STATUSBAR_FIELDS);
  124.  
  125.  
  126.     if (!UseLastScreenChar)
  127.         _columns--;
  128.  
  129.     columns     = _columns;
  130.     line        = _begin_y;
  131.     default_msg = def_msg;
  132.     status_win  = window_init(0, _begin_y, 1, _columns);
  133.     stemp       = xmalloc(columns);
  134.  
  135. #ifdef HAVE_UTSNAME
  136.     uname(&u);
  137. #endif
  138. }
  139.  
  140.  
  141. void
  142. status_end()
  143. {
  144.     window_end(status_win);
  145. }
  146.  
  147.  
  148. static void
  149. build_msg(msg_name, center)
  150.    char *msg_name;
  151.    int center;
  152. {
  153.     int i, j;
  154.     struct tm *time;
  155.     char date_str[32];
  156.     char *ptr, *temp_msg;
  157.     size_t len, temp_msg_len;
  158.  
  159.  
  160.     memset(stemp, ' ', columns);
  161.  
  162.     if (msg_name == NULL)
  163.         msg_name = default_msg;
  164.  
  165.     temp_msg = xmalloc(temp_msg_len = (strlen(msg_name) + 1));
  166.  
  167.     for (i = 0, j = 0; msg_name[i]; i++)
  168.         if (msg_name[i] == '\\')
  169.             switch (msg_name[i + 1])
  170.             {
  171.                 case 'h' :
  172.  
  173. #ifdef HAVE_UTSNAME
  174.                         ptr = u.nodename;
  175. #else
  176.                         ptr = "";
  177. #endif
  178.                         goto get_system_info;
  179.  
  180.                 case 's' :
  181.  
  182. #ifdef HAVE_UTSNAME
  183.                         ptr = u.sysname;
  184. #else
  185.                         ptr = "";
  186. #endif
  187.                         goto get_system_info;
  188.  
  189.                 case 'm' :
  190.  
  191. #ifdef HAVE_UTSNAME
  192.                         ptr = u.machine;
  193. #else
  194.                         ptr = "";
  195. #endif
  196.  
  197.                             get_system_info:
  198.  
  199.                                 if (ptr[0])
  200.                                 {
  201.                                     len = strlen(ptr);
  202.                                     temp_msg = xrealloc(temp_msg,
  203.                                                         temp_msg_len += len);
  204.                                     memcpy(&temp_msg[j], ptr, len);
  205.                                 }
  206.                                 else
  207.                                 {
  208.                                     len = 6;
  209.                                     temp_msg = xrealloc(temp_msg,
  210.                                                         temp_msg_len += len);
  211.                                     memcpy(&temp_msg[j], "(none)", len);
  212.                                 }
  213.  
  214.                                 j += len;
  215.                                 i++;
  216.                                 break;
  217.  
  218.                 case 'd' :      time = get_local_time();
  219.                                 sprintf(date_str, "%s %s %02d %d",
  220.                                         day_name[time->tm_wday],
  221.                                         month_name[time->tm_mon],
  222.                                         time->tm_mday,
  223.                                         time->tm_year + 1900);
  224.                                 len = strlen(date_str);
  225.                                 temp_msg = xrealloc(temp_msg,
  226.                                                     temp_msg_len += len);
  227.                                 memcpy(&temp_msg[j], date_str, len);
  228.                                 j += len;
  229.                                 i++;
  230.                                 break;
  231.  
  232.                 case '\\':      temp_msg[j++] = '\\';
  233.                                 i++;
  234.                                 break;
  235.  
  236.                 case '\0':      temp_msg[j++] = '\\';
  237.                                 break;
  238.  
  239.                 default  :      temp_msg[j++] = '\\';
  240.                                 temp_msg[j++] = msg_name[++i];
  241.                                 break;
  242.             }
  243.         else
  244.         {
  245.             if (msg_name[i] == '\t')
  246.             {
  247.                 temp_msg = xrealloc(temp_msg, temp_msg_len += 8);
  248.                 memcpy(&temp_msg[j], "        ", 8);
  249.                 j += 8;
  250.             }
  251.             else
  252.                 temp_msg[j++] = msg_name[i];
  253.         }
  254.  
  255.     temp_msg[j] = 0;
  256.  
  257.     len = strlen(msg_name = temp_msg);
  258.  
  259.     if (center && len < columns)
  260.         memcpy(stemp + ((columns - len) >> 1), msg_name, len);
  261.     else
  262.         memcpy(stemp, msg_name, min(len, columns));
  263.  
  264.     xfree(temp_msg);
  265.  
  266.     for (i = 0; i < columns; i++)
  267.         if (stemp[i] == '\r' || stemp[i] == '\n')
  268.             stemp[i] = ' ';
  269. }
  270.  
  271.  
  272. int
  273. status(msg_name, wait, sound, restore, msg_type, center)
  274.     char *msg_name;
  275.     int wait, sound, restore, msg_type, center;
  276. {
  277.     int key = 0;
  278.     tty_key_t *ks;
  279.     tty_status_t status;
  280.  
  281.  
  282.     tty_save(&status);
  283.     tty_cursor(OFF);
  284.  
  285.     build_msg(msg_name, center);
  286.  
  287.     if (sound)
  288.         tty_beep();
  289.  
  290.     switch (msg_type)
  291.     {
  292.         case MSG_WARNING:
  293.  
  294.             tty_colors(StatusBarWarningBrightness,
  295.                        StatusBarWarningForeground,
  296.                        StatusBarWarningBackground);
  297.             break;
  298.  
  299.         case MSG_ERROR:
  300.  
  301.             tty_colors(StatusBarErrorBrightness,
  302.                        StatusBarErrorForeground,
  303.                        StatusBarErrorBackground);
  304.             break;
  305.  
  306.         default:
  307.  
  308.             tty_colors(StatusBarBrightness,
  309.                        StatusBarForeground,
  310.                        StatusBarBackground);
  311.             break;
  312.     }
  313.  
  314.     window_cursormove_notify(status_win, 0, 0);
  315.     window_write(stemp, columns);
  316.  
  317.     if (wait)
  318.     {
  319.         ks = tty_get_key(NULL);
  320.         window_cursormove(status_win, line, columns);
  321.         key = ks->key_seq[0];
  322.     }
  323.  
  324.     if (restore)
  325.     {
  326.         build_msg(default_msg, MSG_CENTERED);
  327.  
  328.         tty_colors(StatusBarBrightness,
  329.                    StatusBarForeground,
  330.                    StatusBarBackground);
  331.  
  332.         window_cursormove_notify(status_win, 0, 0);
  333.         window_write(stemp, columns);
  334.     }
  335.  
  336.     tty_restore(&status);
  337.     return key;
  338. }
  339.