Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for users to add their own config files and command #118

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ $ emc [<option>]

![demo][]

| **Option** | **Description** |
| -------------- | -------------------------------- |
| `emc bash` | Opens the Bash config file. |
| `emc fish` | Opens the Fish config file. |
| `emc git` | Opens the Git config file. |
| `emc gpg` | Opens the GPG config file. |
| `emc gpga` | Opens the GPG agent config file. |
| `emc nvim` | Opens the Neovim config file. |
| `emc starship` | Opens the starship config file. |
| `emc tmux` | Opens the tmux config file. |
| `emc vim` | Opens the Vim config file. |
| `emc zsh` | Opens the Zsh config file. |
| `emc version` | Show the current version. |
| `emc help` | Print help. |
| **Option** | **Description** |
| -------------- | ------------------------------------------------------------------ |
| `emc bash` | Opens the Bash config file. |
| `emc fish` | Opens the Fish config file. |
| `emc git` | Opens the Git config file. |
| `emc gpg` | Opens the GPG config file. |
| `emc gpga` | Opens the GPG agent config file. |
| `emc nvim` | Opens the Neovim config file. |
| `emc starship` | Opens the starship config file. |
| `emc tmux` | Opens the tmux config file. |
| `emc vim` | Opens the Vim config file. |
| `emc zsh` | Opens the Zsh config file. |
| `emc add <name> <path/to/file/from/home>` | Adds new option. |
| `emc remove <name>` | Removes an option. |
| `emc list` | List all options, including user added. |
| `emc version` | Show the current version. |
| `emc help` | Print help. |

## Customization

Expand Down Expand Up @@ -109,6 +112,38 @@ Alternatively, you can add the following line to your `~/.config/fish/config.fis
set --export EMC_EDITOR nvim
```

### Using custom config files
Using the `add` option, you can add your own config files to `emc`. For example:

```fish
❯ emc add foo .config/bar
Adding 'foo','.config/bar' to ~/.config/fish/conf.d/emc_options.txt
```
The add command assumes the file is in your home directory, so you don't need to add the full path.

If you are uncertain about everything you have added, you can list all options with the `list` option. For example:

```fish
❯ emc list
Option: bash
Config location: ~/.bashrc

Option: fish
Config location: ~/.config/fish/config.fish
[...]
Option: foo
Config location: ~/.config/bar
```


The option can be removed with the `remove` option. For example:

```fish
❯ emc remove foo
Adding 'foo','.config/bar' to ~/.config/fish/conf.d/emc_options.txt
```


## Contributing

If you are interested in helping contribute, please take a look at our [contribution guidelines][] and open an [issue][] or [pull request][].
Expand Down
11 changes: 11 additions & 0 deletions conf.d/emc_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bash, .bashrc
fish, .config/fish/config.fish
git, .gitconfig
gpg, gnupg/gpg.conf
gpga, .gnupg/gpg-agent.conf
ssh, .ssh/config
nvim, .config/nvim/init.vim
starship, .config/starship.toml
tmux, .tmux.conf
vim, .vimrc
zsh, .zshrc
272 changes: 135 additions & 137 deletions functions/__emc.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,157 @@
# https://github.com/demartini/emc.fish

set -g _emc_version 1.0.0
set -g _options_path "$HOME/.config/fish/conf.d/emc_options.txt"
set -g _pretty_path "~/"(string trim --chars=$HOME /$_options_path)

function __emc -d "Edit My Config"
set options
set files

set option $argv[1]
switch "$option"
case 'help'
_emc_help
return

case 'version'
echo -e "$EMC_CMD, version $_emc_version"
return

case 'bash'
_emc_bash

case 'fish'
_emc_fish
case 'add'
_add_option $argv[2] $argv[3]
return

case 'remove'
_remove_option $argv[2]
return

case 'git'
_emc_git
case 'list'
_list_options
return

case 'gpg'
_emc_gpg
case ''
_emc_help >&2
return 1
end


set lines (string split \n (string trim < $_options_path))
# Read all options and file locations
for line in $lines
set l (string split "," $line)
set -a options (string trim $l[1])
set -a files (string trim $l[2])
end
# Checks the input matches alias
set index 1
for alias in $options
if test "$alias" = "$option"
_open_file $files[$index]
return
end
set index (math $index + 1)
end
echo -e (set_color red --bold)"✗ Unknown option: $option"(set_color normal) >&2
end

case 'gpga'
_emc_gpga

