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
|
#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
{
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
{
int length=[self lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char buffer[length+1];
[self getCString:buffer maxLength:length+1 encoding:NSUTF8StringEncoding];
fwrite(buffer,length,1,fh);
}
#endif
@end
|