forked from ali-rantakari/icalBuddy
-
Notifications
You must be signed in to change notification settings - Fork 13
/
HGCLIUtils.m
203 lines (158 loc) · 6.14 KB
/
HGCLIUtils.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
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
// CLI app utils
//
// http://hasseg.org/
//
/*
The MIT License
Copyright (c) 2010 Ali Rantakari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "HGCLIUtils.h"
// the string encoding to use for output
NSStringEncoding outputStrEncoding = NSUTF8StringEncoding; // default
BOOL debugPrintEnabled = NO;
// helper methods for printing to stdout and stderr
// from: http://www.sveinbjorn.org/objectivec_stdout
// (modified to use non-deprecated version of writeToFile:...
// and allow for using the "string format" syntax)
void Print(NSString *aStr)
{
[aStr writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:NULL];
}
// other Printf functions call this, and you call them
void RealPrintf(NSString *aStr, NSString *aFile, va_list opts)
{
NSString *str = [
[[NSString alloc]
initWithFormat:aStr
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
arguments:opts
] autorelease
];
[str writeToFile:aFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
}
void Printf(NSString *aStr, ...)
{
va_list argList;
va_start(argList, aStr);
RealPrintf(aStr, @"/dev/stdout", argList);
va_end(argList);
}
void PrintfErr(NSString *aStr, ...)
{
va_list argList;
va_start(argList, aStr);
RealPrintf(aStr, @"/dev/stderr", argList);
va_end(argList);
}
void DebugPrintf(NSString *aStr, ...)
{
if (!debugPrintEnabled)
return;
NSString *str = strConcat(@"icalBuddy: ", aStr, nil);
va_list argList;
va_start(argList, aStr);
RealPrintf(str, @"/dev/stderr", argList);
va_end(argList);
}
#define UNICHAR_NEWLINE 10
#define UNICHAR_TAB 9
#define UNICHAR_SPACE 32
#define TAB_STOP_LENGTH 4
void wordWrapMutableAttrStr(NSMutableAttributedString *mutableAttrStr, NSUInteger width)
{
// replace tabs with spaces to avoid problems with different programs
// (that would display our output) using different tab stop lengths:
replaceInMutableAttrStr(mutableAttrStr, @"\t", ATTR_STR(WHITESPACE(TAB_STOP_LENGTH)));
NSString *str = [[mutableAttrStr string] copy];
NSAttributedString *newlineAttrStr = ATTR_STR(@"\n");
// characters we'll consider as indentation:
NSCharacterSet *indentChars = [NSCharacterSet characterSetWithCharactersInString:@" •"];
// find all input string indices where we want to
// wrap the line
NSUInteger strLength = [str length];
NSUInteger strIndex = 0;
NSUInteger currentLineLength = 0;
NSUInteger lastWhitespaceIndex = 0;
unichar currentUnichar = 0;
BOOL lastCharWasWhitespace = NO;
BOOL lastCharWasIndentation = NO;
NSUInteger numAddedChars = 0;
NSUInteger currentLineIndentAmount = 0;
while(strIndex < strLength)
{
if (width <= currentLineLength)
{
// insert newline at the wrap index, eating one whitespace
// *if* we're wrapping at a whitespace (i.e. don't eat characters
// if we've been forced to wrap in the middle of a word)
NSUInteger indexToWrapAt = ((0 < lastWhitespaceIndex) ? lastWhitespaceIndex : strIndex) + numAddedChars;
NSUInteger lengthToReplace = ((lastWhitespaceIndex != 0)?1:0);
NSRange replaceRange = NSMakeRange(indexToWrapAt, lengthToReplace);
NSAttributedString *replaceStr = nil;
if (currentLineIndentAmount == 0)
replaceStr = newlineAttrStr;
else
replaceStr = ATTR_STR(strConcat(@"\n", WHITESPACE(currentLineIndentAmount), nil));
[mutableAttrStr
replaceCharactersInRange:replaceRange
withAttributedString:replaceStr
];
numAddedChars += [replaceStr length] - lengthToReplace;
lastWhitespaceIndex = 0;
currentLineLength = (strIndex-(indexToWrapAt-numAddedChars));
}
else
{
currentUnichar = [str characterAtIndex:strIndex];
if ((lastCharWasIndentation || currentLineLength == 0) &&
[indentChars characterIsMember:currentUnichar]
)
{
lastCharWasIndentation = YES;
currentLineIndentAmount++;
}
else
lastCharWasIndentation = NO;
if (currentUnichar == UNICHAR_NEWLINE)
{
lastWhitespaceIndex = 0;
currentLineLength = 0;
currentLineIndentAmount = 0;
}
// we want to wrap at the beginning of the last
// whitespace run of the current line, excluding the
// beginning of the line (doesn't make sense to wrap
// there):
else if (!lastCharWasWhitespace &&
0 < currentLineLength &&
currentUnichar == UNICHAR_SPACE
)
{
lastWhitespaceIndex = strIndex;
currentLineLength++;
}
else
{
currentLineLength++;
}
lastCharWasWhitespace = (currentUnichar == UNICHAR_SPACE);
}
strIndex++;
}
[str release];
}