-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
182 lines (172 loc) · 4.01 KB
/
display.c
File metadata and controls
182 lines (172 loc) · 4.01 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
#include "display.h"
#include "Parser.h"
#include <string.h>
#include <limits.h>
int min(int x, int y)
{
if (x > y)
return y;
else
return x;
}
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
void printer(int currx, int curry, int *arr, int C, int R)
{
printf(" ");
for (int i = 0; i < (C - currx < 10 ? C - currx : 10); i++)
{
int val = currx + i + 1;
char s[7] = {0}, s2[7] = {0}; // Increased size to prevent overflow
int idx = 0;
while (val > 0 && idx < 6) // Prevent overflow
{
val--;
s[idx++] = 'A' + (val % 26);
val /= 26;
}
s[idx] = '\0';
// Reverse the string for correct order
for (int j = 0; j < idx; j++)
{
s2[j] = s[idx - j - 1];
}
s2[idx] = '\0'; // Ensure null termination
printf("%-10s", s2); // Changed to left-align the column headers
}
printf("\n");
for (int j = 0; j < (R - curry < 10 ? R - curry : 10); j++)
{
printf("%-3d", curry + j + 1); // Row number (left-aligned)
printf(" ");
for (int i = 0; i < (C - currx < 10 ? C - currx : 10); i++)
{
int value = arr[(currx + i) + C * (curry + j)];
if (value == INT_MIN)
{
printf("%-10s", "ERR"); // Left-aligned "ERR"
}
else
{
printf("%-10d", value); // Left-aligned numbers
}
}
printf("\n");
}
}
int scroller(char *a, int *arr, int *currx, int *curry, int C, int R, Graph *graph)
{
int flag = 0;
if (a[0] == 'w' && a[1] == '\0')
{
// If there are less than 10 rows above, scroll to the top
if (*curry < 10)
{
if (*curry > 0) // If we're not already at the top
{
*curry = 0;
}
else
{
flag = 1;
}
}
else
{
*curry -= 10;
}
}
else if (a[0] == 'd' && a[1] == '\0')
{
// Calculate how many columns we can scroll right
int remaining_cols = C - *currx - 10;
if (remaining_cols <= 0)
{
flag = 1;
}
else if (remaining_cols < 10)
{
*currx += remaining_cols;
}
else
{
*currx += 10;
}
}
else if (a[0] == 'a' && a[1] == '\0')
{
// If there are less than 10 columns to the left, scroll to the leftmost
if (*currx < 10)
{
if (*currx > 0) // If we're not already at the leftmost
{
*currx = 0;
}
else
{
flag = 1;
}
}
else
{
*currx -= 10;
}
}
else if (a[0] == 's' && a[1] == '\0')
{
// Calculate how many rows we can scroll down
int remaining_rows = R - *curry - 10;
if (remaining_rows <= 0)
{
flag = 1;
}
else if (remaining_rows < 10)
{
*curry += remaining_rows;
}
else
{
*curry += 10;
}
}
else if (strncmp(a, "scroll_to ", 10) == 0)
{
int cell = cell_parser(a, C, R, 10, strlen(a) - 1, graph);
if (cell == -1)
{
flag = 1;
}
else
{
int start_row = cell / C;
int start_col = cell % C;
if (start_row >= R || start_col >= C)
{
// printf("Scroll target out of bounds\n");
flag = 1;
}
else
{
*currx = start_col;
*curry = start_row;
}
}
}
else
{
// printf("unrecognized command");
return -1;
}
if (flag)
{
; // Do nothing on invalid scroll
}
}