Skip to content

Commit

Permalink
[tuify][cmdr] Move giti.rs from tuify to cmdr
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmulidris committed Dec 18, 2023
1 parent eab7750 commit 3e814e2
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 119 deletions.
16 changes: 9 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
### r3bl-cmdr first release
<a id="markdown-r3bl-cmdr-first-release" name="r3bl-cmdr-first-release"></a>

Added:
- Binary target for `giti`. This is an interactive git client that is tuified. It is a
productivity tool for git workflows, and is meant as a replacement for directly using
`git`. This also serves as a real world example of using the `r3bl_tuify` crate.
- View all the `giti branch` subcommands (e.g. `delete`, `checkout`, `new`, etc.) and
select one subcommand using the `select_from_list()` when `giti branch` runs.
- Delete one or more branches using `select_from_list()` when `giti branch delete`
command runs.

## `r3bl_tuify`
<a id="markdown-r3bl_tuify" name="r3bl_tuify"></a>

Expand All @@ -82,13 +91,6 @@
and Windows. Add many different themes to choose from.

- Added:
- Binary target for `giti`. This is an interactive git client that is tuified. It is a
productivity tool for git workflows, and is meant as a replacement for directly using
`git`. This also serves as a real world example of using the `r3bl_tuify` crate.
- View all the `giti branch` subcommands (e.g. `delete`, `checkout`, `new`, etc.) and
select one subcommand using the `select_from_list()` when `giti branch` runs.
- Delete one or more branches using `select_from_list()` when `giti branch delete`
command runs.
- `Ctrl + c` now behaves just like the `Escape` key. In the past, pressing `Ctrl + c`
would do nothing the user could not exit the app by pressing this shortcut.
- More code quality and ability to test the main event loop, by creating a new
Expand Down
24 changes: 22 additions & 2 deletions cmdr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,39 @@ documentation = "https://developerlife.com/category/Rust/"
# More info on default-run: https://stackoverflow.com/a/51790493/2085356
default-run = "rc"

[[bin]]
name = "giti"
path = "src/bin/giti.rs"

[[bin]]
name = "rc"
path = "src/bin/rc.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

# Tokio.
tokio = { version = "1.24.1", features = ["full"] }

# CLI argument parser.
clap = { version = "4.1.1", features = ["derive"] }
# Clap.
# More info: https://stackoverflow.com/a/76131914/2085356
clap = { version = "4.4.6", features = ["derive", "wrap_help"] }
reedline = "0.27.1"

# serde-json
serde_json = "1.0.91"

# R3BL crates (from this mono repo).
r3bl_ansi_color = { path = "../ansi_color", version = "0.6.9" }
r3bl_rs_utils_core = { path = "../core", version = "0.9.9" }
r3bl_tui = { path = "../tui", version = "0.3.10" }
r3bl_tuify = { path = "../tuify", version = "0.1.21" }

# Logging.
is-terminal = "0.4.9"
log = { version = "0.4.20", features = ["std"] }

[dev-dependencies]
pretty_assertions = "1.4.0"
serial_test = "2.0.0"
109 changes: 61 additions & 48 deletions cmdr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
# r3bl-cmdr
<a id="markdown-r3bl-cmdr" name="r3bl-cmdr"></a>


<!-- TOC -->

