Question

I've a NSTextView in the application window that shows a log of incoming data of a serial port. I append text to the log as it arrives to the application:

NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
NSTextStorage *textStorage = [SerialOutput textStorage];
[textStorage beginEditing];
[textStorage appendAttributedString:attrString];
[textStorage endEditing];

I want to limit the text, for example, to 1000 lines, in order to not collapse the application because it will run indefinitely.

Now I've a provisional solution, based on an NSTimer that clears the log every week and it works but I prefer to implement a clever method, just limiting the text size and create a circular log.

Any idea ? Maybe using method insertAttributedString ?

Regards, Joan

Was it helpful?

Solution

Finally I found the way, when I append text to the NSTextStorage, I just control if the length surpasses a threshold value and I clean some space of the log beginning:

// updates the textarea for incoming text by appending text
- (void)appendToIncomingText: (id) text {
  // add the text to the textarea
  NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
  NSTextStorage *textStorage = [SerialOutput textStorage];
  [textStorage beginEditing];
  [textStorage appendAttributedString:attrString];
      //Max. size of TextArea: LOG_SIZE characters
      if ([textStorage length] > LOG_SIZE){
          [textStorage deleteCharactersInRange:NSMakeRange(0, [attrString length])];
      }
  [textStorage endEditing];

  // scroll to the bottom
  NSRange myRange;
  myRange.length = 1;
  myRange.location = [textStorage length];
  NS[SerialOutput scrollRangeToVisible:myRange];
}

It works as a circular log, as I wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top