ipatool-kit 1.1.2

App Store IPA toolkit — library + interactive TUI for owned IPA downloads
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
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
476
477
478
479
480
481
//! Full-screen redraw menus. Raw mode needs `\r\n` / MoveTo — bare `\n` staircases.

use std::io::{self, Write};
use crossterm::cursor::{Hide, MoveTo, Show};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use crossterm::execute;
use crossterm::style::{Print, Stylize};
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, size, Clear, ClearType, EnterAlternateScreen,
    LeaveAlternateScreen,
};
use ipatool_kit::IpatoolError;

pub struct TermGuard;

impl TermGuard {
    pub fn enter() -> Result<Self, IpatoolError> {
        enable_raw_mode().map_err(io_err)?;
        execute!(io::stdout(), EnterAlternateScreen, Hide).map_err(io_err)?;
        Ok(Self)
    }
}

impl Drop for TermGuard {
    fn drop(&mut self) {
        let _ = execute!(io::stdout(), Show, LeaveAlternateScreen);
        let _ = disable_raw_mode();
    }
}

fn io_err(e: impl ToString) -> IpatoolError {
    IpatoolError::Message(e.to_string())
}

fn paint(lines: &[String]) -> Result<(), IpatoolError> {
    let mut out = io::stdout();
    let (cols, rows) = size().unwrap_or((80, 24));
    let cols = cols as usize;
    let max_rows = rows as usize;

    execute!(out, Clear(ClearType::All), MoveTo(0, 0)).map_err(io_err)?;

    for (row, line) in lines.iter().take(max_rows).enumerate() {
        execute!(out, MoveTo(0, row as u16)).map_err(io_err)?;
        let visible = truncate_for_term(line, cols.saturating_sub(1));
        execute!(out, Print(&visible), Print("\x1b[0m"), Print("\r")).map_err(io_err)?;
    }
    out.flush().map_err(io_err)?;
    Ok(())
}

