@@ -20,6 +20,7 @@ public final class Buffer {
2020 private var _lines : CircularBufferLineList
2121 var xDisp , _yDisp , xBase : Int
2222 private var _x , _y , _yBase : Int
23+ private var _linesWithImagesCount : Int = 0
2324
2425 // this keeps incrementing even as we run out of space in _lines and trim out
2526 // old lines.
@@ -200,6 +201,54 @@ public final class Buffer {
200201 var lines : CircularBufferLineList {
201202 get { return _lines }
202203 }
204+
205+ /// Returns true if any lines in this buffer have images attached
206+ public var hasAnyImages : Bool {
207+ return _linesWithImagesCount > 0
208+ }
209+
210+ /// Attaches an image to the line at the given index, tracking the count of lines with images
211+ func attachImage( _ image: TerminalImage , toLineAt index: Int ) {
212+ let line = lines [ index]
213+ let hadImages = line. images != nil
214+ line. attach ( image: image)
215+ if !hadImages {
216+ _linesWithImagesCount += 1
217+ }
218+ }
219+
220+ /// Clears images from the line at the given index, tracking the count of lines with images
221+ func clearImagesFromLine( at index: Int ) {
222+ let line = lines [ index]
223+ if line. images != nil {
224+ _linesWithImagesCount -= 1
225+ line. images = nil
226+ }
227+ }
228+
229+ /// Recalculates the count of lines with images (used after reflow operations)
230+ func recalculateLinesWithImagesCount( ) {
231+ var count = 0
232+ for i in 0 ..< lines. count {
233+ if lines [ i] . images != nil {
234+ count += 1
235+ }
236+ }
237+ _linesWithImagesCount = count
238+ }
239+
240+ private func setupLinesCallbacks( ) {
241+ _lines. onLineRecycled = { [ weak self] hadImages in
242+ if hadImages {
243+ self ? . _linesWithImagesCount -= 1
244+ }
245+ }
246+ _lines. onLinePushed = { [ weak self] hasImages in
247+ if hasImages {
248+ self ? . _linesWithImagesCount += 1
249+ }
250+ }
251+ }
203252
204253 private var curAttr : Attribute = Attribute . empty
205254 private var insertMode : Bool = false
@@ -242,6 +291,7 @@ public final class Buffer {
242291 let len = hasScrollback ? ( scrollback ?? 0 ) + rows : rows
243292 _lines = CircularBufferLineList ( maxLength: len)
244293 _lines. makeEmpty = { [ unowned self] line in getBlankLine ( attribute: CharData . defaultAttr, isWrapped: false ) }
294+ setupLinesCallbacks ( )
245295 setupTabStops ( tabStopWidth: tabStopWidth)
246296 }
247297
@@ -308,6 +358,8 @@ public final class Buffer {
308358
309359 _lines = CircularBufferLineList ( maxLength: getCorrectBufferLength ( rows) )
310360 _lines. makeEmpty = { [ unowned self] line in getBlankLine ( attribute: CharData . defaultAttr, isWrapped: false ) }
361+ setupLinesCallbacks ( )
362+ _linesWithImagesCount = 0
311363 scrollTop = 0
312364 scrollBottom = rows - 1
313365
@@ -1036,6 +1088,7 @@ public final class Buffer {
10361088 } else {
10371089 reflowNarrower ( cols, rows, newCols, newRows)
10381090 }
1091+ recalculateLinesWithImagesCount ( )
10391092 }
10401093
10411094 static var n = 0
0 commit comments