wryme 1.1.6

wryme • that small, calm window where agents come to meet you
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// The "station" popup. The single place in the TUI where the user can:
//   - tune the active station (model + dials) for the current session
//   - jump to a different saved station
//   - save the active configuration under a new name
//
// State machine:
//   Closed   -> the popup is not visible
//   Browse   -> popup is open, arrow-key navigation
//   SaveAs   -> popup is open, a sub-input is collecting a name
//
// All popup actions mutate App. Dials and model changes apply to the
// next turn (the next time the user hits Enter on the main input).

use crate::app::App;
use crate::input::Input;
use crate::shop::Shop;
use crate::station::{Patience, Station};

/// Popup lifecycle state. Default is closed.
#[derive(Debug, Default)]
pub struct Popup {
    pub mode: Mode,
    /// Which BIOS-style tab the popup is showing. Station is the tuning
    /// list; Help lists the shortcuts.
    pub tab: Tab,
    /// Index of the currently-focused row when in Browse mode. The list
    /// of rows is rebuilt each frame from the current app state; the
    /// renderer clamps this to a legal value.
    pub selected: usize,
    /// Vertical scroll offset for the popup body, in rows.
    pub scroll: usize,
    /// Used while in SaveAs mode.
    pub name_input: Input,
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
pub enum Tab {
    #[default]
    Station,
    Help,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub enum Mode {
    #[default]
    Closed,
    Browse,
    SaveAs,
}

/// One row of the popup. The renderer turns these into Lines; the
/// keyboard handler dispatches based on which one is selected.
#[derive(Debug, Clone)]
pub enum Row {
    SectionHeader(&'static str),
    Model,
    Boldness,
    Patience,
    Verbosity,
    SavedStation(usize), // index into App.stations
    UpdateAction,        // only present when origin is set AND dirty
    SaveAsAction,
    Blank,
}

/// Build the row list from current app state. Order is fixed: active
/// section header, model, three dials, blank, saved header, each saved
/// station (skipping the demo placeholder), blank, conditional update
/// action, save-as action.
pub fn rows(app: &App) -> Vec<Row> {
    let mut out = vec![
        Row::SectionHeader("active"),
        Row::Model,
        Row::Boldness,
        Row::Patience,
        Row::Verbosity,
        Row::Blank,
        Row::SectionHeader("saved"),
    ];
    for (i, st) in app.stations.iter().enumerate() {
        if st.name == "demo" {
            continue;
        }
        out.push(Row::SavedStation(i));
    }
    out.push(Row::Blank);
    if app.active_origin.is_some() && app.is_dirty() {
        out.push(Row::UpdateAction);
    }
    out.push(Row::SaveAsAction);
    out
}

/// Indexes within `rows()` that represent a selectable item (not a
/// section header or blank). Arrow up/down moves between these.
pub fn selectable_indices(rows: &[Row]) -> Vec<usize> {
    rows.iter()
        .enumerate()
        .filter(|(_, r)| {
            !matches!(r, Row::SectionHeader(_) | Row::Blank)
        })
        .map(|(i, _)| i)
        .collect()
}

/// Open the popup if it is closed; close it if it is open. Bound to Ctrl-S.
pub fn toggle(app: &mut App) {
    match app.popup.mode {
        Mode::Closed => {
            app.popup.mode = Mode::Browse;
            app.popup.tab = Tab::Station;
            // Land on the model row by default.
            app.popup.selected = first_selectable(app);
            app.popup.scroll = 0;
        }
        Mode::Browse | Mode::SaveAs => {
            close(app);
        }
    }
}

pub fn close(app: &mut App) {
    app.popup.mode = Mode::Closed;
    app.popup.tab = Tab::Station;
    app.popup.name_input = Input::new();
    app.popup.selected = 0;
    app.popup.scroll = 0;
}

fn first_selectable(app: &App) -> usize {
    let r = rows(app);
    selectable_indices(&r).first().copied().unwrap_or(0)
}

/// Move the selection by `delta` (+1 / -1) through the selectable rows.
pub fn move_selection(app: &mut App, delta: i32) {
    let r = rows(app);
    let sel = selectable_indices(&r);
    if sel.is_empty() {
        return;
    }
    // Find the position of the current selection within the selectable
    // list; if it isn't there, snap to the first.
    let pos = sel
        .iter()
        .position(|&i| i == app.popup.selected)
        .unwrap_or(0);
    let new_pos = ((pos as i32 + delta).rem_euclid(sel.len() as i32)) as usize;
    app.popup.selected = sel[new_pos];
}

/// Left/right arrow on the focused row. Cycles model choices or dial
/// preset values.
pub fn adjust(app: &mut App, delta: i32) {
    let r = rows(app);
    let row = r.get(app.popup.selected).cloned();
    match row {
        Some(Row::Model) => cycle_model(app, delta),
        Some(Row::Boldness) => cycle_boldness(app, delta),
        Some(Row::Patience) => cycle_patience(app, delta),
        Some(Row::Verbosity) => cycle_verbosity(app, delta),
        _ => {}
    }
}

/// Enter on the focused row. For dial rows, the same as a right-adjust.
/// For a saved station, load it. For the update action, write current
/// state back to the saved entry. For the save-as action, switch to
/// SaveAs mode.
pub fn activate(app: &mut App) {
    let r = rows(app);
    let row = r.get(app.popup.selected).cloned();
    match row {
        Some(Row::SavedStation(idx)) => {
            if let Some(st) = app.stations.get(idx).cloned() {
                load_station(app, st);
            }
        }
        Some(Row::UpdateAction) => {
            commit_update(app);
        }
        Some(Row::SaveAsAction) => {
            app.popup.mode = Mode::SaveAs;
            app.popup.name_input = Input::new();
        }
        Some(Row::Model | Row::Boldness | Row::Patience | Row::Verbosity) => {
            adjust(app, 1);
        }
        _ => {}
    }
}

/// Commit the SaveAs name input: append a new station to the stations
/// file with the current model + dials. Then claim that name as the new
/// origin so the session becomes "clean."
pub fn commit_save_as(app: &mut App) {
    let name = app.popup.name_input.text.trim().to_string();
    if name.is_empty() {
        app.note("name can't be empty");
        return;
    }
    if app.stations.iter().any(|s| s.name == name) {
        app.note(format!("station '{}' already exists", name));
        return;
    }
    let new_station = Station {
        name: name.clone(),
        model: app.active_station.model.clone(),
        dials: app.active_station.dials,
    };
    let Some(path) = crate::station::config_path() else {
        app.note("save failed: no $HOME");
        return;
    };
    if let Err(e) = crate::station_save::append_to_file(&path, &new_station) {
        app.note(format!("save failed: {}", e));
        return;
    }
    app.stations.push(new_station);
    app.active_station.name = name.clone();
    app.active_origin = Some(name.clone());
    app.note(format!("saved station '{}'", name));
    app.popup.mode = Mode::Browse;
    app.popup.name_input = Input::new();
}

/// Overwrite the saved entry for `active_origin` with the current
/// active state. Surgical edit: other [[station]] blocks and comments
/// in the file stay intact.
pub fn commit_update(app: &mut App) {
    let Some(origin) = app.active_origin.clone() else {
        app.note("nothing to update; this is an untitled session");
        return;
    };
    let Some(path) = crate::station::config_path() else {
        app.note("save failed: no $HOME");
        return;
    };
    let updated = Station {
        name: origin.clone(),
        model: app.active_station.model.clone(),
        dials: app.active_station.dials,
    };
    if let Err(e) = crate::station_save::update_in_file(&path, &updated) {
        app.note(format!("update failed: {}", e));
        return;
    }
    // Replace the in-memory entry too.
    if let Some(saved) = app.stations.iter_mut().find(|s| s.name == origin) {
        *saved = updated;
    }
    app.note(format!("updated station '{}'", origin));
}

/// Replace the active station and re-resolve the shop for its model.
/// Sets `active_origin` so the session traces back to the loaded entry.
fn load_station(app: &mut App, st: Station) {
    let shop = crate::shop::find_for_model(&app.shops, &st.model).cloned();
    if let Some(shop) = shop {
        let name = st.name.clone();
        app.active_origin = Some(name.clone());
        app.active_station = st;
        app.active_shop = shop;
        app.last_response_id = None;
        app.note(format!("loaded station '{}'", name));
    } else {
        app.note(format!(
            "can't load '{}': no shop runs '{}'",
            st.name, st.model
        ));
    }
}

// ---- model cycling ----

fn cycle_model(app: &mut App, delta: i32) {
    let all_models = collect_models(&app.shops);
    if all_models.is_empty() {
        return;
    }
    let cur = all_models
        .iter()
        .position(|m| m == &app.active_station.model)
        .unwrap_or(0);
    let n = all_models.len() as i32;
    let next = ((cur as i32 + delta).rem_euclid(n)) as usize;
    let next_model = all_models[next].clone();
    if let Some(shop) = crate::shop::find_for_model(&app.shops, &next_model).cloned() {
        app.active_station.model = next_model;
        app.active_shop = shop;
        app.last_response_id = None;
    }
}

fn collect_models(shops: &[Shop]) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for s in shops {
        for m in &s.models {
            if !out.contains(m) {
                out.push(m.clone());
            }
        }
    }
    out
}

// ---- dial cycling ----

const BOLDNESS_PRESETS: &[(&str, f32)] = &[
    ("mild", 0.2),
    ("balanced", 0.7),
    ("spicy", 1.2),
    ("wild", 1.8),
];

fn cycle_boldness(app: &mut App, delta: i32) {
    let states: Vec<Option<f32>> = std::iter::once(None)
        .chain(BOLDNESS_PRESETS.iter().map(|(_, v)| Some(*v)))
        .collect();
    let cur = states
        .iter()
        .position(|s| match (s, app.active_station.dials.boldness) {
            (None, None) => true,
            (Some(a), Some(b)) => (*a - b).abs() < 1e-6,
            _ => false,
        })
        .unwrap_or(0);
    let n = states.len() as i32;
    let next = ((cur as i32 + delta).rem_euclid(n)) as usize;
    app.active_station.dials.boldness = states[next];
}

pub fn boldness_label(v: Option<f32>) -> String {
    match v {
        None => "".into(),
        Some(x) => {
            let preset = BOLDNESS_PRESETS
                .iter()
                .find(|(_, val)| (val - x).abs() < 1e-6);
            match preset {
                Some((name, _)) => format!("{} ({:.1})", name, x),
                None => format!("{:.2}", x),
            }
        }
    }
}

fn cycle_patience(app: &mut App, delta: i32) {
    let states: &[Option<Patience>] = &[
        None,
        Some(Patience::Quick),
        Some(Patience::Steady),
        Some(Patience::Slow),
    ];
    let cur = states
        .iter()
        .position(|s| *s == app.active_station.dials.patience)
        .unwrap_or(0);
    let n = states.len() as i32;
    let next = ((cur as i32 + delta).rem_euclid(n)) as usize;
    app.active_station.dials.patience = states[next];
}

pub fn patience_label(v: Option<Patience>) -> &'static str {
    match v {
        None => "",
        Some(Patience::Quick) => "quick",
        Some(Patience::Steady) => "steady",
        Some(Patience::Slow) => "slow",
    }
}

const VERBOSITY_PRESETS: &[(&str, u32)] = &[
    ("small", 256),
    ("medium", 1024),
    ("large", 4096),
    ("heaping", 8192),
];

fn cycle_verbosity(app: &mut App, delta: i32) {
    let states: Vec<Option<u32>> = std::iter::once(None)
        .chain(VERBOSITY_PRESETS.iter().map(|(_, v)| Some(*v)))
        .collect();
    let cur = states
        .iter()
        .position(|s| *s == app.active_station.dials.verbosity)
        .unwrap_or(0);
    let n = states.len() as i32;
    let next = ((cur as i32 + delta).rem_euclid(n)) as usize;
    app.active_station.dials.verbosity = states[next];
}

pub fn verbosity_label(v: Option<u32>) -> String {
    match v {
        None => "".into(),
        Some(x) => {
            let preset = VERBOSITY_PRESETS.iter().find(|(_, val)| *val == x);
            match preset {
                Some((name, _)) => format!("{} ({})", name, x),
                None => format!("{}", x),
            }
        }
    }
}

/// Cycle the tab bar: Station <-> Help. Returns to Browse mode and
/// snaps the selection to the top of the new tab.
pub fn switch_tab(app: &mut App) {
    app.popup.tab = match app.popup.tab {
        Tab::Station => Tab::Help,
        Tab::Help => Tab::Station,
    };
    app.popup.mode = Mode::Browse;
    app.popup.name_input = Input::new();
    app.popup.selected = 0;
    app.popup.scroll = 0;
}

/// Open the popup with the Help tab selected (bound to F1). If the
/// popup is already open, just switch to Help.
pub fn open_help(app: &mut App) {
    if app.popup.mode == Mode::Closed {
        app.popup.mode = Mode::Browse;
        app.popup.selected = 0;
    }
    app.popup.tab = Tab::Help;
    app.popup.mode = Mode::Browse;
    app.popup.name_input = Input::new();
    app.popup.scroll = 0;
}

/// Scroll the popup body by `delta` rows. Clamped by the renderer each
/// frame, but we keep the offset sane here too.
pub fn scroll(app: &mut App, delta: i32) {
    let r = if delta > 0 {
        app.popup.scroll.saturating_add(delta as usize)
    } else {
        app.popup.scroll.saturating_sub((-delta) as usize)
    };
    app.popup.scroll = r;
}

/// Static shortcut list for the Help tab. One line per binding, kept in
/// roughly the order they appear in keys.rs. The renderer shows this as
/// a two-column table: key on the left, meaning on the right.
pub fn help_rows() -> Vec<(String, String)> {
    vec![
        ("Enter".into(), "send input".into()),
        ("Esc".into(), "stop a streaming reply / clear note".into()),
        ("Ctrl-C".into(), "quit immediately".into()),
        ("Ctrl-T".into(), "toggle page / scroll view".into()),
        ("PgUp / PgDn".into(), "page or scroll up / down".into()),
        ("← / →".into(), "move the input cursor".into()),
        ("Home / End".into(), "jump to input start / end".into()),
        ("Backspace / Delete".into(), "delete before / after cursor".into()),
        ("Ctrl-A / Ctrl-E".into(), "jump to input start / end".into()),
        ("Ctrl-U".into(), "kill to start of line".into()),
        ("Ctrl-K".into(), "kill to end of line".into()),
        ("Ctrl-W".into(), "kill previous word".into()),
        ("Ctrl-S".into(), "open / close this popup".into()),
        ("Tab / F1".into(), "switch Station / Help tab".into()),
        ("Mouse wheel".into(), "scroll in page / scroll view".into()),
        ("".into(), "".into()),
        ("In the Station tab:".into(), "".into()),
        ("↑ / ↓".into(), "move between selectable rows".into()),
        ("← / →".into(), "adjust model / dials (also Enter)".into()),
        ("Enter".into(), "load station / update / save-as".into()),
        ("PgUp / PgDn".into(), "scroll the popup body".into()),
        ("Esc".into(), "close popup".into()),
        ("".into(), "".into()),
        ("In the Help tab:".into(), "".into()),
        ("Esc / Tab".into(), "leave Help back to Station".into()),
        ("F1".into(), "open Help tab from anywhere".into()),
    ]
}