ansiq-surface 0.1.0

Terminal session lifecycle, viewport policy, and inline history management for Ansiq.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::io::{self, Stdout, Write};

use crate::{TerminalCapabilities, detect_terminal_capabilities};
use ansiq_core::HistoryEntry;
use ansiq_render::render_history_entries;
use crossterm::{
    cursor, execute,
    terminal::{self, ClearType, ScrollUp},
};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InlineReservePlan {
    pub origin_y: u16,
    pub scroll_up: u16,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Viewport {
    pub width: u16,
    pub height: u16,
    pub origin_y: u16,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ViewportPolicy {
    PreserveVisible,
    ReservePreferred(u16),
    ReserveFitContent { min: u16, max: u16 },
}

impl ViewportPolicy {
    pub fn requested_height(self, current_height: u16, content_height: u16) -> Option<u16> {
        match self {
            Self::PreserveVisible => None,
            Self::ReservePreferred(_) if content_height > current_height => Some(content_height),
            Self::ReservePreferred(_) => None,
            Self::ReserveFitContent { min, max } => {
                let target = content_height.clamp(min.max(1), max.max(min.max(1)));
                (target != current_height).then_some(target)
            }
        }
    }

    pub fn resolve(
        self,
        size: (u16, u16),
        cursor_y: u16,
        capabilities: TerminalCapabilities,
    ) -> Viewport {
        let (width, height) = normalize_terminal_size(size);
        let cursor_y = cursor_y.min(height.saturating_sub(1));

        match self {
            Self::PreserveVisible => Viewport {
                width,
                height: height.saturating_sub(cursor_y).max(1),
                origin_y: cursor_y,
            },
            Self::ReservePreferred(preferred_height) if capabilities.supports_inline_reserve => {
                let plan = inline_reserve_plan(height, cursor_y, preferred_height);
                Viewport {
                    width,
                    height: preferred_height.clamp(1, height),
                    origin_y: plan.origin_y,
                }
            }
            Self::ReservePreferred(_) => Viewport {
                width,
                height: height.saturating_sub(cursor_y).max(1),
                origin_y: cursor_y,
            },
            Self::ReserveFitContent { min, .. } if capabilities.supports_inline_reserve => {
                let plan = inline_reserve_plan(height, cursor_y, min.max(1));
                Viewport {
                    width,
                    height: min.clamp(1, height),
                    origin_y: plan.origin_y,
                }
            }
            Self::ReserveFitContent { .. } => Viewport {
                width,
                height: height.saturating_sub(cursor_y).max(1),
                origin_y: cursor_y,
            },
        }
    }
}

pub fn initial_viewport_plan(
    policy: ViewportPolicy,
    size: (u16, u16),
    cursor_y: u16,
    capabilities: TerminalCapabilities,
) -> (Viewport, Option<InlineReservePlan>) {
    let viewport = policy.resolve(size, cursor_y, capabilities);
    let reserve_plan = match policy {
        ViewportPolicy::ReservePreferred(preferred_height)
            if capabilities.supports_inline_reserve =>
        {
            let (_, terminal_height) = normalize_terminal_size(size);
            Some(inline_reserve_plan(
                terminal_height,
                cursor_y,
                preferred_height,
            ))
        }
        ViewportPolicy::ReserveFitContent { min, .. } if capabilities.supports_inline_reserve => {
            let (_, terminal_height) = normalize_terminal_size(size);
            Some(inline_reserve_plan(terminal_height, cursor_y, min.max(1)))
        }
        _ => None,
    };

    (viewport, reserve_plan)
}

pub fn reanchor_viewport_plan(
    policy: ViewportPolicy,
    size: (u16, u16),
    cursor_y: u16,
    current: Viewport,
    capabilities: TerminalCapabilities,
) -> (Viewport, Option<InlineReservePlan>) {
    let (width, height) = normalize_terminal_size(size);
    let cursor_y = cursor_y.min(height.saturating_sub(1));

    match policy {
        ViewportPolicy::PreserveVisible => (
            Viewport {
                width,
                height: height.saturating_sub(cursor_y).max(1),
                origin_y: cursor_y,
            },
            None,
        ),
        ViewportPolicy::ReservePreferred(preferred_height)
            if capabilities.supports_inline_reserve =>
        {
            let target_height = preferred_height.clamp(1, height);
            let plan = inline_reserve_plan(height, cursor_y, target_height);
            (
                Viewport {
                    width,
                    height: target_height,
                    origin_y: plan.origin_y,
                },
                Some(plan),
            )
        }
        ViewportPolicy::ReserveFitContent { min, max } if capabilities.supports_inline_reserve => {
            let target_height = current
                .height
                .clamp(min.max(1), max.max(min.max(1)))
                .clamp(1, height);
            let plan = inline_reserve_plan(height, cursor_y, target_height);
            (
                Viewport {
                    width,
                    height: target_height,
                    origin_y: plan.origin_y,
                },
                Some(plan),
            )
        }
        ViewportPolicy::ReservePreferred(_) | ViewportPolicy::ReserveFitContent { .. } => (
            Viewport {
                width,
                height: height.saturating_sub(cursor_y).max(1),
                origin_y: cursor_y,
            },
            None,
        ),
    }
}

pub fn resize_viewport_plan(
    policy: ViewportPolicy,
    size: (u16, u16),
    current: Viewport,
    capabilities: TerminalCapabilities,
) -> Viewport {
    let (width, height) = normalize_terminal_size(size);

    match policy {
        ViewportPolicy::PreserveVisible => Viewport {
            width,
            height: height.saturating_sub(current.origin_y).max(1),
            origin_y: current.origin_y.min(height.saturating_sub(1)),
        },
        ViewportPolicy::ReservePreferred(preferred_height)
            if capabilities.supports_inline_reserve =>
        {
            fit_viewport_height(
                Viewport { width, ..current },
                height,
                current.height.max(preferred_height),
            )
        }
        ViewportPolicy::ReserveFitContent { min, max } if capabilities.supports_inline_reserve => {
            let target_height = current.height.clamp(min.max(1), max.max(min.max(1)));
            fit_viewport_height(Viewport { width, ..current }, height, target_height)
        }
        ViewportPolicy::ReservePreferred(_) | ViewportPolicy::ReserveFitContent { .. } => {
            Viewport {
                width,
                height: height.saturating_sub(current.origin_y).max(1),
                origin_y: current.origin_y.min(height.saturating_sub(1)),
            }
        }
    }
}

pub fn fit_viewport_height(
    current: Viewport,
    terminal_height: u16,
    preferred_height: u16,
) -> Viewport {
    let terminal_height = terminal_height.max(1);
    let target_height = preferred_height.clamp(1, terminal_height);

    if target_height <= current.height {
        Viewport {
            width: current.width,
            height: target_height,
            origin_y: current
                .origin_y
                .min(terminal_height.saturating_sub(target_height)),
        }
    } else {
        let plan = inline_reserve_plan(terminal_height, current.origin_y, target_height);
        Viewport {
            width: current.width,
            height: target_height,
            origin_y: plan.origin_y,
        }
    }
}

pub fn cursor_y_after_history_entries(origin_y: u16, rendered_rows: u16) -> u16 {
    origin_y.saturating_add(rendered_rows)
}

pub fn safe_exit_row(exit_row: u16, size: (u16, u16)) -> u16 {
    let (_, height) = normalize_terminal_size(size);
    exit_row.min(height.saturating_sub(1))
}

pub struct TerminalSession {
    stdout: Stdout,
    capabilities: TerminalCapabilities,
    viewport: Viewport,
    exit_row: u16,
}

impl TerminalSession {
    pub fn enter(policy: ViewportPolicy) -> io::Result<Self> {
        let (_, cursor_y) = cursor::position()?;
        terminal::enable_raw_mode()?;

        let mut stdout = io::stdout();
        execute!(stdout, cursor::Hide)?;

        let capabilities = detect_terminal_capabilities();
        let size = terminal::size()?;
        let (viewport, reserve_plan) = initial_viewport_plan(policy, size, cursor_y, capabilities);
        if let Some(plan) = reserve_plan {
            // ReservePreferred is the app-like mode: we may scroll to make room,
            // but that policy now lives entirely in the surface layer.
            if plan.scroll_up > 0 {
                execute!(stdout, ScrollUp(plan.scroll_up))?;
            }
        }

        Ok(Self {
            stdout,
            capabilities,
            viewport,
            exit_row: viewport
                .origin_y
                .saturating_add(viewport.height.saturating_sub(1)),
        })
    }

    pub fn size(&self) -> io::Result<(u16, u16)> {
        terminal::size()
    }

    pub fn capabilities(&self) -> TerminalCapabilities {
        self.capabilities
    }

    pub fn origin_y(&self) -> u16 {
        self.viewport.origin_y
    }

    pub fn viewport(&self) -> Viewport {
        self.viewport
    }

    pub fn resize(&mut self, policy: ViewportPolicy, size: (u16, u16)) -> Viewport {
        self.viewport = resize_viewport_plan(policy, size, self.viewport, self.capabilities);
        self.exit_row = self
            .viewport
            .origin_y
            .saturating_add(self.viewport.height.saturating_sub(1));
        self.viewport
    }

    pub fn reserve_inline_space(&mut self, preferred_height: u16) -> io::Result<()> {
        let (_, terminal_height) = self.size()?;
        let old_bottom = self
            .viewport
            .origin_y
            .saturating_add(self.viewport.height.saturating_sub(1));
        let target_viewport = fit_viewport_height(self.viewport, terminal_height, preferred_height);
        let plan = inline_reserve_plan(
            terminal_height,
            self.viewport.origin_y,
            target_viewport.height,
        );
        if plan.scroll_up > 0 {
            execute!(self.stdout, ScrollUp(plan.scroll_up))?;
        }
        let clear_from = self.viewport.origin_y.min(target_viewport.origin_y);
        self.viewport = target_viewport;
        let new_bottom = self
            .viewport
            .origin_y
            .saturating_add(self.viewport.height.saturating_sub(1));
        if self.viewport.origin_y != clear_from || new_bottom < old_bottom {
            execute!(
                self.stdout,
                cursor::MoveTo(0, clear_from),
                terminal::Clear(ClearType::FromCursorDown)
            )?;
        }
        self.exit_row = self
            .viewport
            .origin_y
            .saturating_add(self.viewport.height.saturating_sub(1));
        Ok(())
    }

    pub fn commit_history_blocks(
        &mut self,
        blocks: Vec<HistoryEntry>,
        policy: ViewportPolicy,
    ) -> io::Result<Viewport> {
        if blocks.is_empty() {
            return Ok(self.viewport);
        }

        execute!(
            self.stdout,
            cursor::MoveTo(0, self.viewport.origin_y),
            terminal::Clear(ClearType::FromCursorDown)
        )?;

        let rendered_rows = render_history_entries(&mut self.stdout, &blocks, self.viewport.width)?;
        let cursor_y = cursor_y_after_history_entries(self.viewport.origin_y, rendered_rows);
        self.reanchor(policy, cursor_y)
    }

    pub fn reanchor(&mut self, policy: ViewportPolicy, cursor_y: u16) -> io::Result<Viewport> {
        let size = self.size()?;
        let (viewport, reserve_plan) =
            reanchor_viewport_plan(policy, size, cursor_y, self.viewport, self.capabilities);
        if let Some(plan) = reserve_plan {
            if self.capabilities.supports_inline_reserve && plan.scroll_up > 0 {
                execute!(self.stdout, ScrollUp(plan.scroll_up))?;
            }
        }
        self.viewport = viewport;
        self.exit_row = self
            .viewport
            .origin_y
            .saturating_add(self.viewport.height.saturating_sub(1));
        Ok(self.viewport)
    }

    pub fn set_exit_row(&mut self, row: u16) {
        self.exit_row = row;
    }

    pub fn write_ansi(&mut self, output: &str) -> io::Result<()> {
        self.stdout.write_all(output.as_bytes())?;
        self.stdout.flush()
    }
}

impl Drop for TerminalSession {
    fn drop(&mut self) {
        let exit_row = terminal::size()
            .map(|size| safe_exit_row(self.exit_row, size))
            .unwrap_or(self.exit_row);
        let _ = execute!(self.stdout, cursor::MoveTo(0, exit_row), cursor::Show);
        let _ = writeln!(self.stdout);
        let _ = self.stdout.flush();
        let _ = terminal::disable_raw_mode();
    }
}

pub type TerminalGuard = TerminalSession;

pub fn inline_reserve_plan(
    terminal_height: u16,
    cursor_y: u16,
    preferred_height: u16,
) -> InlineReservePlan {
    // Keep the viewport anchored to the launch cursor when possible. Only ask the
    // terminal to scroll when a preferred inline working height cannot fit below it.
    let terminal_height = terminal_height.max(1);
    let cursor_y = cursor_y.min(terminal_height.saturating_sub(1));
    let target_height = preferred_height.clamp(1, terminal_height);
    let remaining = terminal_height.saturating_sub(cursor_y);

    if remaining >= target_height {
        InlineReservePlan {
            origin_y: cursor_y,
            scroll_up: 0,
        }
    } else {
        let scroll_up = target_height - remaining;
        InlineReservePlan {
            origin_y: cursor_y.saturating_sub(scroll_up),
            scroll_up,
        }
    }
}

fn normalize_terminal_size((width, height): (u16, u16)) -> (u16, u16) {
    let width = if width == 0 { 80 } else { width };
    let height = if height == 0 { 24 } else { height };
    (width, height)
}