flywheel-compositor 0.1.2

A zero-flicker terminal compositor for Agentic CLIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Rope Buffer: Chunked storage for efficient large document handling.
//!
//! This module provides a rope-based data structure optimized for:
//! - Large documents (1M+ lines) with minimal allocations
//! - O(1) append and O(log n) random access
//! - Good cache locality through chunking

use crate::buffer::Cell;

/// Number of lines per chunk.
/// Tuned for a balance between overhead and cache utilization.
const CHUNK_SIZE: usize = 64;

/// A chunk of lines stored contiguously.
#[derive(Debug, Clone)]
struct Chunk {
    /// Lines in this chunk.
    lines: Vec<ChunkedLine>,
}

impl Chunk {
    /// Create a new empty chunk.
    fn new() -> Self {
        Self {
            lines: Vec::with_capacity(CHUNK_SIZE),
        }
    }

    /// Check if the chunk is full.
    const fn is_full(&self) -> bool {
        self.lines.len() >= CHUNK_SIZE
    }

    /// Get the number of lines in this chunk.
    const fn len(&self) -> usize {
        self.lines.len()
    }

    /// Check if the chunk is empty.
    #[allow(dead_code)]
    const fn is_empty(&self) -> bool {
        self.lines.is_empty()
    }

    /// Push a line to this chunk.
    fn push(&mut self, line: ChunkedLine) {
        self.lines.push(line);
    }

    /// Get a line by index within this chunk.
    fn get(&self, index: usize) -> Option<&ChunkedLine> {
        self.lines.get(index)
    }

    /// Get a mutable line by index within this chunk.
    fn get_mut(&mut self, index: usize) -> Option<&mut ChunkedLine> {
        self.lines.get_mut(index)
    }
}

/// A line stored in the rope buffer.
#[derive(Debug, Clone)]
pub struct ChunkedLine {
    /// The cells in this line.
    pub content: Vec<Cell>,
    /// Whether this line was soft-wrapped.
    pub wrapped: bool,
}

impl ChunkedLine {
    /// Create a new line with the given content.
    pub const fn new(content: Vec<Cell>, wrapped: bool) -> Self {
        Self { content, wrapped }
    }

    /// Create an empty line.
    pub const fn empty() -> Self {
        Self {
            content: Vec::new(),
            wrapped: false,
        }
    }

    /// Get the number of cells in this line.
    pub const fn len(&self) -> usize {
        self.content.len()
    }

    /// Check if the line is empty.
    pub const fn is_empty(&self) -> bool {
        self.content.is_empty()
    }
}

/// A rope-based line buffer for efficient large document storage.
///
/// Instead of storing each line as a separate allocation, lines are
/// grouped into chunks of `CHUNK_SIZE` lines. This reduces:
/// - Memory fragmentation
/// - Allocation overhead
/// - Cache misses during iteration
#[derive(Debug)]
pub struct RopeBuffer {
    /// Chunks of lines.
    chunks: Vec<Chunk>,
    /// Total number of lines.
    total_lines: usize,
    /// Maximum number of lines to retain (0 = unlimited).
    max_lines: usize,
    /// Current scroll offset from bottom.
    scroll_offset: usize,
}

impl RopeBuffer {
    /// Create a new rope buffer with the given maximum capacity.
    ///
    /// # Arguments
    ///
    /// * `max_lines` - Maximum lines to retain. 0 means unlimited.
    pub fn new(max_lines: usize) -> Self {
        let mut buffer = Self {
            chunks: Vec::new(),
            total_lines: 0,
            max_lines,
            scroll_offset: 0,
        };
        // Start with one empty line
        buffer.push_line(ChunkedLine::empty());
        buffer
    }

    /// Create an unbounded rope buffer.
    pub fn unbounded() -> Self {
        Self::new(0)
    }

    /// Get the total number of lines.
    pub const fn len(&self) -> usize {
        self.total_lines
    }

