par-term 0.28.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
//! CLI install, uninstall, and self-update procedure implementations.
//!
//! These functions are invoked by [`super::process_cli`] when the user runs
//! install/uninstall/self-update subcommands. Each follows a
//! prompt-confirm-execute-report pattern and is the only place in the codebase
//! that uses blocking stdin reads.

use std::io::{self, Write};

use crate::config::ShellType;
use crate::shader_installer;
use crate::shell_integration_installer;

/// Install shaders from the latest GitHub release (CLI version with prompts and output)
pub fn install_shaders_cli(skip_prompt: bool) -> anyhow::Result<()> {
    let shaders_dir = crate::config::Config::shaders_dir();

    println!("=============================================");
    println!("  par-term Shader Installer");
    println!("=============================================");
    println!();
    println!("Target directory: {}", shaders_dir.display());
    println!();

    // Check if directory has existing shaders
    if shaders_dir.exists() && shader_installer::has_shader_files(&shaders_dir) && !skip_prompt {
        println!("WARNING: This will overwrite existing shaders in:");
        println!("  {}", shaders_dir.display());
        println!();
        print!("Do you want to continue? [y/N] ");
        io::stdout().flush()?;

        let mut response = String::new();
        io::stdin().read_line(&mut response)?;
        let response = response.trim().to_lowercase();

        if response != "y" && response != "yes" {
            println!("Installation cancelled.");
            return Ok(());
        }
        println!();
    }

    // Fetch latest release info
    println!("Fetching latest release information...");

    const REPO: &str = "paulrobello/par-term";
    let api_url = format!("https://api.github.com/repos/{}/releases/latest", REPO);
    let (zip_url, checksum_url) = shader_installer::get_shaders_download_url(&api_url, REPO)
        .map_err(|e| anyhow::anyhow!(e))?;

    println!("Downloading shaders from: {}", zip_url);
    println!();

    // Download the zip file (with optional checksum verification)
    let zip_data = shader_installer::download_and_verify(&zip_url, checksum_url.as_deref())
        .map_err(|e| anyhow::anyhow!(e))?;

    // Create shaders directory if it doesn't exist
    std::fs::create_dir_all(&shaders_dir)?;

    // Extract shaders
    println!("Extracting shaders to {}...", shaders_dir.display());
    shader_installer::extract_shaders(&zip_data, &shaders_dir).map_err(|e| anyhow::anyhow!(e))?;

    // Count installed shaders
    let shader_count = shader_installer::count_shader_files(&shaders_dir);

    println!();
    println!("=============================================");
    println!("  Installation complete!");
    println!("=============================================");
    println!();
    println!("Installed {} shaders to:", shader_count);
    println!("  {}", shaders_dir.display());
    println!();
    println!("To use a shader, add to your config.yaml:");
    println!("  custom_shader: \"shader_name.glsl\"");
    println!("  custom_shader_enabled: true");
    println!();
    println!("For cursor shaders:");
    println!("  cursor_shader: \"cursor_glow.glsl\"");
    println!("  cursor_shader_enabled: true");
    println!();
    println!("See docs/SHADERS.md for the full shader gallery.");

    Ok(())
}

/// Install shell integration for the specified or detected shell (CLI version)
pub fn install_shell_integration_cli(shell: Option<ShellType>) -> anyhow::Result<()> {
    let detected = shell_integration_installer::detected_shell();
    let target_shell = shell.unwrap_or(detected);

    println!("=============================================");
    println!("  par-term Shell Integration Installer");
    println!("=============================================");
    println!();

    if target_shell == ShellType::Unknown {
        eprintln!("Error: Could not detect shell type.");
        eprintln!("Please specify your shell with --shell bash|zsh|fish");
        return Err(anyhow::anyhow!("Unknown shell type"));
    }

    println!("Detected shell: {:?}", target_shell);

    // Check if already installed
    if shell_integration_installer::is_installed() {
        println!("Shell integration is already installed.");
        print!("Do you want to reinstall? [y/N] ");
        io::stdout().flush()?;

        let mut response = String::new();
        io::stdin().read_line(&mut response)?;
        let response = response.trim().to_lowercase();

        if response != "y" && response != "yes" {
            println!("Installation cancelled.");
            return Ok(());
        }
        println!();
    }

    println!("Installing shell integration...");

    match shell_integration_installer::install(Some(target_shell)) {
        Ok(result) => {
            println!();
            println!("=============================================");
            println!("  Installation complete!");
            println!("=============================================");
            println!();
            println!("Script installed to:");
            println!("  {}", result.script_path.display());
            println!();
            println!("Added source line to:");
            println!("  {}", result.rc_file.display());
            println!();
            if result.needs_restart {
                println!("Please restart your shell or run:");
                println!("  source {}", result.rc_file.display());
            }
            Ok(())
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            Err(anyhow::anyhow!(e))
        }
    }
}

