r3bl_tui 0.7.7

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright (c) 2025 R3BL LLC. Licensed under Apache License, Version 2.0.

use crate::{InlineString, ItemsOwned, ResultAndCommand, Run, command,
            script::git::types::{LocalBranchInfo,
                                 git_command_args::{GIT_ARG_CREATE_BRANCH,
                                                    GIT_ARG_DELETE_FORCE,
                                                    GIT_ARG_FORMAT,
                                                    GIT_ARG_REFNAME_SHORT,
                                                    GIT_ARG_SHOW_CURRENT},
                                 git_command_names::{GIT_CMD_BRANCH, GIT_CMD_CHECKOUT,
                                                     GIT_PROGRAM}}};

pub async fn try_get_current_branch_name() -> ResultAndCommand<InlineString> {
    let mut cmd = command!(
        program => GIT_PROGRAM,
        args => GIT_CMD_BRANCH, GIT_ARG_SHOW_CURRENT,
    );

    let res_output = cmd.run().await;
    let Ok(output) = res_output else {
        let report = res_output.unwrap_err();
        let err = Err(report);
        return (err, cmd);
    };

    let current_branch = String::from_utf8_lossy(&output)
        .trim_end_matches('\n')
        .to_string();

    (Ok(current_branch.into()), cmd)
}

pub async fn try_checkout_existing_local_branch(
    branch_name: &str,
) -> ResultAndCommand<()> {
    let mut cmd = command!(
        program => GIT_PROGRAM,
        args => GIT_CMD_CHECKOUT, branch_name
    );

    let res_output = cmd.run().await;
    let Ok(_) = res_output else {
        let report = res_output.unwrap_err();
        let err = Err(report);
        return (err, cmd);
    };

    (Ok(()), cmd)
}

pub async fn try_create_and_switch_to_branch(branch_name: &str) -> ResultAndCommand<()> {
    let mut cmd = command!(
        program => GIT_PROGRAM,
        args => GIT_CMD_CHECKOUT, GIT_ARG_CREATE_BRANCH, branch_name
    );

    let res_output = cmd.run().await;
    let Ok(_) = res_output else {
        let report = res_output.unwrap_err();
        let err = Err(report);
        return (err, cmd);
    };

    (Ok(()), cmd)
}

pub async fn try_delete_branches(branches: &ItemsOwned) -> ResultAndCommand<()> {
    let mut cmd = command!(
        program => GIT_PROGRAM,
        args => GIT_CMD_BRANCH, GIT_ARG_DELETE_FORCE,
        + items => branches
    );

    let res_output = cmd.run().await;
    let Ok(_) = res_output else {
        let report = res_output.unwrap_err();
        let err = Err(report);
        return (err, cmd);
    };

    (Ok(()), cmd)
}

/// Get all the local branches as a tuple.
///
/// 1. The first item in the tuple contains the current branch is prefixed with
///    `CURRENT_BRANCH_PREFIX`.
///
///   ```text
///   [
///     "(◕‿◕) main",
///     "tuifyasync",
///   ]
///   ```
///
/// 2. The second item in the tuple contains [`LocalBranchInfo`].
pub async fn try_get_local_branches() -> ResultAndCommand<(ItemsOwned, LocalBranchInfo)> {
    let (res, cmd) = try_get_branch_info().await;
    let Ok(info) = res else {
        let report = res.unwrap_err();
        return (Err(report), cmd);
    };

    let mut items_owned = ItemsOwned::with_capacity(info.other_branches.len() + 1);

    // Add current branch with prefix.
    items_owned.push(LocalBranchInfo::mark_branch_current(&info.current_branch));

    // Add other branches as is.
    items_owned.extend(info.other_branches.clone());

    let tuple = (items_owned, info);

    (Ok(tuple), cmd)
}

