1 /* FDUPES Copyright (c) 2018 Adrian Lopez
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:
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
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. */
24 #include "ncurses-print.h"
25 #include "ncurses-prompt.h"
27 struct prompt_info *prompt_info_alloc(size_t initial_size)
29 struct prompt_info *out;
34 out = (struct prompt_info*) malloc(sizeof(struct prompt_info));
38 out->text = malloc(initial_size * sizeof(wchar_t));
46 out->allocated = initial_size;
54 void free_prompt_info(struct prompt_info *info)
60 int format_prompt(struct prompt_info *prompt, wchar_t *format, ...)
70 size = vwprintflength(format, aq);
72 if (size + 3 > prompt->allocated)
74 newtext = (wchar_t*)realloc(prompt->text, (size + 3) * sizeof(wchar_t));
79 prompt->text = newtext;
80 prompt->allocated = size + 1;
83 vswprintf(prompt->text, prompt->allocated, format, ap);
85 size = wcslen(prompt->text);
87 prompt->text[size + 0] = L':';
88 prompt->text[size + 1] = L' ';
89 prompt->text[size + 2] = L'\0';
97 void set_prompt_active_state(struct prompt_info *prompt, int active)
99 prompt->active = active;
102 void update_prompt(WINDOW *promptwin, struct prompt_info *prompt, wchar_t *commandbuffer, int cursor_delta)
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;
108 if (cursor_delta > 0)
110 if (prompt->cursor + cursor_delta > wcslen(commandbuffer))
111 cursor_delta = wcslen(commandbuffer) - prompt->cursor;
113 if (cursor_position + cursor_delta >= right_edge)
114 prompt->offset += cursor_delta;
116 else if (cursor_delta < 0)
118 if (-cursor_delta > prompt->cursor)
119 cursor_delta = -(int)prompt->cursor;
121 if (cursor_position + cursor_delta < cursor_stop)
122 prompt->offset += cursor_delta;
125 prompt->cursor += cursor_delta;
128 void print_prompt(WINDOW *promptwin, struct prompt_info *prompt, wchar_t *commandbuffer)
131 prompt->text[wcslen(prompt->text) - 2] = '=';
133 prompt->text[wcslen(prompt->text) - 2] = ':';
137 if (prompt->offset <= wcslen(prompt->text))
139 wattron(promptwin, A_BOLD);
140 waddwstr(promptwin, prompt->text + prompt->offset);
141 wattroff(promptwin, A_BOLD);
143 waddwstr(promptwin, commandbuffer);
145 else if (prompt->offset < wcslen(prompt->text) + wcslen(commandbuffer))
147 waddwstr(promptwin, commandbuffer + prompt->offset - wcslen(prompt->text));