pub struct Buffer {
pub width: u16,
pub height: u16,
/* private fields */
}Fields§
§width: u16§height: u16Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn blank(size: Size) -> Self
pub fn blank(size: Size) -> Self
A buffer of blank (opaque space) cells — used for the screen root.
Sourcepub fn transparent(size: Size) -> Self
pub fn transparent(size: Size) -> Self
A buffer of transparent cells — used for overlay surfaces.
pub fn filled(size: Size, cell: Cell) -> Self
pub fn size(&self) -> Size
pub fn area(&self) -> Rect
pub fn get(&self, x: u16, y: u16) -> Option<&Cell>
pub fn cell_mut(&mut self, x: u16, y: u16) -> Option<&mut Cell>
pub fn set(&mut self, x: u16, y: u16, ch: char, style: Style)
Sourcepub fn set_str(&mut self, x: u16, y: u16, s: &str, style: Style) -> u16
pub fn set_str(&mut self, x: u16, y: u16, s: &str, style: Style) -> u16
Write s starting at (x, y), clipping at the buffer edge. Returns the
column just past the last glyph written.
Examples found in repository?
77fn main() -> std::io::Result<()> {
78 let mut term = Terminal::new()?;
79 term.mouse_mode(MouseMode::Motion)?;
80 let start = Instant::now();
81
82 let faint = Style::new().fg(rgb(0x63, 0x63, 0x63));
83 let dim = Style::new().fg(rgb(0x9c, 0x9c, 0x9c));
84
85 let pal = Palette {
86 panel: Style::new().bg(rgb(0x22, 0x22, 0x22)),
87 border: Style::new().fg(rgb(0x6a, 0x6a, 0x6a)),
88 normal: Style::new().fg(rgb(0xb8, 0xb8, 0xb8)),
89 accent: Style::new().fg(rgb(0xe6, 0xe6, 0xe6)),
90 selected: Style::new()
91 .fg(rgb(0x12, 0x12, 0x12))
92 .bg(rgb(0xd8, 0xd8, 0xd8))
93 .bold(),
94 track: Style::new().fg(rgb(0x30, 0x30, 0x30)),
95 thumb: Style::new().fg(rgb(0x86, 0x86, 0x86)),
96 };
97 let ctx_pal = Palette {
98 normal: Style::new()
99 .fg(rgb(0xc4, 0xc4, 0xc4))
100 .bg(rgb(0x2a, 0x2a, 0x2a)),
101 panel: Style::new().bg(rgb(0x2a, 0x2a, 0x2a)),
102 ..pal
103 };
104
105 let mut menu = List::new(
106 [
107 "New", "Open", "Save", "Copy", "Paste", "Find", "Settings", "Quit",
108 ]
109 .iter()
110 .map(|s| s.to_string())
111 .collect(),
112 );
113 let mut log = ScrollView::new(
114 (1..=40)
115 .map(|i| {
116 Line::from(vec![
117 Span::styled(format!(" {i:>2} "), faint),
118 Span::styled(format!("#{i:03} "), dim),
119 Span::styled("ok".to_string(), faint),
120 ])
121 })
122 .collect(),
123 );
124 log.scrollbar.style = ScrollbarStyle {
125 track_glyph: '░',
126 thumb_glyph: '▐',
127 track: Style::new()
128 .fg(rgb(0x40, 0x40, 0x40))
129 .bg(rgb(0x22, 0x22, 0x22)),
130 thumb: Style::new()
131 .fg(rgb(0x72, 0x72, 0x72))
132 .bg(rgb(0x22, 0x22, 0x22)),
133 thumb_active: Style::new()
134 .fg(rgb(0xcc, 0xcc, 0xcc))
135 .bg(rgb(0x22, 0x22, 0x22))
136 .bold(),
137 ..Default::default()
138 };
139 let mut ctx = Menu::new(
140 ["Copy", "Paste", "Delete"]
141 .iter()
142 .map(|s| s.to_string())
143 .collect(),
144 );
145
146 let mut code = CodeBlock::new(SAMPLE_RUST, Lang::Rust);
147 code.scrollbar.style = ScrollbarStyle {
148 track_glyph: '▕',
149 thumb_glyph: '█',
150 width: 1,
151 track: Style::new()
152 .fg(rgb(0x35, 0x35, 0x35))
153 .bg(rgb(0x22, 0x22, 0x22)),
154 thumb: Style::new()
155 .fg(rgb(0x88, 0x88, 0x88))
156 .bg(rgb(0x22, 0x22, 0x22)),
157 thumb_active: Style::new()
158 .fg(rgb(0xee, 0xee, 0xee))
159 .bg(rgb(0x22, 0x22, 0x22)),
160 };
161 let mut diff = DiffView::new(SAMPLE_RUST_OLD, SAMPLE_RUST);
162 diff.scrollbar.style = code.scrollbar.style;
163
164 let mut mode_tabs = Tabs::new(["Code", "Diff"].iter().map(|s| s.to_string()).collect());
165 let mut editor_mode = EditorMode::Code;
166
167 let mut win_menu = Window::new("menu", 2, 3, 28, 10);
168 let mut win_log = Window::new("log — drag ▐", 32, 3, 40, 10);
169 let mut win_spin = Window::new("spin", 74, 3, 18, 10);
170 let mut win_editor = Window::new("editor", 2, 14, 72, 18);
171 win_menu.fill = pal.panel;
172 win_log.fill = pal.panel;
173 win_spin.fill = pal.panel;
174 win_editor.fill = pal.panel;
175 win_editor.min_w = 28;
176 win_editor.min_h = 8;
177
178 let mut zorder = vec![WinId::Spin, WinId::Menu, WinId::Log, WinId::Editor];
179 let mut focused = WinId::Editor;
180
181 let mut input = TextInput::with_placeholder("type a toast message…");
182 let mut toasts = Toasts::default();
183
184 let mut ctx_at: Option<(u16, u16)> = None;
185 let mut input_focus = false;
186 let mut status = String::from("ready");
187 let mut quit = false;
188 let mut code_fill = false;
189
190 while !quit {
191 toasts.tick();
192 let phase = (start.elapsed().as_millis() / 90) as usize;
193 let size = term.size();
194
195 let footer_rows = 2u16;
196 let input_h = 3u16;
197 let input_y = size.height.saturating_sub(footer_rows + input_h);
198 let input_area = Rect::new(2, input_y, size.width.saturating_sub(4).min(52), input_h);
199 let workspace = Rect::new(0, 2, size.width, input_y.saturating_sub(2));
200
201 for w in [&mut win_menu, &mut win_log, &mut win_spin, &mut win_editor] {
202 w.clamp_to(workspace);
203 }
204
205 win_editor.title = match editor_mode {
206 EditorMode::Code => "editor · Code".into(),
207 EditorMode::Diff => "editor · Diff".into(),
208 };
209 mode_tabs.selected = match editor_mode {
210 EditorMode::Code => 0,
211 EditorMode::Diff => 1,
212 };
213
214 term.draw(|f| {
215 let bright = Style::new().fg(rgb(0xe6, 0xe6, 0xe6)).bold();
216 let buf = f.buffer();
217
218 buf.set_line(
219 2,
220 0,
221 &Line::from(shimmer("comb — drag windows · Code / Diff", phase)),
222 size.width.saturating_sub(4),
223 );
224 buf.set_str(
225 2,
226 1,
227 "title-bar move · edge resize · Code/Diff toggle · b fill",
228 faint,
229 );
230
231 for id in &zorder {
232 let focused_win = *id == focused;
233 match id {
234 WinId::Menu => {
235 win_menu.render_chrome(buf, &pal, focused_win);
236 menu.render(buf, win_menu.inner(), &pal);
237 }
238 WinId::Log => {
239 win_log.render_chrome(buf, &pal, focused_win);
240 log.render(buf, win_log.inner(), &pal);
241 }
242 WinId::Spin => {
243 win_spin.render_chrome(buf, &pal, focused_win);
244 let inner = win_spin.inner();
245 let on = |st: Style| st.patch(pal.panel);
246 for (i, sp) in SPINNERS.iter().take(8).enumerate() {
247 let y = inner.y + i as u16;
248 if y >= inner.bottom() {
249 break;
250 }
251 buf.set_str(inner.x, y, sp.frame(phase), on(bright));
252 buf.set_str(inner.x + 2, y, sp.name, on(dim));
253 }
254 }
255 WinId::Editor => {
256 win_editor.render_chrome(buf, &pal, focused_win);
257 let inner = win_editor.inner();
258 if inner.height < 3 {
259 continue;
260 }
261 let mode_bar = Rect::new(inner.x, inner.y, inner.width, 2);
262 mode_tabs.render(buf, mode_bar, &pal);
263 let body = Rect::new(
264 inner.x,
265 inner.y + 2,
266 inner.width,
267 inner.height.saturating_sub(2),
268 );
269 // Always opaque inside a stacked window; `b` toggles a
270 // slightly lifted panel vs the window chrome fill.
271 let bg = Some(if code_fill {
272 Style::new().bg(rgb(0x28, 0x28, 0x28))
273 } else {
274 win_editor.fill
275 });
276 match editor_mode {
277 EditorMode::Code => code.render(buf, body, bg),
278 EditorMode::Diff => diff.render(buf, body, bg),
279 }
280 }
281 }
282 }
283
284 let spark_x = input_area.right().saturating_add(2);
285 if spark_x + 20 < size.width {
286 let t = start.elapsed().as_secs_f32();
287 let values: Vec<f32> = (0..16)
288 .map(|i| {
289 let x = i as f32 / 4.0 + t;
290 (x.sin() * 0.4 + 0.5).clamp(0.05, 0.95)
291 })
292 .collect();
293 let mut line = Line::new();
294 for s in sparkline(&values, 16, Style::new().fg(rgb(0xc8, 0xc8, 0xc8))) {
295 line.push(s);
296 }
297 buf.set_line(spark_x, input_y + 1, &line, 16);
298 }
299
300 let input_cursor = input.render(buf, input_area, &pal);
301
302 let mode = match editor_mode {
303 EditorMode::Code => "Code",
304 EditorMode::Diff => "Diff",
305 };
306 let fill = if code_fill { "fill" } else { "plain" };
307 buf.set_str(
308 2,
309 size.height.saturating_sub(2),
310 &format!(
311 "status: {status} · {mode} · bg:{fill} · editor {}×{}",
312 win_editor.width, win_editor.height
313 ),
314 dim,
315 );
316 let hint = if input_focus {
317 "input focused · enter toast · tab unfocus"
318 } else {
319 "drag title · resize edges · d Code/Diff · b fill · q quit"
320 };
321 buf.set_str(2, size.height.saturating_sub(1), hint, faint);
322
323 if let Some((cx, cy)) = ctx_at {
324 ctx.render(f, cx, cy, 100, &ctx_pal);
325 }
326 toasts.render(f, 150, &pal);
327 if input_focus {
328 if let Some((x, y)) = input_cursor {
329 f.set_cursor(x, y);
330 }
331 }
332 })?;
333
334 let mut first = true;
335 loop {
336 let ms = if first { 40 } else { 0 };
337 let Some(ev) = term.read_event(Duration::from_millis(ms))? else {
338 break;
339 };
340 first = false;
341
342 if input_focus {
343 match handle_input_key(&mut input, &mut toasts, &mut status, &ev) {
344 InputResult::Quit => {
345 quit = true;
346 break;
347 }
348 InputResult::Unfocus => input_focus = false,
349 InputResult::Stay => {}
350 }
351 continue;
352 }
353
354 match ev {
355 Event::Key(k) => match k.code {
356 KeyCode::Char('q') => quit = true,
357 KeyCode::Char('c') if k.mods.ctrl => quit = true,
358 KeyCode::Char('b') => {
359 code_fill = !code_fill;
360 status = if code_fill {
361 "code bg: panel fill".into()
362 } else {
363 "code bg: terminal (plain)".into()
364 };
365 }
366 KeyCode::Char('d') => {
367 editor_mode = match editor_mode {
368 EditorMode::Code => EditorMode::Diff,
369 EditorMode::Diff => EditorMode::Code,
370 };
371 status = format!("mode: {editor_mode:?}");
372 bring_front(&mut zorder, WinId::Editor);
373 focused = WinId::Editor;
374 }
375 KeyCode::Tab if k.mods.ctrl && k.mods.shift => {
376 // cycle focus backward
377 if let Some(pos) = zorder.iter().position(|w| *w == focused) {
378 let prev = if pos == 0 { zorder.len() - 1 } else { pos - 1 };
379 focused = zorder[prev];
380 bring_front(&mut zorder, focused);
381 }
382 }
383 KeyCode::Tab if k.mods.ctrl => {
384 if let Some(pos) = zorder.iter().position(|w| *w == focused) {
385 let next = (pos + 1) % zorder.len();
386 focused = zorder[next];
387 bring_front(&mut zorder, focused);
388 }
389 }
390 KeyCode::Tab => input_focus = true,
391 KeyCode::Esc => {
392 if ctx_at.is_some() {
393 ctx_at = None;
394 } else {
395 quit = true;
396 }
397 }
398 KeyCode::Up => {
399 if ctx_at.is_some() {
400 ctx.list.select_up();
401 } else {
402 menu.select_up();
403 }
404 }
405 KeyCode::Down => {
406 if ctx_at.is_some() {
407 ctx.list.select_down();
408 } else {
409 menu.select_down();
410 }
411 }
412 KeyCode::Enter => {
413 if ctx_at.is_some() {
414 let msg = ctx.list.selected_item().unwrap_or("").to_string();
415 status = format!("action: {msg}");
416 toasts.show(msg, Duration::from_secs(2));
417 ctx_at = None;
418 } else {
419 let msg = menu.selected_item().unwrap_or("").to_string();
420 status = format!("ran: {msg}");
421 toasts.show(format!("ran {msg}"), Duration::from_secs(2));
422 }
423 }
424 _ => {}
425 },
426 Event::Mouse(m) => {
427 // Active window drag/resize first.
428 let dragging = win_menu.is_dragging()
429 || win_log.is_dragging()
430 || win_spin.is_dragging()
431 || win_editor.is_dragging();
432 if dragging {
433 for id in zorder.iter().rev() {
434 let win = match id {
435 WinId::Menu => &mut win_menu,
436 WinId::Log => &mut win_log,
437 WinId::Spin => &mut win_spin,
438 WinId::Editor => &mut win_editor,
439 };
440 if win.is_dragging()
441 && win.handle_mouse(m.kind, m.col, m.row, workspace)
442 {
443 break;
444 }
445 }
446 if matches!(m.kind, MouseKind::Up) {
447 win_menu.end_drag();
448 win_log.end_drag();
449 win_spin.end_drag();
450 win_editor.end_drag();
451 code.end_drag();
452 diff.end_drag();
453 log.scrollbar.end_drag();
454 menu.scrollbar.end_drag();
455 }
456 continue;
457 }
458
459 if let MouseKind::Down(MouseButton::Left) = m.kind {
460 // Hit topmost window under cursor.
461 let mut hit: Option<WinId> = None;
462 for id in zorder.iter().rev() {
463 let win = match id {
464 WinId::Menu => &win_menu,
465 WinId::Log => &win_log,
466 WinId::Spin => &win_spin,
467 WinId::Editor => &win_editor,
468 };
469 if win.contains(m.col, m.row) {
470 hit = Some(*id);
471 break;
472 }
473 }
474 if let Some(id) = hit {
475 focused = id;
476 bring_front(&mut zorder, id);
477 let win = match id {
478 WinId::Menu => &mut win_menu,
479 WinId::Log => &mut win_log,
480 WinId::Spin => &mut win_spin,
481 WinId::Editor => &mut win_editor,
482 };
483 if win.handle_mouse(m.kind, m.col, m.row, workspace) {
484 continue;
485 }
486 // Editor mode tabs.
487 if id == WinId::Editor {
488 let inner = win_editor.inner();
489 let mode_bar = Rect::new(inner.x, inner.y, inner.width, 2);
490 if let Some(idx) = mode_tabs.index_at(mode_bar, m.col, m.row) {
491 editor_mode = if idx == 0 {
492 EditorMode::Code
493 } else {
494 EditorMode::Diff
495 };
496 status = format!("mode: {editor_mode:?}");
497 continue;
498 }
499 }
500 } else if input_area.contains(m.col, m.row) {
501 input_focus = true;
502 continue;
503 }
504 }
505
506 // Content mouse (scroll / select) on focused or hit window.
507 let editor_inner = win_editor.inner();
508 let mode_bar_h = 2u16;
509 let editor_body = Rect::new(
510 editor_inner.x,
511 editor_inner.y + mode_bar_h,
512 editor_inner.width,
513 editor_inner.height.saturating_sub(mode_bar_h),
514 );
515
516 if log.handle_mouse(win_log.inner(), m.kind, m.col, m.row)
517 || menu.handle_mouse(win_menu.inner(), m.kind, m.col, m.row)
518 || match editor_mode {
519 EditorMode::Code => {
520 code.handle_mouse(editor_body, m.kind, m.col, m.row)
521 }
522 EditorMode::Diff => {
523 diff.handle_mouse(editor_body, m.kind, m.col, m.row)
524 }
525 }
526 {
527 continue;
528 }
529
530 match m.kind {
531 MouseKind::Down(MouseButton::Left) => {
532 if let Some((cx, cy)) = ctx_at {
533 if let Some(idx) = ctx.index_at(cx, cy, m.col, m.row) {
534 ctx.list.select(idx);
535 let msg = ctx.list.selected_item().unwrap_or("").to_string();
536 status = format!("action: {msg}");
537 toasts.show(msg, Duration::from_secs(2));
538 }
539 ctx_at = None;
540 } else if win_menu.inner().contains(m.col, m.row) {
541 if let Some(idx) = menu.index_at(win_menu.inner(), m.row) {
542 menu.select(idx);
543 }
544 }
545 }
546 MouseKind::Down(MouseButton::Right) => {
547 ctx.list.selected = 0;
548 ctx.list.offset = 0;
549 ctx_at = Some((m.col, m.row));
550 }
551 MouseKind::ScrollUp => {
552 if ctx_at.is_some() {
553 ctx.list.select_up();
554 } else if win_menu.inner().contains(m.col, m.row) {
555 menu.select_up();
556 }
557 }
558 MouseKind::ScrollDown => {
559 if ctx_at.is_some() {
560 ctx.list.select_down();
561 } else if win_menu.inner().contains(m.col, m.row) {
562 menu.select_down();
563 }
564 }
565 MouseKind::Up => {
566 win_menu.end_drag();
567 win_log.end_drag();
568 win_spin.end_drag();
569 win_editor.end_drag();
570 code.end_drag();
571 diff.end_drag();
572 log.scrollbar.end_drag();
573 menu.scrollbar.end_drag();
574 }
575 _ => {}
576 }
577 }
578 Event::Resize(_, _) => {}
579 Event::Paste(_) => {}
580 }
581 }
582 }
583 Ok(())
584}Sourcepub fn set_line(&mut self, x: u16, y: u16, line: &Line, max_width: u16)
pub fn set_line(&mut self, x: u16, y: u16, line: &Line, max_width: u16)
Draw a styled line at (x, y), each span keeping its own style, clipped
to max_width cells (and the buffer edge).
Examples found in repository?
77fn main() -> std::io::Result<()> {
78 let mut term = Terminal::new()?;
79 term.mouse_mode(MouseMode::Motion)?;
80 let start = Instant::now();
81
82 let faint = Style::new().fg(rgb(0x63, 0x63, 0x63));
83 let dim = Style::new().fg(rgb(0x9c, 0x9c, 0x9c));
84
85 let pal = Palette {
86 panel: Style::new().bg(rgb(0x22, 0x22, 0x22)),
87 border: Style::new().fg(rgb(0x6a, 0x6a, 0x6a)),
88 normal: Style::new().fg(rgb(0xb8, 0xb8, 0xb8)),
89 accent: Style::new().fg(rgb(0xe6, 0xe6, 0xe6)),
90 selected: Style::new()
91 .fg(rgb(0x12, 0x12, 0x12))
92 .bg(rgb(0xd8, 0xd8, 0xd8))
93 .bold(),
94 track: Style::new().fg(rgb(0x30, 0x30, 0x30)),
95 thumb: Style::new().fg(rgb(0x86, 0x86, 0x86)),
96 };
97 let ctx_pal = Palette {
98 normal: Style::new()
99 .fg(rgb(0xc4, 0xc4, 0xc4))
100 .bg(rgb(0x2a, 0x2a, 0x2a)),
101 panel: Style::new().bg(rgb(0x2a, 0x2a, 0x2a)),
102 ..pal
103 };
104
105 let mut menu = List::new(
106 [
107 "New", "Open", "Save", "Copy", "Paste", "Find", "Settings", "Quit",
108 ]
109 .iter()
110 .map(|s| s.to_string())
111 .collect(),
112 );
113 let mut log = ScrollView::new(
114 (1..=40)
115 .map(|i| {
116 Line::from(vec![
117 Span::styled(format!(" {i:>2} "), faint),
118 Span::styled(format!("#{i:03} "), dim),
119 Span::styled("ok".to_string(), faint),
120 ])
121 })
122 .collect(),
123 );
124 log.scrollbar.style = ScrollbarStyle {
125 track_glyph: '░',
126 thumb_glyph: '▐',
127 track: Style::new()
128 .fg(rgb(0x40, 0x40, 0x40))
129 .bg(rgb(0x22, 0x22, 0x22)),
130 thumb: Style::new()
131 .fg(rgb(0x72, 0x72, 0x72))
132 .bg(rgb(0x22, 0x22, 0x22)),
133 thumb_active: Style::new()
134 .fg(rgb(0xcc, 0xcc, 0xcc))
135 .bg(rgb(0x22, 0x22, 0x22))
136 .bold(),
137 ..Default::default()
138 };
139 let mut ctx = Menu::new(
140 ["Copy", "Paste", "Delete"]
141 .iter()
142 .map(|s| s.to_string())
143 .collect(),
144 );
145
146 let mut code = CodeBlock::new(SAMPLE_RUST, Lang::Rust);
147 code.scrollbar.style = ScrollbarStyle {
148 track_glyph: '▕',
149 thumb_glyph: '█',
150 width: 1,
151 track: Style::new()
152 .fg(rgb(0x35, 0x35, 0x35))
153 .bg(rgb(0x22, 0x22, 0x22)),
154 thumb: Style::new()
155 .fg(rgb(0x88, 0x88, 0x88))
156 .bg(rgb(0x22, 0x22, 0x22)),
157 thumb_active: Style::new()
158 .fg(rgb(0xee, 0xee, 0xee))
159 .bg(rgb(0x22, 0x22, 0x22)),
160 };
161 let mut diff = DiffView::new(SAMPLE_RUST_OLD, SAMPLE_RUST);
162 diff.scrollbar.style = code.scrollbar.style;
163
164 let mut mode_tabs = Tabs::new(["Code", "Diff"].iter().map(|s| s.to_string()).collect());
165 let mut editor_mode = EditorMode::Code;
166
167 let mut win_menu = Window::new("menu", 2, 3, 28, 10);
168 let mut win_log = Window::new("log — drag ▐", 32, 3, 40, 10);
169 let mut win_spin = Window::new("spin", 74, 3, 18, 10);
170 let mut win_editor = Window::new("editor", 2, 14, 72, 18);
171 win_menu.fill = pal.panel;
172 win_log.fill = pal.panel;
173 win_spin.fill = pal.panel;
174 win_editor.fill = pal.panel;
175 win_editor.min_w = 28;
176 win_editor.min_h = 8;
177
178 let mut zorder = vec![WinId::Spin, WinId::Menu, WinId::Log, WinId::Editor];
179 let mut focused = WinId::Editor;
180
181 let mut input = TextInput::with_placeholder("type a toast message…");
182 let mut toasts = Toasts::default();
183
184 let mut ctx_at: Option<(u16, u16)> = None;
185 let mut input_focus = false;
186 let mut status = String::from("ready");
187 let mut quit = false;
188 let mut code_fill = false;
189
190 while !quit {
191 toasts.tick();
192 let phase = (start.elapsed().as_millis() / 90) as usize;
193 let size = term.size();
194
195 let footer_rows = 2u16;
196 let input_h = 3u16;
197 let input_y = size.height.saturating_sub(footer_rows + input_h);
198 let input_area = Rect::new(2, input_y, size.width.saturating_sub(4).min(52), input_h);
199 let workspace = Rect::new(0, 2, size.width, input_y.saturating_sub(2));
200
201 for w in [&mut win_menu, &mut win_log, &mut win_spin, &mut win_editor] {
202 w.clamp_to(workspace);
203 }
204
205 win_editor.title = match editor_mode {
206 EditorMode::Code => "editor · Code".into(),
207 EditorMode::Diff => "editor · Diff".into(),
208 };
209 mode_tabs.selected = match editor_mode {
210 EditorMode::Code => 0,
211 EditorMode::Diff => 1,
212 };
213
214 term.draw(|f| {
215 let bright = Style::new().fg(rgb(0xe6, 0xe6, 0xe6)).bold();
216 let buf = f.buffer();
217
218 buf.set_line(
219 2,
220 0,
221 &Line::from(shimmer("comb — drag windows · Code / Diff", phase)),
222 size.width.saturating_sub(4),
223 );
224 buf.set_str(
225 2,
226 1,
227 "title-bar move · edge resize · Code/Diff toggle · b fill",
228 faint,
229 );
230
231 for id in &zorder {
232 let focused_win = *id == focused;
233 match id {
234 WinId::Menu => {
235 win_menu.render_chrome(buf, &pal, focused_win);
236 menu.render(buf, win_menu.inner(), &pal);
237 }
238 WinId::Log => {
239 win_log.render_chrome(buf, &pal, focused_win);
240 log.render(buf, win_log.inner(), &pal);
241 }
242 WinId::Spin => {
243 win_spin.render_chrome(buf, &pal, focused_win);
244 let inner = win_spin.inner();
245 let on = |st: Style| st.patch(pal.panel);
246 for (i, sp) in SPINNERS.iter().take(8).enumerate() {
247 let y = inner.y + i as u16;
248 if y >= inner.bottom() {
249 break;
250 }
251 buf.set_str(inner.x, y, sp.frame(phase), on(bright));
252 buf.set_str(inner.x + 2, y, sp.name, on(dim));
253 }
254 }
255 WinId::Editor => {
256 win_editor.render_chrome(buf, &pal, focused_win);
257 let inner = win_editor.inner();
258 if inner.height < 3 {
259 continue;
260 }
261 let mode_bar = Rect::new(inner.x, inner.y, inner.width, 2);
262 mode_tabs.render(buf, mode_bar, &pal);
263 let body = Rect::new(
264 inner.x,
265 inner.y + 2,
266 inner.width,
267 inner.height.saturating_sub(2),
268 );
269 // Always opaque inside a stacked window; `b` toggles a
270 // slightly lifted panel vs the window chrome fill.
271 let bg = Some(if code_fill {
272 Style::new().bg(rgb(0x28, 0x28, 0x28))
273 } else {
274 win_editor.fill
275 });
276 match editor_mode {
277 EditorMode::Code => code.render(buf, body, bg),
278 EditorMode::Diff => diff.render(buf, body, bg),
279 }
280 }
281 }
282 }
283
284 let spark_x = input_area.right().saturating_add(2);
285 if spark_x + 20 < size.width {
286 let t = start.elapsed().as_secs_f32();
287 let values: Vec<f32> = (0..16)
288 .map(|i| {
289 let x = i as f32 / 4.0 + t;
290 (x.sin() * 0.4 + 0.5).clamp(0.05, 0.95)
291 })
292 .collect();
293 let mut line = Line::new();
294 for s in sparkline(&values, 16, Style::new().fg(rgb(0xc8, 0xc8, 0xc8))) {
295 line.push(s);
296 }
297 buf.set_line(spark_x, input_y + 1, &line, 16);
298 }
299
300 let input_cursor = input.render(buf, input_area, &pal);
301
302 let mode = match editor_mode {
303 EditorMode::Code => "Code",
304 EditorMode::Diff => "Diff",
305 };
306 let fill = if code_fill { "fill" } else { "plain" };
307 buf.set_str(
308 2,
309 size.height.saturating_sub(2),
310 &format!(
311 "status: {status} · {mode} · bg:{fill} · editor {}×{}",
312 win_editor.width, win_editor.height
313 ),
314 dim,
315 );
316 let hint = if input_focus {
317 "input focused · enter toast · tab unfocus"
318 } else {
319 "drag title · resize edges · d Code/Diff · b fill · q quit"
320 };
321 buf.set_str(2, size.height.saturating_sub(1), hint, faint);
322
323 if let Some((cx, cy)) = ctx_at {
324 ctx.render(f, cx, cy, 100, &ctx_pal);
325 }
326 toasts.render(f, 150, &pal);
327 if input_focus {
328 if let Some((x, y)) = input_cursor {
329 f.set_cursor(x, y);
330 }
331 }
332 })?;
333
334 let mut first = true;
335 loop {
336 let ms = if first { 40 } else { 0 };
337 let Some(ev) = term.read_event(Duration::from_millis(ms))? else {
338 break;
339 };
340 first = false;
341
342 if input_focus {
343 match handle_input_key(&mut input, &mut toasts, &mut status, &ev) {
344 InputResult::Quit => {
345 quit = true;
346 break;
347 }
348 InputResult::Unfocus => input_focus = false,
349 InputResult::Stay => {}
350 }
351 continue;
352 }
353
354 match ev {
355 Event::Key(k) => match k.code {
356 KeyCode::Char('q') => quit = true,
357 KeyCode::Char('c') if k.mods.ctrl => quit = true,
358 KeyCode::Char('b') => {
359 code_fill = !code_fill;
360 status = if code_fill {
361 "code bg: panel fill".into()
362 } else {
363 "code bg: terminal (plain)".into()
364 };
365 }
366 KeyCode::Char('d') => {
367 editor_mode = match editor_mode {
368 EditorMode::Code => EditorMode::Diff,
369 EditorMode::Diff => EditorMode::Code,
370 };
371 status = format!("mode: {editor_mode:?}");
372 bring_front(&mut zorder, WinId::Editor);
373 focused = WinId::Editor;
374 }
375 KeyCode::Tab if k.mods.ctrl && k.mods.shift => {
376 // cycle focus backward
377 if let Some(pos) = zorder.iter().position(|w| *w == focused) {
378 let prev = if pos == 0 { zorder.len() - 1 } else { pos - 1 };
379 focused = zorder[prev];
380 bring_front(&mut zorder, focused);
381 }
382 }
383 KeyCode::Tab if k.mods.ctrl => {
384 if let Some(pos) = zorder.iter().position(|w| *w == focused) {
385 let next = (pos + 1) % zorder.len();
386 focused = zorder[next];
387 bring_front(&mut zorder, focused);
388 }
389 }
390 KeyCode::Tab => input_focus = true,
391 KeyCode::Esc => {
392 if ctx_at.is_some() {
393 ctx_at = None;
394 } else {
395 quit = true;
396 }
397 }
398 KeyCode::Up => {
399 if ctx_at.is_some() {
400 ctx.list.select_up();
401 } else {
402 menu.select_up();
403 }
404 }
405 KeyCode::Down => {
406 if ctx_at.is_some() {
407 ctx.list.select_down();
408 } else {
409 menu.select_down();
410 }
411 }
412 KeyCode::Enter => {
413 if ctx_at.is_some() {
414 let msg = ctx.list.selected_item().unwrap_or("").to_string();
415 status = format!("action: {msg}");
416 toasts.show(msg, Duration::from_secs(2));
417 ctx_at = None;
418 } else {
419 let msg = menu.selected_item().unwrap_or("").to_string();
420 status = format!("ran: {msg}");
421 toasts.show(format!("ran {msg}"), Duration::from_secs(2));
422 }
423 }
424 _ => {}
425 },
426 Event::Mouse(m) => {
427 // Active window drag/resize first.
428 let dragging = win_menu.is_dragging()
429 || win_log.is_dragging()
430 || win_spin.is_dragging()
431 || win_editor.is_dragging();
432 if dragging {
433 for id in zorder.iter().rev() {
434 let win = match id {
435 WinId::Menu => &mut win_menu,
436 WinId::Log => &mut win_log,
437 WinId::Spin => &mut win_spin,
438 WinId::Editor => &mut win_editor,
439 };
440 if win.is_dragging()
441 && win.handle_mouse(m.kind, m.col, m.row, workspace)
442 {
443 break;
444 }
445 }
446 if matches!(m.kind, MouseKind::Up) {
447 win_menu.end_drag();
448 win_log.end_drag();
449 win_spin.end_drag();
450 win_editor.end_drag();
451 code.end_drag();
452 diff.end_drag();
453 log.scrollbar.end_drag();
454 menu.scrollbar.end_drag();
455 }
456 continue;
457 }
458
459 if let MouseKind::Down(MouseButton::Left) = m.kind {
460 // Hit topmost window under cursor.
461 let mut hit: Option<WinId> = None;
462 for id in zorder.iter().rev() {
463 let win = match id {
464 WinId::Menu => &win_menu,
465 WinId::Log => &win_log,
466 WinId::Spin => &win_spin,
467 WinId::Editor => &win_editor,
468 };
469 if win.contains(m.col, m.row) {
470 hit = Some(*id);
471 break;
472 }
473 }
474 if let Some(id) = hit {
475 focused = id;
476 bring_front(&mut zorder, id);
477 let win = match id {
478 WinId::Menu => &mut win_menu,
479 WinId::Log => &mut win_log,
480 WinId::Spin => &mut win_spin,
481 WinId::Editor => &mut win_editor,
482 };
483 if win.handle_mouse(m.kind, m.col, m.row, workspace) {
484 continue;
485 }
486 // Editor mode tabs.
487 if id == WinId::Editor {
488 let inner = win_editor.inner();
489 let mode_bar = Rect::new(inner.x, inner.y, inner.width, 2);
490 if let Some(idx) = mode_tabs.index_at(mode_bar, m.col, m.row) {
491 editor_mode = if idx == 0 {
492 EditorMode::Code
493 } else {
494 EditorMode::Diff
495 };
496 status = format!("mode: {editor_mode:?}");
497 continue;
498 }
499 }
500 } else if input_area.contains(m.col, m.row) {
501 input_focus = true;
502 continue;
503 }
504 }
505
506 // Content mouse (scroll / select) on focused or hit window.
507 let editor_inner = win_editor.inner();
508 let mode_bar_h = 2u16;
509 let editor_body = Rect::new(
510 editor_inner.x,
511 editor_inner.y + mode_bar_h,
512 editor_inner.width,
513 editor_inner.height.saturating_sub(mode_bar_h),
514 );
515
516 if log.handle_mouse(win_log.inner(), m.kind, m.col, m.row)
517 || menu.handle_mouse(win_menu.inner(), m.kind, m.col, m.row)
518 || match editor_mode {
519 EditorMode::Code => {
520 code.handle_mouse(editor_body, m.kind, m.col, m.row)
521 }
522 EditorMode::Diff => {
523 diff.handle_mouse(editor_body, m.kind, m.col, m.row)
524 }
525 }
526 {
527 continue;
528 }
529
530 match m.kind {
531 MouseKind::Down(MouseButton::Left) => {
532 if let Some((cx, cy)) = ctx_at {
533 if let Some(idx) = ctx.index_at(cx, cy, m.col, m.row) {
534 ctx.list.select(idx);
535 let msg = ctx.list.selected_item().unwrap_or("").to_string();
536 status = format!("action: {msg}");
537 toasts.show(msg, Duration::from_secs(2));
538 }
539 ctx_at = None;
540 } else if win_menu.inner().contains(m.col, m.row) {
541 if let Some(idx) = menu.index_at(win_menu.inner(), m.row) {
542 menu.select(idx);
543 }
544 }
545 }
546 MouseKind::Down(MouseButton::Right) => {
547 ctx.list.selected = 0;
548 ctx.list.offset = 0;
549 ctx_at = Some((m.col, m.row));
550 }
551 MouseKind::ScrollUp => {
552 if ctx_at.is_some() {
553 ctx.list.select_up();
554 } else if win_menu.inner().contains(m.col, m.row) {
555 menu.select_up();
556 }
557 }
558 MouseKind::ScrollDown => {
559 if ctx_at.is_some() {
560 ctx.list.select_down();
561 } else if win_menu.inner().contains(m.col, m.row) {
562 menu.select_down();
563 }
564 }
565 MouseKind::Up => {
566 win_menu.end_drag();
567 win_log.end_drag();
568 win_spin.end_drag();
569 win_editor.end_drag();
570 code.end_drag();
571 diff.end_drag();
572 log.scrollbar.end_drag();
573 menu.scrollbar.end_drag();
574 }
575 _ => {}
576 }
577 }
578 Event::Resize(_, _) => {}
579 Event::Paste(_) => {}
580 }
581 }
582 }
583 Ok(())
584}Sourcepub fn set_line_on(
&mut self,
x: u16,
y: u16,
line: &Line,
max_width: u16,
base: Style,
)
pub fn set_line_on( &mut self, x: u16, y: u16, line: &Line, max_width: u16, base: Style, )
Like [set_line], but every span is patched with base (typically a
panel background) so fg-only highlight styles don’t wipe the row bg.
Always clears the remainder of the row so scroll / short lines cannot
leave stale glyphs behind.
Sourcepub fn set_lines(&mut self, area: Rect, lines: &[Line], scroll: usize)
pub fn set_lines(&mut self, area: Rect, lines: &[Line], scroll: usize)
Draw lines top-down within area, starting at logical index scroll,
clipped to the area’s height and width. The Paragraph-with-scroll of comb.
Sourcepub fn set_lines_on(
&mut self,
area: Rect,
lines: &[Line],
scroll: usize,
base: Style,
)
pub fn set_lines_on( &mut self, area: Rect, lines: &[Line], scroll: usize, base: Style, )
Like [set_lines], patching every cell with base (e.g. panel bg).
Empty rows in the viewport are always cleared.
Sourcepub fn fill(&mut self, rect: Rect, ch: char, style: Style)
pub fn fill(&mut self, rect: Rect, ch: char, style: Style)
Fill a rectangle with ch/style (intersected with the buffer).
Sourcepub fn paint(&mut self, rect: Rect, style: Style)
pub fn paint(&mut self, rect: Rect, style: Style)
Paint a background over a rectangle, leaving spaces. Handy for panels.
Sourcepub fn put_str_aligned(
&mut self,
x: u16,
y: u16,
width: u16,
text: &str,
style: Style,
align: Align,
)
pub fn put_str_aligned( &mut self, x: u16, y: u16, width: u16, text: &str, style: Style, align: Align, )
Fill a horizontal band with style, then draw text inside it.
The whole band is painted first, then one padded line is written so chip
backgrounds stay solid. Align::Center uses center_pad.
Sourcepub fn put_label(&mut self, area: Rect, text: &str, style: Style)
pub fn put_label(&mut self, area: Rect, text: &str, style: Style)
Fill area with style and center text horizontally + vertically.
Use chip_cols to choose a shared width that centers as many labels
as possible when their display widths have mixed parity.
Sourcepub fn blit(&mut self, ox: u16, oy: u16, src: &Buffer, skip_transparent: bool)
pub fn blit(&mut self, ox: u16, oy: u16, src: &Buffer, skip_transparent: bool)
Copy src onto this buffer at (ox, oy). When skip_transparent, cells
marked transparent in src don’t overwrite what’s underneath.