demartini / emc.fish

Edit My Config is a plugin for Fish Shell.
MIT License
11 stars 2 forks source link

[Feature Request] Let users add their own program/configs with a command #117

Open MrDowntempo opened 10 months ago

MrDowntempo commented 10 months ago

This is a handy tool, but only for the small handful of programs. It would be much more useful if you could add your own programs to it with a single function or command.

Oskar-Idland commented 8 months ago

I agree and would be happy to help. I believe this would require an external text file to keep track. I have rewritten the function to avoid boilerplate, and make it easier to add your own stuff assuming the config file is in the home directory.

# emc - Edit My Config.
# https://github.com/demartini/emc.fish

set -g _emc_version 1.0.0

set -g aliases "bash"\
               "fish"\
               "git"\
               "gpg"\
               "gpga"\
               'ssh'\
               "nvim"\
               "starship"\
               "tmux"\
               "vim"\
               "zsh"

set -g files   ".bashrc"\
               ".config/fish/config.fish"\
               ".gitconfig"\
               ".gnupg/gpg.conf"\
               ".gnupg/gpg-agent.conf"\
               ".ssh/config"\
               ".config/nvim/init.vim"\
               ".config/starship.toml"\
               ".tmux.conf"\
               ".vimrc"\
               ".zshrc"

function __emc -d "Edit My Config"
    set option $argv[1]
    switch "$option"
        case 'help'
            _emc_help
            return

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

        case ''
            _emc_help >&2
            return 1
     end

    # Checks the input mathes alias
    set index 1
    for alias in $aliases
        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

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) (set_color cyan)"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

Using this rewritten function instead, it's also easy to just set the aliases, and their paths as empty list, and read from a file. If we have a text file called "options.txt" like this:

'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'

We could simply define the aliases and their paths like this:

set aliases
set files

set file_content (cat "options.txt")
set lines (string split \n $file_content) 

for line in $lines
    set l  (string split "," $line)
    set -a aliases $l[1]
    set -a files $l[2]
end

With an "add_option" method looking like this:

function _add_option
    set name $argv[1]
    set config_path $argv[2]

    if not contains \'$name\' $aliases
        echo "Adding '$name','$folder' to options.txt"
        echo "'$name'","'$folder'" >> "options.txt"
    else
        echo "Entry '$name','$folder' already exists in options.txt"
    end
end

Thoughts?

Oskar-Idland commented 8 months ago

[#118 ] Opened a pull request

demartini commented 8 months ago

Hello everyone, sorry for the delay, I've been a bit busy in the past few months.

@MrDowntempo I really liked the idea of being able to add more settings without having to rewrite the plugin every time.

@Oskar-Idland Thank you so much for your contribution; it will be of great help. I'll test it, review it, and provide feedback soon.

Oskar-Idland commented 4 months ago

@MrDowntempo If you want the feature now, you can install my fork instead:

fisher install Oskar-Idland/emc.fish

Usage is documented in the README. I have been using it since my pull request

MrDowntempo commented 4 months ago

Very cool! I'll give it a shot a little later this week

MrDowntempo commented 4 months ago

@Oskar-Idland

There seems to be a minor issue with either your implementation, or just the instructions. It mentions an emc_config.txt file, but it seems like the file is just emc_config. Not sure if its smarter to add the .txt to match the docs, or change the docs to just emcconfig. Alternatively, might be nice as a csv? ¯_(ツ)

But other than that, it seems to be working as intended. Got my wezterm config file added which I was missing, and am able to edit with an emc. Nice to be able to remove the config files I'm not using as well.

Oskar-Idland commented 4 months ago

Whoops, I fixed that now. The .txt extension was unnecessary. Thank you for the input. I agree a csv would be wiser. I am open to refactoring, this was just written on the fly.