-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
139 lines (122 loc) · 3.87 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "utils.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ncurses.h>
#include "config.h"
// Helper function to trim whitespace
char* trim(char *str) {
char *end;
while(isspace((unsigned char)*str)) str++;
if(*str == 0) return str;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
char* clean_text(const char *text) {
char *cleaned = malloc(strlen(text) * 2 + 1); // Allocate more space to be safe
int i = 0, j = 0;
int capitalize_next = 1; // Capitalize the first character
while (text[i]) {
if ((text[i] >= 'a' && text[i] <= 'z') ||
(text[i] >= 'A' && text[i] <= 'Z') ||
(text[i] >= '0' && text[i] <= '9') ||
text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
text[i] == '!' || text[i] == '?' || text[i] == '\'' ||
text[i] == '"' || text[i] == '-' || text[i] == ';' ||
text[i] == ':' || text[i] == '(' || text[i] == ')') {
if (capitalize_next && isalpha(text[i])) {
cleaned[j++] = toupper(text[i]);
capitalize_next = 0;
} else {
cleaned[j++] = text[i];
}
// Set flag to capitalize next character after sentence-ending punctuation
if (text[i] == '.' || text[i] == '!' || text[i] == '?') {
capitalize_next = 1;
}
} else if (text[i] == '\n' || text[i] == '\r') {
// Replace newlines with spaces to avoid word cutoffs
cleaned[j++] = ' ';
}
i++;
}
cleaned[j] = '\0';
// Trim leading and trailing whitespace
char *final = trim(cleaned);
if (final != cleaned) {
char *temp = strdup(final);
free(cleaned);
return temp;
}
return cleaned;
}
// Text wrapping function
char **wrap_text(const char *text, int width, int *line_count) {
int text_len = strlen(text);
int max_lines = text_len / width + 1;
char **lines = malloc(max_lines * sizeof(char *));
*line_count = 0;
int line_start = 0;
int line_end = width;
while (line_start < text_len) {
if (line_end >= text_len) {
line_end = text_len;
} else {
while (line_end > line_start && !isspace(text[line_end])) {
line_end--;
}
if (line_end == line_start) {
line_end = line_start + width;
}
}
int line_length = line_end - line_start;
lines[*line_count] = malloc(line_length + 1);
strncpy(lines[*line_count], text + line_start, line_length);
lines[*line_count][line_length] = '\0';
(*line_count)++;
line_start = line_end;
while (line_start < text_len && isspace(text[line_start])) {
line_start++;
}
line_end = line_start + width;
}
return lines;
}
// Color support function
void setup_colors() {
if (has_colors()) {
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK); // Default
init_pair(2, COLOR_BLACK, COLOR_WHITE); // Inverted
init_pair(3, COLOR_YELLOW, COLOR_BLACK); // Highlight
}
}
// Utility functions for pagination
int pgup(int pos, int winhi, int preservedline, int c) {
if (pos >= (winhi - preservedline) * c) {
return pos - (winhi - preservedline) * c;
} else {
return 0;
}
}
int pgdn(int pos, int tot, int winhi, int preservedline, int c) {
(void)preservedline; // Mark as unused
if (pos + (winhi * c) <= tot - winhi) {
return pos + (winhi * c);
} else {
pos = tot - winhi;
if (pos < 0) {
return 0;
}
return pos;
}
}
int pgend(int tot, int winhi) {
if (tot - winhi >= 0) {
return tot - winhi;
} else {
return 0;
}
}