    /// Check if the buffer is empty.
    pub const fn is_empty(&self) -> bool {
        self.total_lines == 0
    }

    /// Get the number of chunks.
    pub const fn chunk_count(&self) -> usize {
        self.chunks.len()
    }

    /// Get a line by global index.
    pub fn get_line(&self, index: usize) -> Option<&ChunkedLine> {
        if index >= self.total_lines {
            return None;
        }
        let chunk_idx = index / CHUNK_SIZE;
        let line_idx = index % CHUNK_SIZE;
        self.chunks.get(chunk_idx)?.get(line_idx)
    }

    /// Get a mutable reference to a line by global index.
    pub fn get_line_mut(&mut self, index: usize) -> Option<&mut ChunkedLine> {
        if index >= self.total_lines {
            return None;
        }
        let chunk_idx = index / CHUNK_SIZE;
        let line_idx = index % CHUNK_SIZE;
        self.chunks.get_mut(chunk_idx)?.get_mut(line_idx)
    }

    /// Get the current (last) line.
    pub fn current_line(&self) -> Option<&ChunkedLine> {
        if self.total_lines == 0 {
            return None;
        }
        self.get_line(self.total_lines - 1)
    }

    /// Get a mutable reference to the current (last) line.
    pub fn current_line_mut(&mut self) -> Option<&mut ChunkedLine> {
        if self.total_lines == 0 {
            return None;
        }
        let idx = self.total_lines - 1;
        self.get_line_mut(idx)
    }

    /// Push a new line to the buffer.
    pub fn push_line(&mut self, line: ChunkedLine) {
        // Check if we need a new chunk
        if self.chunks.is_empty() || self.chunks.last().is_none_or(Chunk::is_full) {
            self.chunks.push(Chunk::new());
        }

        // Push to the last chunk
        if let Some(chunk) = self.chunks.last_mut() {
            chunk.push(line);
            self.total_lines += 1;
        }

        // Enforce max_lines if set
        if self.max_lines > 0 && self.total_lines > self.max_lines {
            self.trim_front();
        }
    }

    /// Add a new empty line.
    pub fn newline(&mut self) {
        self.push_line(ChunkedLine::empty());
    }

    /// Append cells to the current line.
    pub fn append(&mut self, cells: impl Iterator<Item = Cell>) {
        if let Some(line) = self.current_line_mut() {
            line.content.extend(cells);
        }
    }

    /// Clear all content.
    pub fn clear(&mut self) {
        self.chunks.clear();
        self.total_lines = 0;
        self.scroll_offset = 0;
        self.push_line(ChunkedLine::empty());
    }

    /// Get the current scroll offset.
    pub const fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Scroll up by the given number of lines.
    pub fn scroll_up(&mut self, lines: usize) {
        let max_offset = self.total_lines.saturating_sub(1);
        self.scroll_offset = (self.scroll_offset + lines).min(max_offset);
    }

    /// Scroll down by the given number of lines.
    pub const fn scroll_down(&mut self, lines: usize) {
        self.scroll_offset = self.scroll_offset.saturating_sub(lines);
    }

    /// Scroll to the bottom (most recent content).
    pub const fn scroll_to_bottom(&mut self) {
        self.scroll_offset = 0;
    }

    /// Iterate over lines in the visible range.
    ///
    /// Returns an iterator over (`line_index`, &`ChunkedLine`) for lines
    /// that should be visible given the viewport height.
    pub fn visible_lines(&self, viewport_height: usize) -> impl Iterator<Item = (usize, &ChunkedLine)> {
        let end = self.total_lines.saturating_sub(self.scroll_offset);
        let start = end.saturating_sub(viewport_height);
        
        (start..end).filter_map(move |i| {
            self.get_line(i).map(|line| (i, line))
        })
    }

