limitString method

String limitString(
  1. String display, {
  2. int width = 40,
})

Limits the display string's length to width by removing the centre components of the string and replacing them with '...'

Example: var long = 'http://www.noojee.com.au/some/long/url'; print(limitString(long, width: 20))

http://...ong/url

Implementation

String limitString(String display, {int width = 40}) {
  if (display.length <= width) {
    return display;
  }
  final elipses = width <= 2 ? 1 : 3;
  final partLength = (width - elipses) ~/ 2;
  // ignore: lines_longer_than_80_chars
  return '${display.substring(0, partLength)}${'.' * elipses}${display.substring(display.length - partLength)}';
}