public ColorBuffer truncate ( int len ) { ColorBuffer cbuff = new ColorBuffer ( useColor ) ; ColorAttr lastAttr = null ; for ( Iterator < Object > i = parts . iterator ( ) ; cbuff . getVisibleLength ( ) < len && i . hasNext ( ) ; ) { Object next = i . next ( ) ; if ( next instanceof ColorAttr ) { lastAttr = ( ColorAttr ) next ; cbuff . append ( ( ColorAttr ) next ) ; continue ; } String val = next . toString ( ) ; if ( cbuff . getVisibleLength ( ) + val . length ( ) > len ) { int partLen = len - cbuff . getVisibleLength ( ) ; val = val . substring ( 0 , partLen ) ; } cbuff . append ( val ) ; } // close off the buffer with a normal tag if ( lastAttr != null && lastAttr != ColorAttr . NORMAL ) { cbuff . append ( ColorAttr . NORMAL ) ; } return cbuff ; }
Truncate the ColorBuffer to the specified length and return the new ColorBuffer . Any open color tags will be closed .