    /// Trim lines from the front to stay within `max_lines`.
    fn trim_front(&mut self) {
        while self.total_lines > self.max_lines && !self.chunks.is_empty() {
            // Remove the first chunk
            let removed_chunk = self.chunks.remove(0);
            self.total_lines -= removed_chunk.len();
            
            // Adjust scroll offset
            if self.scroll_offset > removed_chunk.len() {
                self.scroll_offset -= removed_chunk.len();
            } else {
                self.scroll_offset = 0;
            }
        }
    }

    /// Get memory usage statistics.
    pub fn memory_stats(&self) -> RopeMemoryStats {
        let mut total_cells = 0;
        for chunk in &self.chunks {
            for line in &chunk.lines {
                total_cells += line.content.len();
            }
        }

        RopeMemoryStats {
            chunks: self.chunks.len(),
            lines: self.total_lines,
            cells: total_cells,
            bytes_estimated: self.chunks.len() * std::mem::size_of::<Chunk>()
                + self.total_lines * std::mem::size_of::<ChunkedLine>()
                + total_cells * std::mem::size_of::<Cell>(),
        }
    }
}

/// Memory usage statistics for a rope buffer.
#[derive(Debug, Clone, Copy)]
pub struct RopeMemoryStats {
    /// Number of chunks.
    pub chunks: usize,
    /// Number of lines.
    pub lines: usize,
    /// Number of cells.
    pub cells: usize,
    /// Estimated memory usage in bytes.
    pub bytes_estimated: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rope_buffer_basic() {
        let mut buffer = RopeBuffer::new(1000);
        
        assert_eq!(buffer.len(), 1); // Starts with empty line
        
        buffer.append([Cell::new('H'), Cell::new('i')].into_iter());
        assert_eq!(buffer.current_line().unwrap().len(), 2);
        
        buffer.newline();
        assert_eq!(buffer.len(), 2);
    }

    #[test]
    fn test_rope_buffer_chunks() {
        let mut buffer = RopeBuffer::unbounded();
        
        // Add enough lines to create multiple chunks
        for i in 0..200 {
            buffer.newline();
            buffer.append([Cell::new(char::from_u32(('a' as u32) + (i % 26)).unwrap())].into_iter());
        }
        
        // Should have multiple chunks
        assert!(buffer.chunk_count() > 1);
        assert_eq!(buffer.len(), 201); // 1 initial + 200 new
    }

    #[test]
    fn test_rope_buffer_max_lines() {
        let mut buffer = RopeBuffer::new(100);
        
        for _ in 0..200 {
            buffer.newline();
        }
        
        // Should be trimmed to around max_lines
        // Note: trimming is chunk-based, so might be slightly over
        assert!(buffer.len() <= 100 + CHUNK_SIZE);
    }

    #[test]
    fn test_rope_buffer_scroll() {
        let mut buffer = RopeBuffer::new(1000);
        
        for _ in 0..50 {
            buffer.newline();
        }
        
        assert_eq!(buffer.scroll_offset(), 0);
        
        buffer.scroll_up(10);
        assert_eq!(buffer.scroll_offset(), 10);
        
        buffer.scroll_down(5);
        assert_eq!(buffer.scroll_offset(), 5);
        
        buffer.scroll_to_bottom();
        assert_eq!(buffer.scroll_offset(), 0);
    }

    #[test]
    fn test_rope_buffer_visible_lines() {
        let mut buffer = RopeBuffer::new(1000);
        
        for i in 0..20 {
            buffer.append([Cell::new(char::from_u32('a' as u32 + i).unwrap())].into_iter());
            buffer.newline();
        }
        
        let visible: Vec<_> = buffer.visible_lines(10).collect();
        assert_eq!(visible.len(), 10);
    }

    #[test]
    fn test_rope_buffer_memory_stats() {
        let mut buffer = RopeBuffer::new(1000);
        
        for _ in 0..100 {
            buffer.append([Cell::new('x'); 80].into_iter());
            buffer.newline();
        }
        
        let stats = buffer.memory_stats();
        assert_eq!(stats.lines, 101);
        assert_eq!(stats.cells, 8000);
        assert!(stats.bytes_estimated > 0);
    }
}