segment.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'package:xterm/src/core/buffer/cell_offset.dart';
  2. import 'package:xterm/src/core/buffer/range.dart';
  3. class BufferSegment {
  4. /// The range that this segment belongs to.
  5. final BufferRange range;
  6. /// The line that this segment resides on.
  7. final int line;
  8. /// The start position of this segment. [null] means the start of the line.
  9. final int? start;
  10. /// The end position of this segment. [null] means the end of the line.
  11. /// Should be greater than or equal to [start].
  12. final int? end;
  13. const BufferSegment(this.range, this.line, this.start, this.end)
  14. : assert((start != null && end != null) ? start <= end : true);
  15. bool isWithin(CellOffset position) {
  16. if (position.y != line) {
  17. return false;
  18. }
  19. if (start != null && position.x < start!) {
  20. return false;
  21. }
  22. if (end != null && position.x > end!) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. @override
  28. String toString() {
  29. final start = this.start != null ? this.start.toString() : 'start';
  30. final end = this.end != null ? this.end.toString() : 'end';
  31. return 'Segment($line, $start -> $end)';
  32. }
  33. @override
  34. int get hashCode =>
  35. range.hashCode ^ line.hashCode ^ start.hashCode ^ end.hashCode;
  36. @override
  37. bool operator ==(Object other) =>
  38. identical(this, other) ||
  39. other is BufferSegment &&
  40. runtimeType == other.runtimeType &&
  41. range == other.range &&
  42. line == other.line &&
  43. start == other.start &&
  44. end == other.end;
  45. }