[22cf42d9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <io/console.h>
|
---|
| 30 | #include <errno.h>
|
---|
| 31 | #include <fmtutil.h>
|
---|
[38d150e] | 32 | #include <stdlib.h>
|
---|
[1d6dd2a] | 33 | #include <str.h>
|
---|
[22cf42d9] | 34 |
|
---|
| 35 | typedef struct {
|
---|
| 36 | align_mode_t alignment;
|
---|
| 37 | bool newline_always;
|
---|
| 38 | size_t width;
|
---|
| 39 | } printmode_t;
|
---|
| 40 |
|
---|
[b7fd2a0] | 41 | errno_t print_wrapped_console(const char *str, align_mode_t alignment)
|
---|
[22cf42d9] | 42 | {
|
---|
| 43 | console_ctrl_t *console = console_init(stdin, stdout);
|
---|
| 44 | if (console == NULL) {
|
---|
| 45 | printf("%s", str);
|
---|
| 46 | return EOK;
|
---|
| 47 | }
|
---|
| 48 | sysarg_t con_rows, con_cols, con_col, con_row;
|
---|
[b7fd2a0] | 49 | errno_t rc = console_get_size(console, &con_cols, &con_rows);
|
---|
[22cf42d9] | 50 | if (rc != EOK) {
|
---|
| 51 | return rc;
|
---|
| 52 | }
|
---|
| 53 | rc = console_get_pos(console, &con_col, &con_row);
|
---|
| 54 | if (rc != EOK) {
|
---|
| 55 | return rc;
|
---|
| 56 | }
|
---|
| 57 | if (con_col != 0) {
|
---|
| 58 | printf("\n");
|
---|
| 59 | }
|
---|
| 60 | return print_wrapped(str, con_cols, alignment);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[6045ecf] | 63 | /** Line consumer that prints the lines aligned according to spec
|
---|
| 64 | *
|
---|
| 65 | **/
|
---|
[b7fd2a0] | 66 | static errno_t print_line(wchar_t *wstr, size_t chars, bool last, void *data)
|
---|
[22cf42d9] | 67 | {
|
---|
| 68 | printmode_t *pm = (printmode_t *) data;
|
---|
[6045ecf] | 69 | wchar_t old_char = wstr[chars];
|
---|
[22cf42d9] | 70 | wstr[chars] = 0;
|
---|
[b7fd2a0] | 71 | errno_t rc = print_aligned_w(wstr, pm->width, last, pm->alignment);
|
---|
[6045ecf] | 72 | wstr[chars] = old_char;
|
---|
| 73 | return rc;
|
---|
[22cf42d9] | 74 | }
|
---|
| 75 |
|
---|
[b7fd2a0] | 76 | errno_t print_wrapped(const char *str, size_t width, align_mode_t mode)
|
---|
[22cf42d9] | 77 | {
|
---|
| 78 | printmode_t pm;
|
---|
| 79 | pm.alignment = mode;
|
---|
| 80 | pm.newline_always = false;
|
---|
| 81 | pm.width = width;
|
---|
| 82 | wchar_t *wstr = str_to_awstr(str);
|
---|
| 83 | if (wstr == NULL) {
|
---|
| 84 | return ENOMEM;
|
---|
| 85 | }
|
---|
[b7fd2a0] | 86 | errno_t rc = wrap(wstr, width, print_line, &pm);
|
---|
[22cf42d9] | 87 | free(wstr);
|
---|
| 88 | return rc;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[b7fd2a0] | 91 | errno_t print_aligned_w(const wchar_t *wstr, size_t width, bool last,
|
---|
[6045ecf] | 92 | align_mode_t mode)
|
---|
[22cf42d9] | 93 | {
|
---|
| 94 | size_t i;
|
---|
| 95 | size_t len = wstr_length(wstr);
|
---|
[6045ecf] | 96 | if (mode == ALIGN_LEFT || (mode == ALIGN_JUSTIFY && last)) {
|
---|
[22cf42d9] | 97 | for (i = 0; i < width; i++) {
|
---|
| 98 | if (i < len)
|
---|
| 99 | putchar(wstr[i]);
|
---|
| 100 | else
|
---|
| 101 | putchar(' ');
|
---|
| 102 | }
|
---|
[1433ecda] | 103 | } else if (mode == ALIGN_RIGHT) {
|
---|
[22cf42d9] | 104 | for (i = 0; i < width; i++) {
|
---|
| 105 | if (i < width - len)
|
---|
| 106 | putchar(' ');
|
---|
| 107 | else
|
---|
[1433ecda] | 108 | putchar(wstr[i - (width - len)]);
|
---|
[22cf42d9] | 109 | }
|
---|
[1433ecda] | 110 | } else if (mode == ALIGN_CENTER) {
|
---|
[22cf42d9] | 111 | size_t padding = (width - len) / 2;
|
---|
| 112 | for (i = 0; i < width; i++) {
|
---|
| 113 | if ((i < padding) || ((i - padding) >= len))
|
---|
| 114 | putchar(' ');
|
---|
| 115 | else
|
---|
[1433ecda] | 116 | putchar(wstr[i - padding]);
|
---|
[22cf42d9] | 117 | }
|
---|
[1433ecda] | 118 | } else if (mode == ALIGN_JUSTIFY) {
|
---|
[22cf42d9] | 119 | size_t words = 0;
|
---|
| 120 | size_t word_chars = 0;
|
---|
| 121 | bool space = true;
|
---|
| 122 | for (i = 0; i < len; i++) {
|
---|
| 123 | if (space && wstr[i] != ' ') {
|
---|
| 124 | words++;
|
---|
| 125 | }
|
---|
| 126 | space = wstr[i] == ' ';
|
---|
[1433ecda] | 127 | if (!space)
|
---|
| 128 | word_chars++;
|
---|
[22cf42d9] | 129 | }
|
---|
[19d007e] | 130 | size_t done_words = 0;
|
---|
| 131 | size_t done_chars = 0;
|
---|
| 132 | if (words == 0)
|
---|
| 133 | goto skip_words;
|
---|
[1433ecda] | 134 | size_t excess_spaces = width - word_chars - (words - 1);
|
---|
[22cf42d9] | 135 | space = true;
|
---|
| 136 | i = 0;
|
---|
| 137 | size_t j;
|
---|
| 138 | while (i < len) {
|
---|
| 139 | /* Find a word */
|
---|
[1433ecda] | 140 | while (i < len && wstr[i] == ' ')
|
---|
| 141 | i++;
|
---|
| 142 | if (i == len)
|
---|
| 143 | break;
|
---|
[22cf42d9] | 144 | if (done_words) {
|
---|
[fcfac250] | 145 | size_t spaces = 1 + (((done_words *
|
---|
| 146 | excess_spaces) / (words - 1)) -
|
---|
| 147 | (((done_words - 1) * excess_spaces) /
|
---|
| 148 | (words - 1)));
|
---|
[22cf42d9] | 149 | for (j = 0; j < spaces; j++) {
|
---|
| 150 | putchar(' ');
|
---|
| 151 | }
|
---|
| 152 | done_chars += spaces;
|
---|
| 153 | }
|
---|
| 154 | while (i < len && wstr[i] != ' ') {
|
---|
| 155 | putchar(wstr[i++]);
|
---|
| 156 | done_chars++;
|
---|
| 157 | }
|
---|
| 158 | done_words++;
|
---|
| 159 | }
|
---|
[1433ecda] | 160 | skip_words:
|
---|
[22cf42d9] | 161 | while (done_chars < width) {
|
---|
| 162 | putchar(' ');
|
---|
| 163 | done_chars++;
|
---|
| 164 | }
|
---|
[1433ecda] | 165 | } else {
|
---|
[e406736] | 166 | return EINVAL;
|
---|
| 167 | }
|
---|
[a35b458] | 168 |
|
---|
[22cf42d9] | 169 | return EOK;
|
---|
| 170 | }
|
---|
[b7fd2a0] | 171 | errno_t print_aligned(const char *str, size_t width, bool last, align_mode_t mode)
|
---|
[22cf42d9] | 172 | {
|
---|
| 173 | wchar_t *wstr = str_to_awstr(str);
|
---|
| 174 | if (wstr == NULL) {
|
---|
| 175 | return ENOMEM;
|
---|
| 176 | }
|
---|
[b7fd2a0] | 177 | errno_t rc = print_aligned_w(wstr, width, last, mode);
|
---|
[22cf42d9] | 178 | free(wstr);
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[b7fd2a0] | 182 | errno_t wrap(wchar_t *wstr, size_t width, line_consumer_fn consumer, void *data)
|
---|
[22cf42d9] | 183 | {
|
---|
| 184 | size_t word_start = 0;
|
---|
| 185 | size_t last_word_end = 0;
|
---|
| 186 | size_t line_start = 0;
|
---|
| 187 | size_t line_len = 0;
|
---|
| 188 | size_t pos = 0;
|
---|
[a35b458] | 189 |
|
---|
[22cf42d9] | 190 | /*
|
---|
| 191 | * Invariants:
|
---|
| 192 | * * line_len = last_word_end - line_start
|
---|
| 193 | * * line_start <= last_word_end <= word_start <= pos
|
---|
| 194 | */
|
---|
[a35b458] | 195 |
|
---|
[22cf42d9] | 196 | while (wstr[pos] != 0) {
|
---|
| 197 | /* Skip spaces and process newlines */
|
---|
| 198 | while (wstr[pos] == ' ' || wstr[pos] == '\n') {
|
---|
| 199 | if (wstr[pos] == '\n') {
|
---|
[6045ecf] | 200 | consumer(wstr + line_start, line_len, true,
|
---|
| 201 | data);
|
---|
[22cf42d9] | 202 | last_word_end = line_start = pos + 1;
|
---|
| 203 | line_len = 0;
|
---|
| 204 | }
|
---|
| 205 | pos++;
|
---|
| 206 | }
|
---|
| 207 | word_start = pos;
|
---|
| 208 | /* Find end of word */
|
---|
| 209 | while (wstr[pos] != 0 && wstr[pos] != ' ' &&
|
---|
| 210 | wstr[pos] != '\n')
|
---|
| 211 | pos++;
|
---|
[6045ecf] | 212 | bool last = wstr[pos] == 0;
|
---|
[22cf42d9] | 213 | /* Check if the line still fits width */
|
---|
| 214 | if (pos - line_start > width) {
|
---|
| 215 | if (line_len > 0)
|
---|
[6045ecf] | 216 | consumer(wstr + line_start, line_len, last,
|
---|
| 217 | data);
|
---|
[22cf42d9] | 218 | line_start = last_word_end = word_start;
|
---|
| 219 | line_len = 0;
|
---|
| 220 | }
|
---|
| 221 | /* Check if we need to force wrap of long word*/
|
---|
| 222 | if (pos - word_start > width) {
|
---|
[6045ecf] | 223 | consumer(wstr + word_start, width, last, data);
|
---|
[22cf42d9] | 224 | pos = line_start = last_word_end = word_start + width;
|
---|
| 225 | line_len = 0;
|
---|
| 226 | }
|
---|
| 227 | last_word_end = pos;
|
---|
| 228 | line_len = last_word_end - line_start;
|
---|
| 229 | }
|
---|
| 230 | /* Here we have less than width chars starting from line_start.
|
---|
[1b20da0] | 231 | * Moreover, the last portion does not contain spaces or newlines
|
---|
[22cf42d9] | 232 | */
|
---|
| 233 | if (pos - line_start > 0)
|
---|
[6045ecf] | 234 | consumer(wstr + line_start, pos - line_start, true, data);
|
---|
[22cf42d9] | 235 |
|
---|
| 236 | return EOK;
|
---|
| 237 | }
|
---|
| 238 |
|
---|