/// Uninstall shell integration (CLI version)
pub fn uninstall_shell_integration_cli() -> anyhow::Result<()> {
    println!("=============================================");
    println!("  par-term Shell Integration Uninstaller");
    println!("=============================================");
    println!();

    if !shell_integration_installer::is_installed() {
        println!("Shell integration is not installed.");
        return Ok(());
    }

    println!("Uninstalling shell integration...");

    match shell_integration_installer::uninstall() {
        Ok(result) => {
            println!();
            println!("=============================================");
            println!("  Uninstallation complete!");
            println!("=============================================");
            println!();

            if !result.cleaned.is_empty() {
                println!("Cleaned RC files:");
                for path in &result.cleaned {
                    println!("  {}", path.display());
                }
                println!();
            }

            if !result.scripts_removed.is_empty() {
                println!("Removed integration scripts:");
                for path in &result.scripts_removed {
                    println!("  {}", path.display());
                }
                println!();
            }

            if !result.needs_manual.is_empty() {
                println!("WARNING: Some files need manual cleanup:");
                for path in &result.needs_manual {
                    println!("  {}", path.display());
                }
                println!();
            }

            Ok(())
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            Err(anyhow::anyhow!(e))
        }
    }
}

/// Uninstall shaders using manifest (CLI version)
pub fn uninstall_shaders_cli(force: bool) -> anyhow::Result<()> {
    let shaders_dir = crate::config::Config::shaders_dir();

    println!("=============================================");
    println!("  par-term Shader Uninstaller");
    println!("=============================================");
    println!();
    println!("Shaders directory: {}", shaders_dir.display());
    println!();

    if !shaders_dir.exists() {
        println!("No shaders installed.");
        return Ok(());
    }

    // Check for manifest
    let manifest_path = shaders_dir.join("manifest.json");
    if !manifest_path.exists() {
        println!("No manifest.json found. Cannot determine which files are bundled.");
        println!("Only files installed with the installer can be safely uninstalled.");
        return Err(anyhow::anyhow!("No manifest found"));
    }

    if !force {
        println!("This will remove bundled shader files.");
        println!("User-created and modified files will be preserved.");
        println!();
        print!("Do you want to continue? [y/N] ");
        io::stdout().flush()?;

        let mut response = String::new();
        io::stdin().read_line(&mut response)?;
        let response = response.trim().to_lowercase();

        if response != "y" && response != "yes" {
            println!("Uninstallation cancelled.");
            return Ok(());
        }
        println!();
    }

    println!("Uninstalling shaders...");

    match shader_installer::uninstall_shaders(force) {
        Ok(result) => {
            println!();
            println!("=============================================");
            println!("  Uninstallation complete!");
            println!("=============================================");
            println!();
            println!("Removed {} bundled files.", result.removed);

            if result.kept > 0 {
                println!("Preserved {} user files.", result.kept);
            }

            if !result.needs_confirmation.is_empty() {
                println!();
                println!("Modified files that were preserved:");
                for path in &result.needs_confirmation {
                    println!("  {}", path);
                }
            }

            Ok(())
        }
        Err(e) => {
            eprintln!("Error: {}", e);
            Err(anyhow::anyhow!(e))
        }
    }
}

