-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathNSStringPrinting.m
128 lines (102 loc) · 2.87 KB
/
NSStringPrinting.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
/*
* NSStringPrinting.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "NSStringPrinting.h"
#ifdef __MINGW32__
#include <windows.h>
#endif
@implementation NSString (Printing)
-(void)print
{
[self printToFile:stdout];
}
-(NSString *)stringByEscapingControlCharacters
{
NSMutableString *res=[NSMutableString string];
int length=[self length];
for(int i=0;i<length;i++)
{
unichar c=[self characterAtIndex:i];
if(c<32) [res appendFormat:@"^%c",c+64];
else [res appendFormat:@"%C",c];
}
return res;
}
-(NSArray *)linesWrappedToWidth:(int)width
{
int length=[self length];
NSMutableArray *wrapped=[NSMutableArray array];
int linestartpos=0,lastspacepos=-1;
for(int i=0;i<length;i++)
{
unichar c=[self characterAtIndex:i];
if(c==' ') lastspacepos=i;
int linelength=i-linestartpos;
if(linelength>=width && lastspacepos!=-1)
{
[wrapped addObject:[self substringWithRange:NSMakeRange(linestartpos,lastspacepos-linestartpos)]];
linestartpos=lastspacepos+1;
lastspacepos=-1;
}
}
if(linestartpos<length)
[wrapped addObject:[self substringWithRange:NSMakeRange(linestartpos,length-linestartpos)]];
return wrapped;
}
#ifdef __MINGW32__
+(int)terminalWidth
{
return 79; // Gah, too lazy to fix the weird Windows printing behaviour.
}
-(void)printToFile:(FILE *)fh
{
if(!fh) return;
int length=[self length];
unichar buffer[length];
[self getCharacters:buffer range:NSMakeRange(0,length)];
int bufsize=WideCharToMultiByte(GetConsoleOutputCP(),0,buffer,length,NULL,0,NULL,NULL);
char mbuffer[bufsize];
WideCharToMultiByte(GetConsoleOutputCP(),0,buffer,length,mbuffer,bufsize,NULL,NULL);
fwrite(mbuffer,bufsize,1,fh);
}
#else
#include <sys/ioctl.h>
+(int)terminalWidth
{
#ifdef TIOCGSIZE
struct ttysize ts;
ioctl(0,TIOCGSIZE,&ts);
return ts.ts_cols;
#else
struct winsize ws;
ioctl(0,TIOCGWINSZ,&ws);
return ws.ws_col;
#endif
}
-(void)printToFile:(FILE *)fh
{
if(!fh) return;
int length=[self lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char buffer[length+1];
[self getCString:buffer maxLength:length+1 encoding:NSUTF8StringEncoding];
fwrite(buffer,length,1,fh);
}
#endif
@end