"Fossies" - the Fresh Open Source Software Archive

Member "fdupes-2.1.2/ncurses-prompt.c" (12 Aug 2020, 3963 Bytes) of package /linux/privat/fdupes-2.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ncurses-prompt.c" see the Fossies "Dox" file reference documentation.

    1 /* FDUPES Copyright (c) 2018 Adrian Lopez
    2 
    3    Permission is hereby granted, free of charge, to any person
    4    obtaining a copy of this software and associated documentation files
    5    (the "Software"), to deal in the Software without restriction,
    6    including without limitation the rights to use, copy, modify, merge,
    7    publish, distribute, sublicense, and/or sell copies of the Software,
    8    and to permit persons to whom the Software is furnished to do so,
    9    subject to the following conditions:
   10 
   11    The above copyright notice and this permission notice shall be
   12    included in all copies or substantial portions of the Software.
   13 
   14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
   15    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
   16    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
   17    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
   18    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
   19    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
   20    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
   21 
   22 #include "config.h"
   23 #include <stdlib.h>
   24 #include "ncurses-print.h"
   25 #include "ncurses-prompt.h"
   26 
   27 struct prompt_info *prompt_info_alloc(size_t initial_size)
   28 {
   29   struct prompt_info *out;
   30 
   31   if (initial_size < 1)
   32     return 0;
   33 
   34   out = (struct prompt_info*) malloc(sizeof(struct prompt_info));
   35   if (out == 0)
   36     return 0;
   37 
   38   out->text = malloc(initial_size * sizeof(wchar_t));
   39   if (out->text == 0)
   40   {
   41     free(out);
   42     return 0;
   43   }
   44 
   45   out->text[0] = L'\0';
   46   out->allocated = initial_size;
   47   out->offset = 0;
   48   out->cursor = 0;
   49   out->active = 0;
   50 
   51   return out;
   52 }
   53 
   54 void free_prompt_info(struct prompt_info *info)
   55 {
   56   free(info->text);
   57   free(info);
   58 }
   59 
   60 int format_prompt(struct prompt_info *prompt, wchar_t *format, ...)
   61 {
   62   va_list ap;
   63   va_list aq;
   64   int size;
   65   wchar_t *newtext;
   66 
   67   va_start(ap, format);
   68   va_copy(aq, ap);
   69 
   70   size = vwprintflength(format, aq);
   71 
   72   if (size + 3 > prompt->allocated)
   73   {
   74     newtext = (wchar_t*)realloc(prompt->text, (size + 3) * sizeof(wchar_t));
   75 
   76     if (newtext == 0)
   77       return 0;
   78 
   79     prompt->text = newtext;
   80     prompt->allocated = size + 1;
   81   }
   82 
   83   vswprintf(prompt->text, prompt->allocated, format, ap);
   84 
   85   size = wcslen(prompt->text);
   86 
   87   prompt->text[size + 0] = L':';
   88   prompt->text[size + 1] = L' ';
   89   prompt->text[size + 2] = L'\0';
   90 
   91   va_end(aq);
   92   va_end(ap);
   93 
   94   return 1;
   95 }
   96 
   97 void set_prompt_active_state(struct prompt_info *prompt, int active)
   98 {
   99   prompt->active = active;
  100 }
  101 
  102 void update_prompt(WINDOW *promptwin, struct prompt_info *prompt, wchar_t *commandbuffer, int cursor_delta)
  103 {
  104   const size_t cursor_stop = wcslen(prompt->text);
  105   const size_t right_edge = getmaxx(promptwin);
  106   const size_t cursor_position = cursor_stop + prompt->cursor - prompt->offset;
  107 
  108   if (cursor_delta > 0)
  109   {
  110     if (prompt->cursor + cursor_delta > wcslen(commandbuffer))
  111      cursor_delta = wcslen(commandbuffer) - prompt->cursor;
  112 
  113     if (cursor_position + cursor_delta >= right_edge)
  114       prompt->offset += cursor_delta;
  115   }
  116   else if (cursor_delta < 0)
  117   {
  118     if (-cursor_delta > prompt->cursor)
  119       cursor_delta = -(int)prompt->cursor;
  120 
  121     if (cursor_position + cursor_delta < cursor_stop)
  122       prompt->offset += cursor_delta;
  123   }
  124 
  125   prompt->cursor += cursor_delta;
  126 }
  127 
  128 void print_prompt(WINDOW *promptwin, struct prompt_info *prompt, wchar_t *commandbuffer)
  129 {
  130   if (prompt->active)
  131     prompt->text[wcslen(prompt->text) - 2] = '=';
  132   else
  133     prompt->text[wcslen(prompt->text) - 2] = ':';
  134 
  135   werase(promptwin);
  136 
  137   if (prompt->offset <= wcslen(prompt->text))
  138   {
  139     wattron(promptwin, A_BOLD);
  140     waddwstr(promptwin, prompt->text + prompt->offset);
  141     wattroff(promptwin, A_BOLD);
  142 
  143     waddwstr(promptwin, commandbuffer);
  144   }
  145   else if (prompt->offset < wcslen(prompt->text) + wcslen(commandbuffer))
  146   {
  147     waddwstr(promptwin, commandbuffer + prompt->offset - wcslen(prompt->text));
  148   }
  149 }