forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VT100GridTypes.h
257 lines (216 loc) · 7.52 KB
/
VT100GridTypes.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// VT100GridTypes.h
// iTerm
//
// Created by George Nachman on 10/13/13.
//
//
#import <Foundation/Foundation.h>
typedef struct {
int x;
int y;
} VT100GridCoord;
typedef struct {
int width;
int height;
} VT100GridSize;
typedef struct {
int location;
int length;
} VT100GridRange;
typedef struct {
VT100GridCoord origin;
VT100GridSize size;
} VT100GridRect;
typedef struct {
VT100GridCoord origin;
int length;
} VT100GridRun;
typedef struct {
VT100GridCoord start;
VT100GridCoord end;
} VT100GridCoordRange;
typedef struct {
VT100GridCoordRange coordRange;
VT100GridRange columnWindow;
} VT100GridWindowedRange;
@interface NSValue (VT100Grid)
+ (NSValue *)valueWithGridCoord:(VT100GridCoord)coord;
+ (NSValue *)valueWithGridSize:(VT100GridSize)size;
+ (NSValue *)valueWithGridRange:(VT100GridRange)range;
+ (NSValue *)valueWithGridRect:(VT100GridRect)rect;
+ (NSValue *)valueWithGridRun:(VT100GridRun)run;
+ (NSValue *)valueWithGridCoordRange:(VT100GridCoordRange)coordRange;
- (VT100GridCoord)gridCoordValue;
- (VT100GridSize)gridSizeValue;
- (VT100GridRange)gridRangeValue;
- (VT100GridRect)gridRectValue;
- (VT100GridRun)gridRunValue;
- (VT100GridCoordRange)gridCoordRangeValue;
// Use for sorting array of VT100GridCoorRange's in NSValue*s by the start coord.
- (NSComparisonResult)compareGridCoordRangeStart:(NSValue *)other;
@end
NSString *VT100GridCoordRangeDescription(VT100GridCoordRange range);
NSString *VT100GridWindowedRangeDescription(VT100GridWindowedRange range);
NS_INLINE VT100GridCoord VT100GridCoordMake(int x, int y) {
VT100GridCoord coord;
coord.x = x;
coord.y = y;
return coord;
}
NS_INLINE VT100GridSize VT100GridSizeMake(int width, int height) {
VT100GridSize size;
size.width = width;
size.height = height;
return size;
}
NS_INLINE VT100GridRange VT100GridRangeMake(int location, int length) {
VT100GridRange range;
range.location = location;
range.length = length;
return range;
}
NS_INLINE BOOL VT100GridRangeContains(VT100GridRange range, int value) {
return value >= range.location && value < range.location + range.length;
}
NS_INLINE int VT100GridRangeMax(VT100GridRange range) {
return range.location + range.length - 1;
}
NS_INLINE VT100GridRect VT100GridRectMake(int x, int y, int width, int height) {
VT100GridRect rect;
rect.origin = VT100GridCoordMake(x, y);
rect.size = VT100GridSizeMake(width, height);
return rect;
}
NS_INLINE BOOL VT100GridRectEquals(VT100GridRect a, VT100GridRect b) {
return (a.origin.x == b.origin.x &&
a.origin.y == b.origin.y &&
a.size.width == b.size.width &&
a.size.height == b.size.height);
}
NS_INLINE BOOL VT100GridCoordEquals(VT100GridCoord a, VT100GridCoord b) {
return a.x == b.x && a.y == b.y;
}
NS_INLINE VT100GridWindowedRange VT100GridWindowedRangeMake(VT100GridCoordRange range,
int windowStart,
int windowWidth) {
VT100GridWindowedRange windowedRange;
windowedRange.coordRange = range;
windowedRange.columnWindow.location = windowStart;
windowedRange.columnWindow.length = windowWidth;
return windowedRange;
}
NS_INLINE VT100GridCoord VT100GridWindowedRangeStart(VT100GridWindowedRange range) {
VT100GridCoord coord = range.coordRange.start;
if (range.columnWindow.length) {
coord.x = MIN(MAX(coord.x, range.columnWindow.location),
range.columnWindow.location + range.columnWindow.length);
}
return coord;
}
NS_INLINE VT100GridCoord VT100GridWindowedRangeEnd(VT100GridWindowedRange range) {
VT100GridCoord coord = range.coordRange.end;
if (range.columnWindow.length) {
coord.x = MIN(coord.x, VT100GridRangeMax(range.columnWindow) + 1);
}
return coord;
}
// Ascending: a < b
// Descending: a > b
// Same: a == b
NS_INLINE NSComparisonResult VT100GridCoordOrder(VT100GridCoord a, VT100GridCoord b) {
if (a.y < b.y) {
return NSOrderedAscending;
}
if (a.y > b.y) {
return NSOrderedDescending;
}
if (a.x < b.x) {
return NSOrderedAscending;
}
if (a.x > b.x) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
NS_INLINE VT100GridRun VT100GridRunMake(int x, int y, int length) {
VT100GridRun run;
run.origin.x = x;
run.origin.y = y;
run.length = length;
return run;
}
NS_INLINE VT100GridCoordRange VT100GridCoordRangeMake(int startX, int startY, int endX, int endY) {
VT100GridCoordRange coordRange;
coordRange.start.x = startX;
coordRange.start.y = startY;
coordRange.end.x = endX;
coordRange.end.y = endY;
return coordRange;
}
NS_INLINE NSString *VT100GridCoordDescription(VT100GridCoord c) {
return [NSString stringWithFormat:@"(%d, %d)", c.x, c.y];
}
NS_INLINE VT100GridCoord VT100GridCoordRangeMin(VT100GridCoordRange range) {
if (VT100GridCoordOrder(range.start, range.end) == NSOrderedAscending) {
return range.start;
} else {
return range.end;
}
}
NS_INLINE VT100GridCoord VT100GridCoordRangeMax(VT100GridCoordRange range) {
if (VT100GridCoordOrder(range.start, range.end) == NSOrderedAscending) {
return range.end;
} else {
return range.start;
}
}
NS_INLINE long long VT100GridCoordDistance(VT100GridCoord a, VT100GridCoord b, int gridWidth) {
long long aPos = a.y;
aPos *= gridWidth;
aPos += a.x;
long long bPos = b.y;
bPos *= gridWidth;
bPos += b.x;
return llabs(aPos - bPos);
}
NS_INLINE long long VT100GridWindowedRangeLength(VT100GridWindowedRange range, int gridWidth) {
if (range.coordRange.start.y == range.coordRange.end.y) {
return VT100GridWindowedRangeEnd(range).x - VT100GridWindowedRangeStart(range).x;
} else {
int left = range.columnWindow.location;
int right = left + range.columnWindow.length;
int numFullLines = MAX(0, (range.coordRange.end.y - range.coordRange.start.y - 1));
return ((right - VT100GridWindowedRangeStart(range).x) + // Chars on first line
(VT100GridWindowedRangeEnd(range).x - left) + // Chars on second line
range.columnWindow.length * numFullLines); // Chars inbetween
}
}
NS_INLINE long long VT100GridCoordRangeLength(VT100GridCoordRange range, int gridWidth) {
return VT100GridCoordDistance(range.start, range.end, gridWidth);
}
// Returns the coord of the last char inside the run.
NS_INLINE VT100GridCoord VT100GridRunMax(VT100GridRun run, int width) {
VT100GridCoord coord = run.origin;
coord.y += (coord.x + run.length - 1) / width;
coord.x = (coord.x + run.length - 1) % width;
return coord;
}
// Returns the coord of the bottom-right cell that is in the rect. The rect must not be 0-dimensioned.
NS_INLINE VT100GridCoord VT100GridRectMax(VT100GridRect rect) {
VT100GridCoord coord = rect.origin;
coord.x += rect.size.width - 1;
coord.y += rect.size.height - 1;
return coord;
}
// Returns if the coord is within the rect.
NS_INLINE BOOL VT100GridCoordInRect(VT100GridCoord coord, VT100GridRect rect) {
return (coord.x >= rect.origin.x &&
coord.y >= rect.origin.y &&
coord.x < rect.origin.x + rect.size.width &&
coord.y < rect.origin.y + rect.size.height);
}
// Creates a run between two coords, not inclusive of end.
VT100GridRun VT100GridRunFromCoords(VT100GridCoord start,
VT100GridCoord end,
int width);