forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTermLocatedString.m
133 lines (111 loc) · 3.73 KB
/
iTermLocatedString.m
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
//
// iTermLocatedString.m
// iTerm2SharedARC
//
// Created by George Nachman on 5/16/20.
//
#import "iTermLocatedString.h"
#import "NSMutableAttributedString+iTerm.h"
#import "NSStringITerm.h"
@implementation iTermLocatedString {
NSMutableString *_string;
@protected
NSMutableArray<NSValue *> *_coords;
}
- (instancetype)init {
self = [super init];
if (self) {
_string = [NSMutableString string];
_coords = [NSMutableArray array];
}
return self;
}
- (void)appendString:(NSString *)string at:(VT100GridCoord)coord {
[_string appendString:string];
[self appendCoordsForString:string at:coord];
}
- (void)appendCoordsForString:(NSString *)string at:(VT100GridCoord)coord {
const NSInteger length = string.length;
NSValue *value = [NSValue valueWithGridCoord:coord];
for (NSInteger i = 0; i < length; i++) {
[_coords addObject:value];
}
}
- (void)erase {
_string = [NSMutableString string];
_coords = [NSMutableArray array];
}
- (NSInteger)length {
return _string.length;
}
- (void)dropFirst:(NSInteger)count {
const NSRange range = NSMakeRange(0, count);
[_string replaceCharactersInRange:range withString:@""];
// TODO: Remove leading low surrogate
[_coords removeObjectsInRange:range];
}
- (void)trimTrailingWhitespace {
const NSInteger lengthBeforeTrimming = _string.length;
[_string trimTrailingWhitespace];
[_coords removeObjectsInRange:NSMakeRange(_string.length,
lengthBeforeTrimming - _string.length)];
}
- (void)removeOcurrencesOfString:(NSString *)string {
NSArray *empty = @[];
[_string reverseEnumerateSubstringsEqualTo:string block:^(NSRange range) {
[_string replaceCharactersInRange:range withString:@""];
[_coords replaceObjectsInRange:range withObjectsFromArray:empty];
}];
}
@end
@implementation iTermLocatedAttributedString {
NSMutableAttributedString *_attributedString;
}
- (instancetype)init {
self = [super init];
if (self) {
_attributedString = [[NSMutableAttributedString alloc] init];
}
return self;
}
- (NSString *)string {
return _attributedString.string;
}
- (void)appendString:(NSString *)string
withAttributes:(NSDictionary *)attributes
at:(VT100GridCoord)coord {
[_attributedString iterm_appendString:string withAttributes:attributes];
[self appendCoordsForString:string at:coord];
}
- (void)appendAttributedString:(NSAttributedString *)attributedString
at:(VT100GridCoord)coord {
[_attributedString appendAttributedString:attributedString];
[self appendCoordsForString:attributedString.string at:coord];
}
- (void)erase {
[super erase];
_attributedString = [[NSMutableAttributedString alloc] init];
}
- (NSInteger)length {
return _attributedString.length;
}
- (void)dropFirst:(NSInteger)count {
const NSRange range = NSMakeRange(0, count);
[_attributedString replaceCharactersInRange:range withString:@""];
// TODO: Remove leading low surrogate
[_coords removeObjectsInRange:range];
}
- (void)trimTrailingWhitespace {
const NSInteger lengthBeforeTrimming = _attributedString.length;
[_attributedString trimTrailingWhitespace];
[_coords removeObjectsInRange:NSMakeRange(_attributedString.length,
lengthBeforeTrimming - _attributedString.length)];
}
- (void)removeOcurrencesOfString:(NSString *)string {
NSArray *empty = @[];
[_attributedString.string reverseEnumerateSubstringsEqualTo:string block:^(NSRange range) {
[_attributedString replaceCharactersInRange:range withString:@""];
[_coords replaceObjectsInRange:range withObjectsFromArray:empty];
}];
}
@end