- [Context](#context)
- [This binary crate: r3bl-cmdr](#this-binary-crate-r3bl-cmdr)
- [Building & running locally](#building--running-locally)
- [Contributing](#contributing)
- [Run giti binary target](#run-giti-binary-target)
- [Run r3bl-cmdr or rc binary target](#run-r3bl-cmdr-or-rc-binary-target)
- [Build, run, test tasks](#build-run-test-tasks)
- [Prerequisites](#prerequisites)
- [Nu shell scripts to build, run, test etc.](#nu-shell-scripts-to-build-run-test-etc)

<!-- /TOC -->

## Context
<a id="markdown-context" name="context"></a>


<!-- R3BL TUI library & suite of apps focused on developer productivity -->

<span style="color:#FD2F53">R</span><span style="color:#FC2C57">3</span><span style="color:#FB295B">B</span><span style="color:#FA265F">L</span><span style="color:#F92363">
Expand Down Expand Up @@ -61,9 +61,19 @@ it.
2. integrations w/ github issues
3. integrations w/ calendar, email, contacts APIs

## This binary crate: r3bl-cmdr
<a id="markdown-this-binary-crate%3A-r3bl-cmdr" name="this-binary-crate%3A-r3bl-cmdr"></a>
## Run `giti` binary target
<a id="markdown-run-giti-binary-target" name="run-giti-binary-target"></a>

1. Move to `./tuify` folder in your terminal
2. Run `nu run.nu install` to install `giti` locally to `~/.cargo/bin`
3. Run `giti` from anywhere on your system
4. To delete one or more branches in your repo run `giti branch delete`

[![asciicast](https://asciinema.org/a/14V8v3OKKYvDkUDkRFiMDsCNg.svg)](https://asciinema.org/a/14V8v3OKKYvDkUDkRFiMDsCNg)

## Run `r3bl-cmdr` or `rc` binary target

<a id="markdown-run-r3bl-cmdr-or-rc-binary-target" name="run-r3bl-cmdr-or-rc-binary-target"></a>

`r3bl-cmdr` is the second thing that's described above. It contains a set of apps for developers by
developers. It is engineered to enhance your:
Expand All @@ -75,44 +85,47 @@ developers. It is engineered to enhance your:

Our goal is to put a smile on your face every time you use this product.

## Building & running locally
<a id="markdown-building-%26-running-locally" name="building-%26-running-locally"></a>


You can run it using `cargo run`. There are 2 other ways to launch it if you install it on your
machine both that involve a little less typing.

```sh
cargo install --path .
r3bl-cmdr # this is the same as `cargo run` or `cargo run --bin r3bl-cmdr`
rc # this is an alias for `r3bl-cmdr`, equivalent to `cargo run --bin rc``
```

The `Cargo.toml` file contains a `[dependencies]` section which lists all the dependencies that this
crate has, one of which is a
[path dependency](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#multiple-locations)
on the `r3bl_rs_utils` crate. The creates.io version uses the `r3bl_rs_utils` crate from the
crates.io repository, but your local copy will use the folder specified in the path.

> We plan to remove the `path` dependency when this crate is ready to be used by end users.
> Currently it early WIP so it has this dependency for ease of development.
So, to build and run this locally, you have to clone the
[r3bl_rs_utils](https://github.com/r3bl-org/r3bl_rs_utils) repo so that it shares the same parent as
this crate. Here's a sample folder structure.

```text
├── github
│   ├── r3bl-cmdr
│ └── r3bl-rs-utils
```

## Contributing
<a id="markdown-contributing" name="contributing"></a>


This binary crate is being developed as a set of examples. The actual product will emerge as these
examples are evolved into features of the actual product, which is intended to be released to
developers.

Please read our [community contributing guidelines here](./CONTRIBUTING.md).
## Build, run, test tasks
<a id="markdown-build%2C-run%2C-test-tasks" name="build%2C-run%2C-test-tasks"></a>

### Prerequisites
<a id="markdown-prerequisites" name="prerequisites"></a>

🌠 In order for these to work you have to install the Rust toolchain, `nu`, `cargo-watch`, `bat`,
and `flamegraph` on your system. Here are the instructions:

1. Install the Rust toolchain using `rustup` by following the instructions
[here](https://rustup.rs/).
1. Install `cargo-watch` using `cargo install cargo-watch`.
1. Install `flamegraph` using `cargo install flamegraph`.
1. Install `bat` using `cargo install bat`.
1. Install [`nu`](https://crates.io/crates/nu) shell on your system using `cargo install nu`. It is
available for Linux, macOS, and Windows.

### Nu shell scripts to build, run, test etc.
<a id="markdown-nu-shell-scripts-to-build%2C-run%2C-test-etc." name="nu-shell-scripts-to-build%2C-run%2C-test-etc."></a>

| Command | Description |
| ----------------------------------------- | ------------------------------------------------- |
| `nu run.nu run` | Run the default binary target |
| `nu run.nu install` | Install `giti` and `rc` locally to `~/.cargo/bin` |
| `nu run.nu build` | Build |
| `nu run.nu clean` | Clean |
| `nu run.nu all` | All |
| `nu run.nu run-with-flamegraph-profiling` | Run examples with flamegraph profiling |
| `nu run.nu test` | Run tests |
| `nu run.nu clippy` | Run clippy |
| `nu run.nu docs` | Build docs |
| `nu run.nu serve-docs` | Serve docs over VSCode Remote SSH session. |
| `nu run.nu upgrade-deps` | Upgrade deps |
| `nu run.nu rustfmt` | Run rustfmt |

The following commands will watch for changes in the source folder and re-run:

| Command | Description |
| ------------------------------------------------------ | ---------------------------------- |
| `nu run.nu watch-run` | Watch run |
| `nu run.nu watch-all-tests` | Watch all test |
| `nu run.nu watch-one-test <test_name>` | Watch one test |
| `nu run.nu watch-clippy` | Watch clippy |
| `nu run.nu watch-macro-expansion-one-test <test_name>` | Watch macro expansion for one test |
4 changes: 2 additions & 2 deletions cmdr/run.nu
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ def clean [] {
}

def run [] {
cargo run --example main_interactive -q
cargo run
}

def install [] {
cargo install --path .
cargo install --path . --force
}

def run-with-flamegraph [] {
Expand Down
18 changes: 14 additions & 4 deletions tuify/src/bin/giti.rs → cmdr/src/bin/giti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
//! For more information on how to use CLAP and Tuify, please read this tutorial:
//! <https://developerlife.com/2023/09/17/tuify-clap/>
use crate::giti::branch::delete::try_delete_branch;
use clap::{Args, Parser, Subcommand, ValueEnum};
use clap_config::*;
use r3bl_rs_utils_core::{call_if_true, log_debug, log_error, try_to_set_log_level, CommonResult};
use r3bl_tuify::{giti_ui_templates::ask_user_to_select_from_list, *};
use giti::{branch::delete::try_delete_branch,
giti_ui_templates::ask_user_to_select_from_list,
*};
use r3bl_cmdr::{giti, DEVELOPMENT_MODE};
use r3bl_rs_utils_core::{call_if_true,
log_debug,
log_error,
try_to_set_log_level,
CommonResult};
use r3bl_tuify::SelectionMode;

fn main() {
// If no args are passed, the following line will fail, and help will be printed
Expand Down Expand Up @@ -91,7 +98,8 @@ pub fn get_giti_command_subcommand_names(arg: CLICommand) -> Vec<String> {
CLICommand::Branch { .. } => BranchSubcommand::value_variants()
.iter()
.map(|subcommand| {
let lower_case_subcommand = format!("{:?}", subcommand).to_ascii_lowercase();
let lower_case_subcommand =
format!("{:?}", subcommand).to_ascii_lowercase();
lower_case_subcommand
})
.collect(),
Expand All @@ -102,6 +110,8 @@ pub fn get_giti_command_subcommand_names(arg: CLICommand) -> Vec<String> {
/// - <https://docs.rs/clap/latest/clap/_derive/#overview>
/// - <https://developerlife.com/2023/09/17/tuify-clap/>
mod clap_config {
use r3bl_tuify::SelectionMode;

use super::*;

#[derive(Debug, Parser)]
Expand Down
55 changes: 45 additions & 10 deletions tuify/src/giti/branch/delete.rs → cmdr/src/giti/branch/delete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
use self::giti_ui_templates::ask_user_to_select_from_list;
use crate::*;
/*
* Copyright (c) 2023 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::process::Command;

use r3bl_ansi_color::{AnsiStyledText, Color, Style};
use r3bl_rs_utils_core::{log_error, CommonError, CommonErrorType, CommonResult};
use std::process::Command;
use r3bl_tuify::{SelectionMode, DELETE_BRANCH, DELETE_BRANCHES, EXIT};

use self::giti_ui_templates::ask_user_to_select_from_list;

pub fn try_delete_branch() -> CommonResult<()> {
giti_ui_templates::multi_select_instruction_header();
Expand Down Expand Up @@ -51,13 +70,17 @@ pub fn try_delete_branch() -> CommonResult<()> {
match Selection::from(selected) {
Delete => {
let mut command =
inner::run_git_command_to_delete_branches_on_all_branches(&branches);
inner::run_git_command_to_delete_branches_on_all_branches(
&branches,
);
let output = command.output()?;
if output.status.success() {
if num_of_branches == 1 {
inner::display_one_branch_deleted_success_message(&branches);
} else {
inner::display_all_branches_deleted_success_messages(&branches);
inner::display_all_branches_deleted_success_messages(
&branches,
);
}
} else {
inner::display_correct_error_message(branches, output);
Expand All @@ -82,7 +105,8 @@ pub fn try_delete_branch() -> CommonResult<()> {

impl From<Vec<String>> for Selection {
fn from(selected: Vec<String>) -> Selection {
let selected_to_delete_one_branch = selected[0] == DELETE_BRANCH.to_string();
let selected_to_delete_one_branch =
selected[0] == DELETE_BRANCH.to_string();
let selected_to_delete_multiple_branches =
selected[0] == DELETE_BRANCHES.to_string();
let selected_to_exit = selected[0] == EXIT.to_string();
Expand All @@ -99,9 +123,14 @@ pub fn try_delete_branch() -> CommonResult<()> {
}

mod inner {
use r3bl_tuify::{FAILED_COLOR, LIGHT_GRAY_COLOR, SUCCESS_COLOR};

use super::*;

pub fn display_correct_error_message(branches: Vec<String>, output: std::process::Output) {
pub fn display_correct_error_message(
branches: Vec<String>,
output: std::process::Output,
) {
if branches.len() == 1 {
let branch = &branches[0];
AnsiStyledText {
Expand Down Expand Up @@ -199,9 +228,14 @@ pub fn try_execute_git_command_to_get_branches() -> CommonResult<Vec<String>> {
}

pub mod giti_ui_templates {
use r3bl_rs_utils_core::{ch, ChUnit};
use r3bl_tuify::{get_size,
select_from_list,
StyleSheet,
DUSTY_LIGHT_BLUE_COLOR,
LIGHT_GRAY_COLOR};

use super::*;
use r3bl_rs_utils_core::ch;
use r3bl_rs_utils_core::ChUnit;

pub fn multi_select_instruction_header() {
AnsiStyledText {
Expand Down Expand Up @@ -236,7 +270,8 @@ pub mod giti_ui_templates {
selection_mode: SelectionMode,
) -> Option<Vec<String>> {
let max_height_row_count = 20;
let max_width_col_count = get_size().map(|it| it.col_count).unwrap_or(ch!(80)).into();
let max_width_col_count =
get_size().map(|it| it.col_count).unwrap_or(ch!(80)).into();
let style = StyleSheet::default();
let user_input = select_from_list(
header,
Expand Down
19 changes: 19 additions & 0 deletions cmdr/src/giti/branch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub mod delete;
pub use delete::*;
Loading

0 comments on commit 3e814e2

Please sign in to comment.