Search code examples
cocoalessnstask

Strange output from LESS when using with NSTask


I'm trying to use LESS with NSTask. It basically works but the output is sometimes quite strange. Example:

[31mParseError: Unrecognised input[39m[31m in [39m/Volumes/Macintosh HD/Users/x/Sites/y/css/style.less[90m on line 4, column 2:[39m
[90m3   background-color: purple;[39m
4   [7m[31m[1md[22md[39m[27m
[90m5 }[39m[0m[0m

Where does this [31m etc. come from?

My relevant code is:

NSTask *task = [[NSTask alloc] init];
task.launchPath = [bundle.resourcePath stringByAppendingPathComponent:@"less.js/bin/lessc"];
task.arguments = @[path,cssPath];

NSPipe *outputPipe = [NSPipe pipe];
[task setStandardError:outputPipe];

[task setTerminationHandler:^(NSTask *task) {
    NSString *s = [[NSString alloc] initWithData: [[outputPipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding];
    if(s.length > 0){
        [[NSAlert alertWithMessageText:@"LESS Compilation" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@",s] runModal];
    }
}];
[task launch];

Edit: I think now this has something to do with the coloring from the command line, hasn't it? So my question is: What's the best way to avoid them?


Solution

  • Those are ANSI color codes. You might want to look at whether or not there are arguments for the task that will not return color coded text.