Skip to content

Commit

Permalink
feat: ready for 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitestack committed Mar 23, 2024
1 parent 088dda1 commit 5083bb1
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 181 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ description = "A CLI to easily switch between multiple Neovim configuration envi
readme = "README.md"
keywords = ["neovim", "nvim", "switcher", "cli"]
categories = ["command-line-utilities"]
homepage = "https://github.com/Nitestack/nvim-switcher"
repository = "https://github.com/Nitestack/nvim-switcher"
license = "Apache-2.0"
authors = ["Nitestack"]

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

Expand All @@ -15,6 +18,7 @@ clap = { version = "4.5.3", features = ["derive"] }
cliclack = "0.1.13"
colored = "2.1.0"
console = "0.15.8"
ctrlc = "3.4.4"
dirs = "5.0.1"
merge = "0.1.0"
prettytable-rs = "0.10.0"
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# Neovim Configuration Switcher
Neovim Configuration Switcher (short `nvims`) is a CLI to easily switch between multiple Neovim configuration environments.

## 🧭 Features
- Use fuzzy search to switch between Neovim configurations
- Add and remove Neovim configurations
## 📦 Requirements
- [rustup](https://rustup.rs)
- [git](https://git-scm.com/downloads)
- [Neovim](https://github.com/neovim/neovim/blob/master/INSTALL.md)
- [fzf](https://github.com/junegunn/fzf)

## 🔧 Installation

### crates.io
```sh
cargo install nvim-switcher
```

### GitHub
```sh
cargo install --git https://github.com/Nitestack/nvim-switcher
```

## 🚀 Usage

Use `nvims --help` to see all available options.

## 📖 License
This project is licensed under the Apache-2.0 license.
21 changes: 10 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
cmds::*,
utils::{is_neovim_installed, print_outro_cancel},
};
use crate::{cmds, utils};
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
Expand All @@ -24,18 +21,20 @@ enum Commands {
}

pub fn run_cli() {
utils::print_intro(None);
let cli = Cli::parse();

if !is_neovim_installed() {
print_outro_cancel("Neovim not installed! Please install Neovim and try again.");
return;
if !utils::is_neovim_installed() {
utils::print_outro_cancel(Some(
"Neovim not installed! Please install Neovim and try again.",
));
}

match &cli.command {
Some(Commands::Add {}) => add::add_config(),
Some(Commands::Remove {}) => remove::remove_config(),
Some(Commands::Add {}) => cmds::add::add_config(),
Some(Commands::Remove {}) => cmds::remove::remove_config(),
Some(Commands::Config {}) => {}
Some(Commands::List {}) => list::list_configs(),
None => select::select_config(),
Some(Commands::List {}) => cmds::list::list_configs(),
None => cmds::select::select_config(),
}
}
65 changes: 41 additions & 24 deletions src/cmds/add.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use cliclack::{input, spinner};
use cliclack;
use std::process::Command;

use crate::store::{get_config, set_config, Config, NeovimConfig};
use crate::utils::{get_nvim_config_dir, is_valid_github_url, print_intro, print_outro};
use crate::{store, utils};

pub fn add_config() {
let configs_dir = get_nvim_config_dir(None);
let configs_dir = utils::get_nvim_config_dir(None);

print_intro(None);

let name: String = input("Name")
let name: String = match cliclack::input("Name")
.placeholder("Configuration Name")
.validate(|input: &String| {
if get_config()
if store::get_config()
.configs
.iter()
.any(|nvim_config| nvim_config.name.to_lowercase() == *input.to_lowercase())
Expand All @@ -23,54 +20,71 @@ pub fn add_config() {
}
})
.interact()
.unwrap();
let repo_url: String = input("GitHub Repository URL")
{
Ok(name) => name,
Err(_) => utils::print_outro_cancel(Some("Failed to get configuration name")),
};
let repo_url: String = match cliclack::input("GitHub Repository URL")
.placeholder("https://github.com/user/repo")
.validate(|input: &String| {
if !is_valid_github_url(input) {
if !utils::is_valid_github_url(input) {
Err("URL must be a valid GitHub repository URL")
} else {
Ok(())
}
})
.interact()
.unwrap();
let nvim_dir_name: String = input(format!(
{
Ok(repo_url) => repo_url,
Err(_) => utils::print_outro_cancel(Some("Failed to get GitHub Repository URL")),
};
let nvim_dir_name: String = match cliclack::input(format!(
"{} ({})",
"Neovim Directory Name",
configs_dir.join("<configuration-name>").to_str().unwrap()
match configs_dir.join("<configuration-name>").to_str() {
Some(s) => s,
None => utils::print_outro_cancel(None),
}
))
.placeholder("configuration-name")
.validate(|input: &String| {
if get_config()
if store::get_config()
.configs
.iter()
.any(|nvim_config| nvim_config.nvim_dir_name == *input)
{
Err("Directory name already exists")
} else if !utils::is_valid_dir_name(input) {
Err("Directory name is not valid")
} else {
Ok(())
}
})
.interact()
.unwrap();
{
Ok(nvim_dir_name) => nvim_dir_name,
Err(_) => utils::print_outro_cancel(Some("Failed to get Neovim Directory Name")),
};

let mut spinner = spinner();
let mut spinner = cliclack::spinner();
spinner.start(format!("Cloning {} from '{}'...", name, repo_url).as_str());
Command::new("git")
if Command::new("git")
.arg("clone")
.arg(&repo_url)
.arg(configs_dir.join(&nvim_dir_name))
.arg("--depth")
.arg("1")
.output()
.expect("Failed to clone repository");
.is_err()
{
utils::print_outro_cancel(Some("Failed to clone repository"))
}
spinner.stop(format!("Cloned {} from '{}'", name, repo_url).as_str());

spinner.start("Editing user config...");
set_config(
Config {
configs: vec![NeovimConfig {
store::set_config(
store::Config {
configs: vec![store::NeovimConfig {
name: name.clone(),
repo_url: repo_url.clone(),
nvim_dir_name: nvim_dir_name.clone(),
Expand All @@ -80,12 +94,15 @@ pub fn add_config() {
);
spinner.stop("User config saved");

print_outro(Some(
utils::print_outro(Some(
format!(
"Added {} ({}) to '{}'",
name,
repo_url,
configs_dir.join(nvim_dir_name).to_str().unwrap()
match configs_dir.join(nvim_dir_name).to_str() {
Some(s) => s,
None => utils::print_outro_cancel(None),
}
)
.as_str(),
));
Expand Down
18 changes: 9 additions & 9 deletions src/cmds/list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use prettytable::{color, Attr, Cell, Row, Table};

use crate::{store::get_config, utils::get_nvim_config_dir};
use crate::{store, utils};

pub fn list_configs() {
let config = get_config();
let config_dir = get_nvim_config_dir(None);
utils::ensure_non_empty_config();

let config = store::get_config();
let config_dir = utils::get_nvim_config_dir(None);
let mut table = Table::new();

table.add_row(Row::new(vec![
Expand All @@ -18,12 +20,10 @@ pub fn list_configs() {
for nvim_config in config.configs.iter() {
table.add_row(Row::new(vec![
Cell::new(&nvim_config.name),
Cell::new(
config_dir
.join(&nvim_config.nvim_dir_name)
.to_str()
.unwrap(),
),
Cell::new(match config_dir.join(&nvim_config.nvim_dir_name).to_str() {
Some(s) => s,
None => utils::print_outro_cancel(None),
}),
Cell::new(&nvim_config.repo_url),
]));
}
Expand Down
Loading

0 comments on commit 5083bb1

Please sign in to comment.