/// Self-update par-term to the latest version (CLI version)
pub fn self_update_cli(skip_prompt: bool) -> anyhow::Result<()> {
    use crate::self_updater;
    use crate::update_checker;

    println!("=============================================");
    println!("  par-term Self-Updater");
    println!("=============================================");
    println!();

    let current_version = env!("CARGO_PKG_VERSION");
    println!("Current version: {}", current_version);

    // Detect installation type
    let installation = self_updater::detect_installation();
    println!("Installation type: {}", installation.description());
    println!();

    // Check for managed installations early
    match &installation {
        self_updater::InstallationType::Homebrew => {
            println!("par-term is installed via Homebrew.");
            println!("Please update with:");
            println!("  brew upgrade --cask par-term");
            return Err(anyhow::anyhow!("Cannot self-update Homebrew installation"));
        }
        self_updater::InstallationType::CargoInstall => {
            println!("par-term is installed via cargo.");
            println!("Please update with:");
            println!("  cargo install par-term");
            return Err(anyhow::anyhow!("Cannot self-update cargo installation"));
        }
        _ => {}
    }

    // Check for updates
    println!("Checking for updates...");
    let release_info = update_checker::fetch_latest_release().map_err(|e| anyhow::anyhow!(e))?;

    let latest_version = release_info
        .version
        .strip_prefix('v')
        .unwrap_or(&release_info.version);

    let current = semver::Version::parse(current_version)?;
    let latest = semver::Version::parse(latest_version)?;

    if latest <= current {
        println!();
        println!(
            "You are already running the latest version ({}).",
            current_version
        );
        return Ok(());
    }

    println!();
    println!(
        "New version available: {} -> {}",
        current_version, latest_version
    );
    if let Some(ref notes) = release_info.release_notes {
        println!();
        println!("Release notes:");
        // Show first few lines of release notes
        for line in notes.lines().take(10) {
            println!("  {}", line);
        }
        if notes.lines().count() > 10 {
            println!("  ...");
        }
    }
    println!();

    // Confirm unless --yes
    if !skip_prompt {
        print!("Do you want to update? [y/N] ");
        io::stdout().flush()?;

        let mut response = String::new();
        io::stdin().read_line(&mut response)?;
        let response = response.trim().to_lowercase();

        if response != "y" && response != "yes" {
            println!("Update cancelled.");
            return Ok(());
        }
        println!();
    }

    println!("Downloading and installing update...");

    match self_updater::perform_update(latest_version, crate::VERSION) {
        Ok(result) => {
            println!();
            println!("=============================================");
            println!("  Update complete!");
            println!("=============================================");
            println!();
            println!("Updated: {} -> {}", result.old_version, result.new_version);
            println!("Location: {}", result.install_path.display());
            if result.needs_restart {
                println!();
                println!("Please restart par-term to use the new version.");
            }
            Ok(())
        }
        Err(e) => {
            eprintln!("Update failed: {}", e);
            Err(anyhow::anyhow!(e))
        }
    }
}

/// Install both shaders and shell integration (CLI version)
pub fn install_integrations_cli(skip_prompt: bool) -> anyhow::Result<()> {
    println!("=============================================");
    println!("  par-term Integrations Installer");
    println!("=============================================");
    println!();
    println!("This will install:");
    println!("  1. Shader collection from latest release");
    println!("  2. Shell integration for your current shell");
    println!();

    if !skip_prompt {
        print!("Do you want to continue? [y/N] ");
        io::stdout().flush()?;

        let mut response = String::new();
        io::stdin().read_line(&mut response)?;
        let response = response.trim().to_lowercase();

        if response != "y" && response != "yes" {
            println!("Installation cancelled.");
            return Ok(());
        }
        println!();
    }

    // Install shaders
    println!("Step 1: Installing shaders...");
    println!("---------------------------------------------");

    let shader_result = install_shaders_cli(true);
    if shader_result.is_err() {
        println!();
        println!("WARNING: Shader installation failed.");
        println!("Continuing with shell integration...");
    }

    println!();
    println!("Step 2: Installing shell integration...");
    println!("---------------------------------------------");

    let shell_result = install_shell_integration_cli(None);

    println!();
    println!("=============================================");
    println!("  Integrations Installation Summary");
    println!("=============================================");
    println!();

    match (&shader_result, &shell_result) {
        (Ok(()), Ok(())) => {
            println!("All integrations installed successfully!");
        }
        (Err(_), Ok(())) => {
            println!("Shell integration: INSTALLED");
            println!("Shaders: FAILED (see above for errors)");
        }
        (Ok(()), Err(_)) => {
            println!("Shaders: INSTALLED");
            println!("Shell integration: FAILED (see above for errors)");
        }
        (Err(_), Err(_)) => {
            println!("Both installations failed. See above for errors.");
        }
    }

    // Return success if at least one succeeded
    if shader_result.is_ok() || shell_result.is_ok() {
        Ok(())
    } else {
        Err(anyhow::anyhow!("Both installations failed"))
    }
}