messenger-cli

Command-line user interface for GNUnet Messenger
Log | Files | Refs | README | LICENSE

util.c (3244B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2022--2025 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author Tobias Frisch
     22  * @file util.c
     23  */
     24 
     25 #include "util.h"
     26 #include <string.h>
     27 
     28 void
     29 util_print_logo(WINDOW *window)
     30 {
     31   int x = getcurx(window);
     32   int y = getcury(window);
     33 
     34   wmove(window, y++, x); wprintw(window, "                            ");
     35   wmove(window, y++, x); wprintw(window, "    o/                 \\o   ");
     36   wmove(window, y++, x); wprintw(window, "  ooo                   oo  ");
     37   wmove(window, y++, x); wprintw(window, "    \\oooo\\        /oooo/    ");
     38   wmove(window, y++, x); wprintw(window, "         oo      ooo        ");
     39   wmove(window, y++, x); wprintw(window, "          oo    ooo         ");
     40   wmove(window, y++, x); wprintw(window, "           ooooooo          ");
     41   wmove(window, y++, x); wprintw(window, "           \\oooo/           ");
     42   wmove(window, y++, x); wprintw(window, "            oooo            ");
     43   wmove(window, y++, x); wprintw(window, "                            ");
     44 }
     45 
     46 void
     47 util_print_info(WINDOW *window,
     48                 const char *info)
     49 {
     50   const int x = getmaxx(window) - strlen(info) - 1;
     51   const int y = getcury(window);
     52 
     53   if ((x + (y - 2) / 2 < UTIL_LOGO_COLS - 7) ||
     54       ((y < 4) && (x < UTIL_LOGO_COLS)))
     55     return;
     56 
     57   wmove(window, y, x);
     58   wprintw(window, "%s", info);
     59 }
     60 
     61 void
     62 util_print_prompt(WINDOW *window,
     63                   const char *prompt)
     64 {
     65   const int x = getcurx(window);
     66   const int y = getcury(window);
     67   int remaining = getmaxx(window) - x;
     68 
     69   if (strlen(prompt) < remaining)
     70     wprintw(window, "%s", prompt);
     71   else
     72   {
     73     for (int i = 0; i < remaining; i++)
     74     {
     75       wmove(window, y, x + i);
     76       wprintw(window, "%c", prompt[i]);
     77     }
     78   }
     79 }
     80 
     81 void
     82 util_init_unique_colors()
     83 {
     84   start_color();
     85 
     86   init_pair(1, COLOR_RED, COLOR_BLACK);
     87   init_pair(2, COLOR_GREEN, COLOR_BLACK);
     88   init_pair(3, COLOR_YELLOW, COLOR_BLACK);
     89   init_pair(4, COLOR_BLUE, COLOR_BLACK);
     90   init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
     91   init_pair(6, COLOR_CYAN, COLOR_BLACK);
     92 }
     93 
     94 void
     95 util_enable_unique_color(WINDOW *window,
     96                          const void *data)
     97 {
     98   if ((FALSE == has_colors()) || (!data))
     99     return;
    100 
    101   const int attr_color = COLOR_PAIR(1 + ((size_t) data) % UTIL_UNIQUE_COLORS);
    102   wattron(window, attr_color);
    103 }
    104 
    105 void
    106 util_disable_unique_color(WINDOW *window,
    107                           const void *data)
    108 {
    109   if ((FALSE == has_colors()) || (!data))
    110     return;
    111 
    112   const int attr_color = COLOR_PAIR(1 + ((size_t) data) % UTIL_UNIQUE_COLORS);
    113   wattroff(window, attr_color);
    114 }