case 'ssh'
_emc_ssh
# Based on input, opends the associated config file
function _open_file
set file $argv[1]
if test -f $HOME/$file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/$file"(set_color normal)
command $EMC_EDITOR $HOME/$file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/$file"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

case 'nvim'
_emc_nvim
# Lets user add their own options
function _add_option
set name $argv[1]
set file $argv[2]

# Check if both name and file are provided
# Should maybe add more checks for valid names and paths names
if test -z "$name" -o -z "$file"
echo "Error: Both name and file must be provided. You gave: "
echo "Name: '$name'"
echo "File: '$file'"
return 1
end

case 'starship'
_emc_starship
# Read all options
set options
set lines (string split \n (string trim < $_options_path))
for line in $lines
set l (string split "," $line)
set -a options (string trim $l[1])
end

case 'tmux'
_emc_tmux
if string match -q "$name" $options
echo "Option '$name' already exists in '$_pretty_path'"
return
else
echo "Adding '$name' at '$file' to '$_pretty_path'"
echo "$name, $file" >> $_options_path
return
end
end

case 'vim'
_emc_vim
function _remove_option
set name $argv[1]

case 'zsh'
_emc_zsh
# Check if name is provided
# Should maybe add more checks for valid names and paths names
if test -z "$name"
echo "Error: Both name must be provided. You gave: "
echo "Name: '$name'"
return 1
end

case ''
_emc_help >&2
return 1
# Read the current options from the file
set lines (string split \n (string trim < $_options_path))
# Create a temporary file to store modified content
set temp_file (mktemp)

for line in $lines
set l (string split "," $line)
set alias (string trim $l[1])

set found_option false
if test "$alias" != "$name"
# Write the line to the temporary file (exclude the line to be removed)
echo $line >> $temp_file

else
set found_option true
end

end

if $found_option
echo "'$name' was removed from options in '$_pretty_path'"
else
echo "Found no command '$name'"
end

# Replace the original file with the modified content
mv $temp_file $_options_path
end

case '*'
echo -e (set_color red --bold)"✗ Unknown option: $option"(set_color normal) >&2
return 1
function _list_options
# Read the current options from the file
set lines (string split \n (string trim < $_options_path))
# Create a temporary file to store modified content
set temp_file (mktemp)

for line in $lines
set l (string split "," $line)
set alias (string trim $l[1])
set file (string trim $l[2])
echo "Option: $alias"
echo "Config location: ~/$file"\n
end
end

Expand All @@ -72,116 +173,13 @@ function _emc_help
echo -e " vim Opens the Vim config file."
echo -e " zsh Opens the Zsh config file."
echo -e " version Show the current version."
echo -e " add Adds new option.
Format should be: emc add <name> <path/to/file/from/home"
echo -e " remove Removes an option.
Format should be emc remove <name>"
echo -e " list List all options, including user added."
echo -e " help Print this help.\n"
echo -e "For more information visit → https://git.io/emc.fish"
end

function _emc_bash
set file $HOME/.bashrc
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.bashrc"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.bashrc"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_fish
set file $HOME/.config/fish/config.fish
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.config/fish/config.fish"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.config/fish/config.fish"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_git
set file $HOME/.gitconfig
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.gitconfig"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.gitconfig"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_gpg
set file $HOME/.gnupg/gpg.conf
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.gnupg/gpg.conf"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.gnupg/gpg.conf"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_gpga
set file $HOME/.gnupg/gpg-agent.conf
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.gnupg/gpg-agent.conf"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.gnupg/gpg-agent.conf"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_ssh
set file $HOME/.ssh/config
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.ssh/config"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.ssh/config"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_nvim
set file $HOME/.config/nvim/init.vim
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.config/nvim/init.vim"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.config/nvim/init.vim"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_starship
set file $HOME/.config/starship.toml
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.config/starship.toml"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.config/starship.toml"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_tmux
set file $HOME/.tmux.conf
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.tmux.conf"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.tmux.conf"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_vim
set file $HOME/.vimrc
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.vimrc"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.vimrc"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end

function _emc_zsh
set file $HOME/.zshrc
if test -f $file
echo -e (set_color cyan)"→ Opening" (set_color --underline)"~/.zshrc"(set_color normal) (set_color cyan)"file."(set_color normal)
command $EMC_EDITOR $file
else
echo -e (set_color red --bold)"✗ The" (set_color --underline)"~/.zshrc"(set_color normal) (set_color red --bold)"file does not exist."(set_color normal)
end
end