/// Returns information about local git branches:
/// 1. The currently checked out branch.
/// 2. List of other local branches (excluding the current one).
async fn try_get_branch_info() -> ResultAndCommand<LocalBranchInfo> {
    // Get all branches first.
    let (res, cmd) = try_execute_git_command_to_get_branches().await;
    let Ok(all_branches) = res else {
        let report = res.unwrap_err();
        return (Err(report), cmd);
    };

    // Get current branch.
    let (res, _cmd) = try_get_current_branch_name().await;
    let Ok(current_branch) = res else {
        let report = res.unwrap_err();
        return (Err(report), cmd);
    };

    // Filter out current branch from all branches to get other branches.
    let other_branches = all_branches
        .into_iter()
        .filter(|branch| branch != &current_branch)
        .collect();

    let info = LocalBranchInfo {
        current_branch,
        other_branches,
    };

    (Ok(info), cmd)
}

pub(super) async fn try_execute_git_command_to_get_branches()
-> ResultAndCommand<ItemsOwned> {
    let mut cmd = command!(
        program => GIT_PROGRAM,
        args => GIT_CMD_BRANCH, GIT_ARG_FORMAT, GIT_ARG_REFNAME_SHORT,
    );

    let res_output = cmd.run().await;
    let Ok(output) = res_output else {
        let report = res_output.unwrap_err();
        let err = Err(report);
        return (err, cmd);
    };

    let output_string = String::from_utf8_lossy(&output);
    let mut branches = ItemsOwned::with_capacity(output_string.lines().count());
    for line in output_string.lines() {
        branches.push(line.into());
    }
    (Ok(branches), cmd)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{command, inline_vec, ok,
                script::git::{test_fixtures::helper_setup_git_repo_with_commit,
                              types::{BranchExists,
                                      git_ui_strings::CURRENT_BRANCH_PREFIX,
                                      test_config::TEST_ENV_ISOLATED_TEST_RUNNER}},
                try_create_temp_dir_and_cd, with_saved_pwd};

    async fn test_try_get_current_branch_name() -> miette::Result<()> {
        with_saved_pwd!(async {
            // Create an empty temp dir to verify that running the command fails.
            {
                // Create a temp dir and cd to it.
                let _temp_dir_root = try_create_temp_dir_and_cd!();

                // Assert that running command will error out before `git init` is run.
                assert!(try_get_current_branch_name().await.0.is_err());
            } // Drop temp_dir_root here (which cleans up that folder).

            // Setup new git folder.
            {
                let (
                    /* don't drop this immediately using `_` */ _temp_dir_root,
                    initial_branch_name,
                ) = helper_setup_git_repo_with_commit().await?;

                // Get initial branch name.
                let name = try_get_current_branch_name().await.0?;
                assert_eq!(name, initial_branch_name);

                // Create and switch to a new branch.
                let _unused: Vec<_> = command!(program => GIT_PROGRAM, args => "checkout", "-b", "feature-branch")
                    .run()
                    .await?;

                // Get current branch name after switch.
                let new_feature_branch = try_get_current_branch_name().await.0?;

                // Verify branch name has changed.
                assert_eq!(new_feature_branch, "feature-branch");
                assert_ne!(new_feature_branch, initial_branch_name);
            } // Drop _temp_dir_root here (which cleans up that folder).

            ok!(())
        })
    }

    async fn test_try_checkout_existing_local_branch() -> miette::Result<()> {
        with_saved_pwd!(async {
            let (
                /* don't drop this immediately using `_` */ _temp_dir_root,
                initial_branch,
            ) = helper_setup_git_repo_with_commit().await?;

            // Create a new branch (without switching to it).
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "test-branch")
                    .run()
                    .await?;

            // Checkout the test branch.
            let res = try_checkout_existing_local_branch("test-branch").await.0;
            assert!(res.is_ok());

            // Get current branch after checkout.
            let current_branch = try_get_current_branch_name().await.0?;

            // Verify current branch is now the test branch.
            assert_eq!(current_branch, "test-branch");
            assert_ne!(current_branch, initial_branch);

            // Try to checkout a non-existent branch (should fail).
            let res = try_checkout_existing_local_branch("nonexistent-branch")
                .await
                .0;
            assert!(res.is_err());

            ok!(())
        })
    }

    async fn test_try_create_and_switch_to_branch() -> miette::Result<()> {
        with_saved_pwd!(async {
            let (
                /* don't drop this immediately using `_` */ _temp_dir_root,
                initial_branch,
            ) = helper_setup_git_repo_with_commit().await?;

            // Create a new branch and switch to it.
            let res = try_create_and_switch_to_branch("new-feature").await.0;
            assert!(res.is_ok());

            // Get current branch after creating and switching.
            let current_branch = try_get_current_branch_name().await.0?;

            // Verify current branch is now the new branch.
            assert_eq!(current_branch, "new-feature");
            assert_ne!(current_branch, initial_branch);

            // Ensure the branch exists in the list of branches.
            let (_, branch_info) = crate::try_get_local_branches().await.0?;
            assert_eq!(
                branch_info.exists_locally("new-feature"),
                crate::BranchExists::Yes
            );

            ok!(())
        })
    }

    async fn test_try_delete_branches() -> miette::Result<()> {
        with_saved_pwd!(async {
            let (
                /* don't drop this immediately using `_` */ _temp_dir_root,
                initial_branch,
            ) = helper_setup_git_repo_with_commit().await?;

            // Should fail to delete the current branch.
            let res = try_delete_branches(&initial_branch.into()).await.0;
            assert!(res.is_err());

            // Create some branches.
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch1")
                    .run()
                    .await?;
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch2")
                    .run()
                    .await?;
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch3")
                    .run()
                    .await?;

            // Verify branches exist.
            let (_, branch_info) = crate::try_get_local_branches().await.0?;

            assert_eq!(branch_info.exists_locally("main"), crate::BranchExists::Yes);

            assert_eq!(
                branch_info.exists_locally("branch1"),
                crate::BranchExists::Yes
            );
            assert_eq!(
                branch_info.exists_locally("branch2"),
                crate::BranchExists::Yes
            );
            assert_eq!(
                branch_info.exists_locally("branch3"),
                crate::BranchExists::Yes
            );

            // Delete branches.
            let res = try_delete_branches(&inline_vec!["branch1", "branch2"].into())
                .await
                .0;
            assert!(res.is_ok());

            // Verify branches are deleted.
            let (_, branch_info) = crate::try_get_local_branches().await.0?;

            assert_eq!(
                branch_info.exists_locally("branch1"),
                crate::BranchExists::No
            );
            assert_eq!(
                branch_info.exists_locally("branch2"),
                crate::BranchExists::No
            );
            assert_eq!(
                branch_info.exists_locally("branch3"),
                crate::BranchExists::Yes
            );

            ok!(())
        })
    }

    async fn test_try_get_local_branches() -> miette::Result<()> {
        with_saved_pwd!(async {
            let (
                /* don't drop this immediately using `_` */ _temp_dir_root,
                initial_branch,
            ) = helper_setup_git_repo_with_commit().await?;

            // Create some branches.
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch1")
                    .run()
                    .await?;
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch2")
                    .run()
                    .await?;

            // Get local branches.
            let (items_owned, branch_info) = try_get_local_branches().await.0?;
            // Verify `items_owned` list is correct.
            {
                // Verify current branch is the same as the initial branch.
                assert_eq!(branch_info.current_branch, initial_branch);

                // Verify current branch is marked correctly and is in `items_owned`.
                assert!(items_owned.contains(&LocalBranchInfo::mark_branch_current(
                    initial_branch.as_str()
                )));

                // Verify other branches are in the list.
                assert!(items_owned.iter().any(|branch| branch == "branch1"));
                assert!(items_owned.iter().any(|branch| branch == "branch2"));
            }

            // Verify all branches are in the list.
            assert_eq!(
                branch_info.exists_locally(initial_branch.as_str()),
                BranchExists::Yes
            );
            assert_eq!(branch_info.exists_locally("branch1"), BranchExists::Yes);
            assert_eq!(branch_info.exists_locally("branch2"), BranchExists::Yes);

            // Switch to another branch.
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "checkout", "branch1")
                    .run()
                    .await?;

            // Get local branches again.
            let (items_owned, branch_info) = try_get_local_branches().await.0?;
            {
                // Verify the current branch is now "branch1".
                assert_eq!(branch_info.current_branch.as_str(), "branch1");

                // Verify the marked current branch in items_owned contains "branch1".
                assert!(
                    items_owned
                        .contains(&LocalBranchInfo::mark_branch_current("branch1"))
                );

                // Verify other branches are in the list.
                assert!(items_owned.iter().any(|branch| branch == "main"));
                assert!(items_owned.iter().any(|branch| branch == "branch2"));
            }

            ok!(())
        })
    }

    async fn test_local_branch_info_methods() -> miette::Result<()> {
        with_saved_pwd!(async {
            // Test exists_locally method.
            let branch_info = LocalBranchInfo {
                current_branch: "main".into(),
                other_branches: (&["develop", "feature/x"]).into(),
            };

            assert_eq!(branch_info.exists_locally("main"), BranchExists::Yes);
            assert_eq!(branch_info.exists_locally("develop"), BranchExists::Yes);
            assert_eq!(branch_info.exists_locally("feature/x"), BranchExists::Yes);
            assert_eq!(branch_info.exists_locally("nonexistent"), BranchExists::No);

            // Test mark_branch_current method.
            let marked = LocalBranchInfo::mark_branch_current("main");
            assert_eq!(marked, format!("{CURRENT_BRANCH_PREFIX} main"));

            // Test trim_current_prefix_from_branch method.
            let formatted = LocalBranchInfo::mark_branch_current("main");
            let trimmed = LocalBranchInfo::trim_current_prefix_from_branch(&formatted);
            assert_eq!(trimmed, "main");

            // Test trim_current_prefix_from_branch doesn't affect strings without prefix.
            let unchanged = LocalBranchInfo::trim_current_prefix_from_branch("develop");
            assert_eq!(unchanged, "develop");

            ok!(())
        })
    }

    async fn test_try_execute_git_command_to_get_branches() -> miette::Result<()> {
        with_saved_pwd!(async {
            let (
                /* don't drop this immediately using `_` */ _temp_dir_root,
                initial_branch,
            ) = helper_setup_git_repo_with_commit().await?;

            // Create some branches.
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch1")
                    .run()
                    .await?;
            let _unused: Vec<_> =
                command!(program => GIT_PROGRAM, args => "branch", "branch2")
                    .run()
                    .await?;

            // Get all branches.
            let branches = try_execute_git_command_to_get_branches().await.0?;

            // Verify all branches are listed.
            assert!(branches.iter().any(|b| b == &initial_branch));
            assert!(branches.iter().any(|b| b == "branch1"));
            assert!(branches.iter().any(|b| b == "branch2"));
            assert_eq!(branches.len(), 3); // initial + 2 created branches

            ok!(())
        })
    }

    async fn run_branch_ops_tests() -> miette::Result<()> {
        test_try_get_current_branch_name().await?;
        test_try_checkout_existing_local_branch().await?;
        test_try_create_and_switch_to_branch().await?;
        test_try_delete_branches().await?;
        test_try_get_local_branches().await?;
        test_local_branch_info_methods().await?;
        test_try_execute_git_command_to_get_branches().await?;
        ok!(())
    }

    #[tokio::test]
    async fn test_branch_ops_in_isolated_process() {
        if std::env::var(TEST_ENV_ISOLATED_TEST_RUNNER).is_ok() {
            if let Err(err) = run_branch_ops_tests().await {
                eprintln!("Test failed with error: {err}");
                std::process::exit(1);
            }
            std::process::exit(0);
        }

        let current_exe = std::env::current_exe().unwrap();
        let mut cmd = std::process::Command::new(&current_exe);
        cmd.env(TEST_ENV_ISOLATED_TEST_RUNNER, "1")
            .env("RUST_BACKTRACE", "1")
            .args([
                "--test-threads",
                "1",
                "--nocapture",
                "test_branch_ops_in_isolated_process",
            ]);

        let output = cmd.output().expect("Failed to run isolated test");
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();

        if !output.status.success()
            || stderr.contains("panicked at")
            || stderr.contains("Test failed with error")
        {
            eprintln!("Exit status: {:?}", output.status);
            eprintln!("Stdout: {}", String::from_utf8_lossy(&output.stdout));
            eprintln!("Stderr: {stderr}");

            panic!(
                "Isolated test failed with status code {:?}: {}",
                output.status.code(),
                stderr
            );
        }
    }
}