Resizable grid with orca's bindings: ( ) width, _ + height, Ctrl+arrows

Grid is now dynamic (w/h up to a max, fixed-stride storage so slot indices
stay stable and content isn't remapped). Ruler marks (+) on empty cells for
orientation; cursor clamps on shrink; status shows the size.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GLUwWE2KmFPzhKaf67tWbx
This commit is contained in:
Bendik Aagaard Lynghaug
2026-09-13 16:16:32 +02:00
co-authored by Claude Opus 4.8
parent 071d1b6d63
commit edd9ff70c6
4 changed files with 145 additions and 45 deletions
+11 -8
View File
@@ -9,7 +9,7 @@
//! params, lowercase a-z read MIDI CC (a = CC 0). A neighbour to the left/right
//! supplies a parameter; `M` instead reads the left cell as a second signal.
use crate::grid::{Grid, H, SLOTS, W};
use crate::grid::{Grid, MAX_W, SLOTS};
pub const MAX_STATE: usize = 4 * SLOTS;
pub const TAU: f32 = std::f32::consts::TAU;
@@ -110,20 +110,23 @@ pub fn compile(grid: &Grid) -> Program {
let mut nodes = Vec::new();
let mut st = 0usize;
let mut has_env = false;
for row in 0..H {
for col in 0..W {
for row in 0..grid.h {
for col in 0..grid.w {
let Some((op, ssize)) = op_of(grid.get(col, row)) else { continue };
if matches!(op, Op::Env) {
has_env = true;
}
let input =
if row > 0 && occupied(grid.get(col, row - 1)) { Some((row - 1) * W + col) } else { None };
let lsig = if col > 0 { Some(row * W + col - 1) } else { None };
let input = if row > 0 && occupied(grid.get(col, row - 1)) {
Some((row - 1) * MAX_W + col)
} else {
None
};
let lsig = if col > 0 { Some(row * MAX_W + col - 1) } else { None };
let left = if col > 0 { param_of(grid.get(col - 1, row)) } else { Param::None };
let right = if col + 1 < W { param_of(grid.get(col + 1, row)) } else { Param::None };
let right = if col + 1 < grid.w { param_of(grid.get(col + 1, row)) } else { Param::None };
let this_st = st;
st = (st + ssize).min(MAX_STATE - 4);
nodes.push(Node { op, out: row * W + col, input, lsig, left, right, st: this_st });
nodes.push(Node { op, out: row * MAX_W + col, input, lsig, left, right, st: this_st });
}
}
Program { nodes, has_env }
+33 -9
View File
@@ -1,31 +1,55 @@
//! The fixed-size character grid that is the editor surface.
//! The character grid — the editor surface. Logical size (`w`×`h`) is
//! resizable up to the max; cells are stored at a fixed `MAX_W` stride so a
//! resize never remaps existing content and compiled slot indices stay stable.
pub const W: usize = 24;
pub const H: usize = 16;
pub const SLOTS: usize = W * H;
pub const MAX_W: usize = 40;
pub const MAX_H: usize = 24;
pub const SLOTS: usize = MAX_W * MAX_H;
pub const DEFAULT_W: usize = 24;
pub const DEFAULT_H: usize = 16;
pub const RULER: usize = 8;
#[derive(Clone)]
pub struct Grid {
pub w: usize,
pub h: usize,
cells: Vec<char>,
}
impl Grid {
pub fn new() -> Self {
Grid { cells: vec!['.'; SLOTS] }
Grid { w: DEFAULT_W, h: DEFAULT_H, cells: vec!['.'; SLOTS] }
}
#[inline]
pub fn get(&self, col: usize, row: usize) -> char {
if col < W && row < H {
self.cells[row * W + col]
if col < self.w && row < self.h {
self.cells[row * MAX_W + col]
} else {
'.'
}
}
pub fn set(&mut self, col: usize, row: usize, c: char) {
if col < W && row < H {
self.cells[row * W + col] = c;
if col < self.w && row < self.h {
self.cells[row * MAX_W + col] = c;
}
}
/// Resize to nh×nw, clamped to [1, MAX]. Content keeps its position.
pub fn resize(&mut self, nh: usize, nw: usize) {
self.h = nh.clamp(1, MAX_H);
self.w = nw.clamp(1, MAX_W);
}
}
/// orca-c's ruler snap: resize by one ruler division, snapped to a
/// ruler-aligned size (1, 9, 17, ...) rather than a plain +/-1.
pub fn ruler_snap(size: usize, delta_rulers: isize) -> usize {
if size == 0 {
return if delta_rulers > 0 { RULER * delta_rulers as usize } else { 1 };
}
let size = if delta_rulers < 0 { size + RULER - 1 } else { size };
let n = ((size as isize - 1) / RULER as isize + delta_rulers).max(0);
(RULER as isize * n + 1) as usize
}
+97 -27
View File
@@ -1,7 +1,7 @@
//! Grid editor. crossterm renderer adapted from orca-rs's grid view: a centered
//! canvas with per-glyph colour and a change-diff so only cells that actually
//! change get repainted (a full clear+redraw each frame flickers over SSH on
//! terminals that ignore synchronized-update, e.g. macOS Terminal). `?` toggles
//! canvas with per-glyph colour, ruler marks, resize bindings, and a change-diff
//! so only cells that actually change repaint (a full clear+redraw each frame
//! flickers over SSH on terminals that ignore synchronized-update). `?` toggles
//! a detailed help overlay; run a spectrum analyzer on the side if you want one.
use std::io::{self, Write};
@@ -19,7 +19,7 @@ use crossterm::terminal::{
use crossterm::{execute, queue, terminal};
use crate::dsp::{compile, Program};
use crate::grid::{Grid, H, W};
use crate::grid::{ruler_snap, Grid, RULER};
const HELP: &[&str] = &[
"galdr — grid synthesizer",
@@ -40,6 +40,7 @@ const HELP: &[&str] = &[
"ROUTING M mix — adds the cell to its LEFT ! output to master",
"",
"KEYS arrows move · type a glyph to place · Backspace clears",
" ( ) width -/+ _ + height -/+ Ctrl+arrows resize by one",
" ? toggle this help · Esc quit",
];
@@ -58,7 +59,6 @@ fn glyph_color(c: char) -> Color {
'!' => Color::Magenta,
'0'..='9' => Color::Grey,
'a'..='z' => Color::DarkYellow,
'.' => Color::DarkGrey,
_ => Color::White,
}
}
@@ -68,13 +68,24 @@ struct Renderer {
status: String,
tw: u16,
th: u16,
gw: usize,
gh: usize,
help: bool,
init: bool,
}
impl Renderer {
fn new() -> Self {
Renderer { cells: Vec::new(), status: String::new(), tw: 0, th: 0, help: false, init: false }
Renderer {
cells: Vec::new(),
status: String::new(),
tw: 0,
th: 0,
gw: 0,
gh: 0,
help: false,
init: false,
}
}
}
@@ -102,6 +113,8 @@ pub fn run(
continue;
}
let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
let mut resized = false;
let mut edited = false;
match k.code {
KeyCode::Esc => {
if help {
@@ -112,22 +125,61 @@ pub fn run(
}
KeyCode::Char('c') if ctrl => return Ok(()),
KeyCode::Char('?') => help = !help,
_ if help => {} // any other key: ignore while help is open
_ if help => {}
KeyCode::Char('(') => {
grid.resize(grid.h, ruler_snap(grid.w, -1));
resized = true;
}
KeyCode::Char(')') => {
grid.resize(grid.h, ruler_snap(grid.w, 1));
resized = true;
}
KeyCode::Char('_') => {
grid.resize(ruler_snap(grid.h, -1), grid.w);
resized = true;
}
KeyCode::Char('+') => {
grid.resize(ruler_snap(grid.h, 1), grid.w);
resized = true;
}
KeyCode::Left if ctrl => {
grid.resize(grid.h, grid.w.saturating_sub(1));
resized = true;
}
KeyCode::Right if ctrl => {
grid.resize(grid.h, grid.w + 1);
resized = true;
}
KeyCode::Up if ctrl => {
grid.resize(grid.h.saturating_sub(1), grid.w);
resized = true;
}
KeyCode::Down if ctrl => {
grid.resize(grid.h + 1, grid.w);
resized = true;
}
KeyCode::Left => cx = cx.saturating_sub(1),
KeyCode::Right => cx = (cx + 1).min(W - 1),
KeyCode::Right => cx = (cx + 1).min(grid.w - 1),
KeyCode::Up => cy = cy.saturating_sub(1),
KeyCode::Down => cy = (cy + 1).min(H - 1),
KeyCode::Down => cy = (cy + 1).min(grid.h - 1),
KeyCode::Backspace | KeyCode::Delete => {
grid.set(cx, cy, '.');
prog.store(Arc::new(compile(&grid)));
edited = true;
}
KeyCode::Char(c) => {
grid.set(cx, cy, if c == ' ' { '.' } else { c });
prog.store(Arc::new(compile(&grid)));
cx = (cx + 1).min(W - 1);
edited = true;
cx = (cx + 1).min(grid.w - 1);
}
_ => {}
}
if resized {
cx = cx.min(grid.w - 1);
cy = cy.min(grid.h - 1);
}
if resized || edited {
prog.store(Arc::new(compile(&grid)));
}
}
})();
@@ -149,15 +201,20 @@ fn draw(
) -> io::Result<()> {
let (tw, th) = terminal::size()?;
// Full repaint only on first frame, resize, or a help toggle - never every
// frame (that is the flicker).
let full = !r.init || tw != r.tw || th != r.th || help != r.help;
let full = !r.init
|| tw != r.tw
|| th != r.th
|| help != r.help
|| grid.w != r.gw
|| grid.h != r.gh;
if full {
queue!(out, Clear(ClearType::All))?;
r.cells = vec![('\0', Cs::Fg(Color::Reset)); W * H];
r.cells = vec![('\0', Cs::Fg(Color::Reset)); grid.w * grid.h];
r.status.clear();
r.tw = tw;
r.th = th;
r.gw = grid.w;
r.gh = grid.h;
r.help = help;
r.init = true;
}
@@ -179,27 +236,40 @@ fn draw(
return out.flush();
}
let off_x = (tw as usize).saturating_sub(W) / 2;
let off_y = (th as usize).saturating_sub(H + 1) / 2;
for row in 0..H {
for col in 0..W {
let c = grid.get(col, row);
let cs = if col == cx && row == cy { Cs::Cursor } else { Cs::Fg(glyph_color(c)) };
let idx = row * W + col;
if r.cells[idx] == (c, cs) {
let off_x = (tw as usize).saturating_sub(grid.w) / 2;
let off_y = (th as usize).saturating_sub(grid.h + 1) / 2;
for row in 0..grid.h {
for col in 0..grid.w {
let raw = grid.get(col, row);
// ruler mark on empty intersections, for orientation while sizing
let disp = if raw == '.' {
if row % RULER == 0 && col % RULER == 0 { '+' } else { '.' }
} else {
raw
};
let cs = if col == cx && row == cy {
Cs::Cursor
} else if raw == '.' {
Cs::Fg(Color::DarkGrey)
} else {
Cs::Fg(glyph_color(raw))
};
let idx = row * grid.w + col;
if r.cells[idx] == (disp, cs) {
continue;
}
r.cells[idx] = (c, cs);
r.cells[idx] = (disp, cs);
queue!(out, MoveTo((off_x + col) as u16, (off_y + row) as u16), SetAttribute(Attribute::Reset))?;
match cs {
Cs::Cursor => queue!(out, SetAttribute(Attribute::Reverse))?,
Cs::Fg(color) => queue!(out, SetForegroundColor(color))?,
}
queue!(out, Print(c))?;
queue!(out, Print(disp))?;
}
}
let status = format!("voices {voices} latency {latency}f ? help Esc quit");
let status =
format!("voices {voices} {}x{} latency {latency}f ? help Esc quit", grid.w, grid.h);
if status != r.status {
r.status = status.clone();
let pad: String = format!("{status:<width$}", width = tw as usize);