Imported Upstream version 2.1.2
[platform/upstream/fdupes.git] / ncurses-status.c
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-status.h"
26
27 struct status_text *status_text_alloc(struct status_text *status, size_t width)
28 {
29   struct status_text *result;
30   wchar_t *newleft;
31   wchar_t *newright;
32
33   if (status == 0)
34   {
35     result = (struct status_text*) malloc(sizeof(struct status_text));
36     if (result == 0)
37       return 0;
38
39     result->left = (wchar_t*) malloc((width+1) * sizeof(wchar_t));
40     if (result->left == 0)
41     {
42       free(result);
43       return 0;
44     }
45
46     result->right = (wchar_t*) malloc((width+1) * sizeof(wchar_t));
47     if (result->right == 0)
48     {
49       free(result->left);
50       free(result);
51       return 0;
52     }
53
54     result->left[0] = '\0';
55     result->right[0] = '\0';
56
57     result->width = width;
58   }
59   else
60   {
61     if (status->width >= width)
62       return status;
63
64     newleft = (wchar_t*) realloc(status->left, (width+1) * sizeof(wchar_t));
65     if (newleft == 0)
66       return 0;
67
68     newright = (wchar_t*) realloc(status->right, (width+1) * sizeof(wchar_t));
69     if (newright == 0)
70     {
71       free(newleft);
72       return 0;
73     }
74
75     result = status;
76     result->left = newleft;
77     result->right = newright;
78     result->width = width;
79   }
80
81   return result;
82 }
83
84 void free_status_text(struct status_text *status)
85 {
86   free(status->left);
87   free(status->right);
88   free(status);
89 }
90
91 void format_status_left(struct status_text *status, wchar_t *format, ...)
92 {
93   va_list ap;
94   va_list aq;
95   int size;
96
97   va_start(ap, format);
98   va_copy(aq, ap);
99
100   size = vwprintflength(format, aq);
101
102   status_text_alloc(status, size);
103
104   vswprintf(status->left, status->width + 1, format, ap);
105
106   va_end(aq);
107   va_end(ap);
108 }
109
110 void format_status_right(struct status_text *status, wchar_t *format, ...)
111 {
112   va_list ap;
113   va_list aq;
114   int size;
115
116   va_start(ap, format);
117   va_copy(aq, ap);
118
119   size = vwprintflength(format, aq);
120
121   status_text_alloc(status, size);
122
123   vswprintf(status->right, status->width + 1, format, ap);
124
125   va_end(aq);
126   va_end(ap);
127 }
128
129 void print_status(WINDOW *statuswin, struct status_text *status)
130 {
131   wchar_t *text;
132   size_t cols;
133   size_t x;
134   size_t l;
135
136   cols = getmaxx(statuswin);
137
138   text = (wchar_t*)malloc((cols + 1) * sizeof(wchar_t));
139
140   l = wcslen(status->left);
141   for (x = 0; x < l && x < cols; ++x)
142     text[x] = status->left[x];
143   for (x = l; x < cols; ++x)
144     text[x] = L' ';
145
146   l = wcslen(status->right);
147   for (x = cols; x >= 1 && l >= 1; --x, --l)
148     text[x - 1] = status->right[l - 1];
149
150   text[cols] = L'\0';
151
152   mvwaddnwstr(statuswin, 0, 0, text, wcslen(text));
153
154   free(text);
155 }