diff --git a/README.md b/README.md index a7ce280..b85c670 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,8 @@ [LINK_NAME] : [LINK_NAME] cannot be "last" -### To use add a function in .bashrc like: +### To use add a function in shell like: +#### Bash ```bash function goto() { if [[ "$#" -eq 1 ]] && [[ "$1" != -* ]]; then @@ -34,5 +35,24 @@ function goto() { fi } ``` +#### Fish +```fish +function goto + if test (count $argv) -eq 1 + if not string match -rq -- '^-' "$argv[1]" + set out (/home/ud/Scripts/goto $argv[1] 2>&1 ) + if test $status -eq 0 + cd "$out" + else + echo $out + end + else + /home/ud/Scripts/goto $argv + end + else + /home/ud/Scripts/goto $argv + end +end +``` diff --git a/main.cpp b/main.cpp index 286aacc..732fcf6 100644 --- a/main.cpp +++ b/main.cpp @@ -73,6 +73,7 @@ void show_help() { ss << " -h --help : Show this info" << std::endl; ss << " -c --create_link : Create a new link e.g: goto --create_link [LINK_NAME] [FROM_PATH]" << std::endl; ss << " -l --list : List all the existing links" << std::endl; + ss << " -lo --links-only : List the name of the links only (This is mostly for autocompletion)." << std::endl; ss << " -d --delete_link : Delete an exisiting link e.g: goto -d [LINK_NAME]" << std::endl; ss << " -rf --delete_file : Delete the existing link file completely." << std::endl; ss << " [LINK_NAME] : Goes to an exisiting link e.g: goto [LINK_NAME]" << std::endl; @@ -109,6 +110,24 @@ void print_links() { } } +void print_links_only() { + QJsonObject links = globals::read_config().or_else([](bool err){ + std::cout << "Could not read config file." << std::endl; + exit(-1); + return std::expected {QJsonObject()}; + }).value(); + std::unordered_map link_list; + for(auto i = links.begin(); i != links.end(); i++) { + link_list[i.key().toStdString()] = i.value().toString().toStdString(); + } + int iter = 1; + for(auto &[key, value]: link_list) { + std::cout << key << " "; + } + std::cout << std::endl; +} + + void delete_link(std::string link_name) { QJsonObject obj = globals::read_config().or_else([](bool err){ std::cout << "Could not read config file." << std::endl; @@ -222,7 +241,7 @@ int main(int argc, char *argv[]) { } globals::config_path = globals::config_path / "links.json"; - std::unordered_map command_list = {{"help", 1}, {"h", 1}, {"create_link", 2}, {"c", 2}, {"l", 3}, {"list", 3}, {"d", 4}, {"delete_link", 4}, {"rf", 5}, {"delete_file", 5}}; + std::unordered_map command_list = {{"help", 1}, {"h", 1}, {"create_link", 2}, {"c", 2}, {"l", 3}, {"list", 3}, {"d", 4}, {"delete_link", 4}, {"rf", 5}, {"delete_file", 5}, {"lo", 6}, {"links-only", 6}}; int command_size = argc - 1; @@ -285,6 +304,14 @@ int main(int argc, char *argv[]) { delete_file(); exit(0); } + case 6: { + if(command_size != 1) { + show_invalid(); + exit(0); + } + print_links_only(); + exit(0); + } } }