fn truncate_for_term(s: &str, max_cols: usize) -> String {
    if max_cols == 0 {
        return String::new();
    }
    let mut out = String::new();
    let mut cols = 0usize;
    let mut chars = s.chars().peekable();
    let mut truncated = false;
    while let Some(c) = chars.next() {
        if c == '\u{1b}' {
            out.push(c);
            if chars.peek() == Some(&'[') {
                out.push(chars.next().unwrap());
                for c2 in chars.by_ref() {
                    out.push(c2);
                    if c2.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
            continue;
        }
        if cols >= max_cols {
            truncated = true;
            break;
        }
        out.push(c);
        cols += 1;
    }
    if truncated {
        out.push_str("\x1b[0m");
    }
    out
}

fn read_key() -> Result<KeyCode, IpatoolError> {
    Ok(read_key_event()?.code)
}

fn read_key_event() -> Result<KeyEvent, IpatoolError> {
    loop {
        let ev = event::read().map_err(io_err)?;
        if let Event::Key(k) = ev {
            if k.kind == KeyEventKind::Press {
                return Ok(k);
            }
        }
    }
}

/// Single-choice list. Full clear each key — never paints over scrollback.
pub fn select<S: AsRef<str>>(
    header: &[String],
    prompt: impl AsRef<str>,
    items: &[S],
) -> Result<usize, IpatoolError> {
    let prompt = prompt.as_ref();
    if items.is_empty() {
        return Err(IpatoolError::Message("empty select".into()));
    }
    let mut idx = 0usize;
    let view = visible_rows().saturating_sub(header.len() + 6).max(5);
    loop {
        let mut lines = header.to_vec();
        lines.push(String::new());
        lines.push(prompt.to_string());
        lines.push(String::new());

        let start = if items.len() <= view {
            0
        } else {
            idx.saturating_sub(view / 2)
                .min(items.len().saturating_sub(view))
        };
        let end = (start + view).min(items.len());
        if start > 0 {
            lines.push("  …".dim().to_string());
        }
        for (i, item) in items.iter().enumerate().take(end).skip(start) {
            let mark = if i == idx { "❯ " } else { "  " };
            let row = format!("{mark}{}", item.as_ref());
            lines.push(if i == idx {
                row.cyan().bold().to_string()
            } else {
                row
            });
        }
        if end < items.len() {
            lines.push("  …".dim().to_string());
        }
        lines.push(String::new());
        lines.push(crate::i18n::t("ui.hint_select").dim().to_string());
        paint(&lines)?;

        match read_key()? {
            KeyCode::Up | KeyCode::Char('k') => {
                idx = if idx == 0 { items.len() - 1 } else { idx - 1 };
            }
            KeyCode::Down | KeyCode::Char('j') => {
                idx = (idx + 1) % items.len();
            }
            KeyCode::Home => idx = 0,
            KeyCode::End => idx = items.len() - 1,
            KeyCode::Enter => return Ok(idx),
            KeyCode::Esc | KeyCode::Char('q') => {
                return Err(IpatoolError::Message("cancelled".into()));
            }
            _ => {}
        }
    }
}

/// Multi-select with live search. Returns indices into the original `items` slice.
pub fn multi_select(
    header: &[String],
    prompt: impl AsRef<str>,
    items: &[String],
) -> Result<Vec<usize>, IpatoolError> {
    let prompt = prompt.as_ref();
    if items.is_empty() {
        return Ok(Vec::new());
    }
    let mut query = String::new();
    let mut on = vec![false; items.len()];
    let mut idx = 0usize;

    loop {
        let q = query.to_lowercase();
        let visible: Vec<usize> = if q.is_empty() {
            (0..items.len()).collect()
        } else {
            items
                .iter()
                .enumerate()
                .filter(|(_, s)| s.to_lowercase().contains(&q))
                .map(|(i, _)| i)
                .collect()
        };
        if idx >= visible.len() {
            idx = visible.len().saturating_sub(1);
        }

        let selected_n = on.iter().filter(|&&v| v).count();
        let view = visible_rows().saturating_sub(header.len() + 9).max(5);

        let mut lines = header.to_vec();
        lines.push(String::new());
        lines.push(prompt.to_string());
        lines.push(format!("{}{query}▌", crate::i18n::t("ui.search").cyan()));
        lines.push(
            format!(
                "{} {}/{} · {} {selected_n}",
                crate::i18n::t("ui.shown"),
                visible.len(),
                items.len(),
                crate::i18n::t("ui.selected"),
            )
            .dim()
            .to_string(),
        );
        lines.push(String::new());

        if visible.is_empty() {
            lines.push(crate::i18n::t("ui.no_matches").dim().to_string());
        } else {
            let start = if visible.len() <= view {
                0
            } else {
                idx.saturating_sub(view / 2)
                    .min(visible.len().saturating_sub(view))
            };
            let end = (start + view).min(visible.len());
            if start > 0 {
                lines.push("  …".dim().to_string());
            }
            for (vi, &oi) in visible.iter().enumerate().take(end).skip(start) {
                let boxc = if on[oi] { "[x]" } else { "[ ]" };
                let mark = if vi == idx { "❯ " } else { "  " };
                let row = format!("{mark}{boxc} {}", items[oi]);
                lines.push(if vi == idx {
                    row.cyan().bold().to_string()
                } else {
                    row
                });
            }
            if end < visible.len() {
                lines.push("  …".dim().to_string());
            }
        }

        lines.push(String::new());
        lines.push(
            crate::i18n::t("ui.hint_multi")
            .dim()
            .to_string(),
        );
        paint(&lines)?;

        let key = read_key_event()?;
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);

        match key.code {
            KeyCode::Up => {
                if !visible.is_empty() {
                    idx = if idx == 0 { visible.len() - 1 } else { idx - 1 };
                }
            }
            KeyCode::Down => {
                if !visible.is_empty() {
                    idx = (idx + 1) % visible.len();
                }
            }
            KeyCode::PageUp => {
                idx = idx.saturating_sub(view);
            }
            KeyCode::PageDown => {
                if !visible.is_empty() {
                    idx = (idx + view).min(visible.len() - 1);
                }
            }
            KeyCode::Home => idx = 0,
            KeyCode::End => {
                if !visible.is_empty() {
                    idx = visible.len() - 1;
                }
            }
            KeyCode::Char(' ') => {
                if let Some(&oi) = visible.get(idx) {
                    on[oi] = !on[oi];
                }
            }
            KeyCode::Char('*') | KeyCode::Char('+') => {
                let all_on = !visible.is_empty() && visible.iter().all(|&oi| on[oi]);
                for &oi in &visible {
                    on[oi] = !all_on;
                }
            }
            KeyCode::Char('a') | KeyCode::Char('A') if ctrl => {
                let all_on = !visible.is_empty() && visible.iter().all(|&oi| on[oi]);
                for &oi in &visible {
                    on[oi] = !all_on;
                }
            }
            KeyCode::Char(c) if !c.is_control() && !ctrl => {
                query.push(c);
                idx = 0;
            }
            KeyCode::Backspace => {
                query.pop();
                idx = 0;
            }
            KeyCode::Enter => {
                return Ok(on
                    .iter()
                    .enumerate()
                    .filter_map(|(i, v)| v.then_some(i))
                    .collect());
            }
            KeyCode::Esc => {
                if query.is_empty() {
                    return Ok(Vec::new());
                }
                query.clear();
                idx = 0;
            }
            _ => {}
        }
    }
}

pub fn input_line(header: &[String], prompt: impl AsRef<str>) -> Result<String, IpatoolError> {
    let prompt = prompt.as_ref();
    let mut buf = String::new();
    loop {
        let mut lines = header.to_vec();
        lines.push(String::new());
        lines.push(format!("{prompt}{buf}▌"));
        lines.push(String::new());
        lines.push(
            crate::i18n::t("ui.hint_input")
            .dim()
            .to_string(),
        );
        paint(&lines)?;

        match read_key()? {
            KeyCode::Enter => return Ok(buf),
            KeyCode::Esc => return Ok(String::new()),
            KeyCode::Backspace => {
                buf.pop();
            }
            KeyCode::Char(c) => buf.push(c),
            _ => {}
        }
    }
}

pub fn input_password(header: &[String], prompt: impl AsRef<str>) -> Result<String, IpatoolError> {
    let prompt = prompt.as_ref();
    let mut buf = String::new();
    loop {
        let stars: String = "*".repeat(buf.chars().count());
        let mut lines = header.to_vec();
        lines.push(String::new());
        lines.push(format!("{prompt}{stars}▌"));
        lines.push(String::new());
        lines.push(
            crate::i18n::t("ui.hint_input")
            .dim()
            .to_string(),
        );
        paint(&lines)?;

        match read_key()? {
            KeyCode::Enter => return Ok(buf),
            KeyCode::Esc => return Ok(String::new()),
            KeyCode::Backspace => {
                buf.pop();
            }
            KeyCode::Char(c) => buf.push(c),
            _ => {}
        }
    }
}

pub fn message(header: &[String], body: impl AsRef<str>) -> Result<(), IpatoolError> {
    show_message(header, body.as_ref(), false)
}

/// Like [`message`], but also writes the full text to `~/.ipatool/last-error.txt`
/// and shows that path (for long Apple/HTTP failures).
pub fn error_message(header: &[String], body: impl AsRef<str>) -> Result<(), IpatoolError> {
    show_message(header, body.as_ref(), true)
}

fn show_message(header: &[String], body: &str, save_error: bool) -> Result<(), IpatoolError> {
    let saved = if save_error {
        persist_last_error(body)
    } else {
        None
    };

    let (cols, rows) = size().unwrap_or((80, 24));
    let cols = (cols as usize).saturating_sub(1).max(20);
    let max_rows = rows as usize;

    let mut content = Vec::new();
    if let Some(ref path) = saved {
        content.push(format!("full error saved: {}", path.display()));
        content.push(String::new());
    }
    for part in body.lines() {
        content.extend(wrap_plain(part, cols));
    }

    // Reserve header + blank + footer ("Enter…") + optional overflow note.
    let header_rows = header.len() + 1;
    let footer_rows = 2;
    let budget = max_rows.saturating_sub(header_rows + footer_rows).max(1);
    let overflow = content.len().saturating_sub(budget);
    let shown = if overflow > 0 {
        let mut s = content[..budget.saturating_sub(1)].to_vec();
        s.push(format!("… ({overflow} more lines — open the file above)"));
        s
    } else {
        content
    };

    let mut lines = header.to_vec();
    lines.push(String::new());
    lines.extend(shown);
    lines.push(String::new());
    lines.push(crate::i18n::t("ui.enter_continue").dim().to_string());
    paint(&lines)?;
    loop {
        match read_key()? {
            KeyCode::Enter | KeyCode::Esc | KeyCode::Char('q') => return Ok(()),
            _ => {}
        }
    }
}

fn persist_last_error(body: &str) -> Option<std::path::PathBuf> {
    let dir = ipatool_kit::session::ipatool_dir()?;
    let _ = std::fs::create_dir_all(&dir);
    let path = dir.join("last-error.txt");
    std::fs::write(&path, body).ok()?;
    Some(path)
}

fn wrap_plain(line: &str, width: usize) -> Vec<String> {
    if line.is_empty() {
        return vec![String::new()];
    }
    if width == 0 {
        return vec![line.to_string()];
    }
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut cols = 0usize;
    for c in line.chars() {
        if cols >= width {
            out.push(std::mem::take(&mut cur));
            cols = 0;
        }
        cur.push(c);
        cols += 1;
    }
    if !cur.is_empty() || out.is_empty() {
        out.push(cur);
    }
    out
}

/// Paint a status screen without waiting for a key (live progress).
pub fn status(header: &[String], body: impl AsRef<str>) -> Result<(), IpatoolError> {
    let body = body.as_ref();
    let mut lines = header.to_vec();
    lines.push(String::new());
    for part in body.lines() {
        lines.push(part.to_string());
    }
    lines.push(String::new());
    lines.push("…".dim().to_string());
    paint(&lines)
}

fn visible_rows() -> usize {
    size().map(|(_, r)| r as usize).unwrap_or(24)
}