Compare commits

..
3 changed files with 18 additions and 107 deletions

View file

@ -9,8 +9,6 @@ set(Qt6_DIR "/home/ud/Qt/6.9.2/gcc_64/lib/cmake/Qt6")
find_package(Boost CONFIG) find_package(Boost CONFIG)
find_package(Qt6 REQUIRED COMPONENTS Core) find_package(Qt6 REQUIRED COMPONENTS Core)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
qt_add_executable(goto main.cpp) qt_add_executable(goto main.cpp)
target_link_libraries(goto PRIVATE Qt6::Core) target_link_libraries(goto PRIVATE Qt6::Core)

View file

@ -1,24 +1,16 @@
### Simple Goto script written in cpp. ### Simple Goto script written in cpp.
#### Usage: Usage: goto [OPTION|LINK_NAME]
goto [OPTION|LINK_NAME] Options:
#### Options:
-h --help : Show this info -h --help : Show this info
-c --create_link : Create a new link e.g: goto --create_link [LINK_NAME] [FROM_PATH] -c --create_link : Create a new link e.g: goto --create_link [LINK_NAME] [FROM_PATH]
-l --list : List all the existing links -l --list : List all the existing links
-lo --links-only : List the name of the links only (This is mostly for autocompletion). -d --delete_link : Delete an exisiting link e.g: goto -d [LINK_NAME]
-p --print-path : Prints the path of the specified link and exits. e.g: goto -p [LINK_NAME] -rf --delete_file : Delete the existing link file completely.
-d --delete-link : Delete an exisiting link e.g: goto -d [LINK_NAME]
-rf --delete-file : Delete the existing link file completely.
[LINK_NAME] : Goes to an exisiting link e.g: goto [LINK_NAME] [LINK_NAME] : Goes to an exisiting link e.g: goto [LINK_NAME]
[LINK_NAME] : Rules for creating [LINK_NAME] name must be BASH variable and must not start with "-"
[LINK_NAME] : [LINK_NAME] cannot be "last"
#### Reserved link names: ### To use add a function in bash like:
last : Stores the last navigated link directory
prev : Stores the directory from which last navigation was performed
### To use add a function in shell like:
#### Bash
```bash ```bash
function goto() { function goto() {
if [[ "$#" -eq 1 ]] && [[ "$1" != -* ]]; then if [[ "$#" -eq 1 ]] && [[ "$1" != -* ]]; then
@ -33,24 +25,5 @@ function goto() {
fi 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
```

View file

@ -73,14 +73,11 @@ void show_help() {
ss << " -h --help : Show this info" << std::endl; 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 << " -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 << " -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 << " -p --print-path : Prints the path of the specified link and exits. e.g: goto -p [LINK_NAME]" << std::endl; ss << " -rf --delete_file : Delete the existing link file completely." << 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; ss << " [LINK_NAME] : Goes to an exisiting link e.g: goto [LINK_NAME]" << std::endl;
ss << "Reserved link names: " << std::endl; ss << " [LINK_NAME] : Rules for creating [LINK_NAME] name must be BASH variable and must not start with \"-\"" << std::endl;
ss << " last : Stores the last navigated link directory" << std::endl; ss << " [LINK_NAME] : [LINK_NAME] cannot be \"last\"" << std::endl;
ss << " prev : Stores the directory from which last navigation was performed" << std::endl;
std::cout << ss.str(); std::cout << ss.str();
} }
@ -104,50 +101,23 @@ void print_links() {
int iter = 1; int iter = 1;
std::cout << "Links from file: " << globals::config_path << std::endl; std::cout << "Links from file: " << globals::config_path << std::endl;
for(auto &[key, value]: link_list) { for(auto &[key, value]: link_list) {
if(key != "last" && key != "prev") if(key != "last")
std::cout << iter++ << ". " << key << " --> " << value << std::endl; std::cout << iter++ << ". " << key << " --> " << value << std::endl;
} }
if(link_list.find("last") != link_list.end()) { if(link_list.find("last") != link_list.end()) {
std::cout << iter++ << ". last --> " << link_list["last"] << std::endl; std::cout << iter << ". Last place --> " << link_list["last"] << std::endl;
} }
if(link_list.find("prev") != link_list.end()) {
std::cout << iter++ << ". prev --> " << link_list["prev"] << std::endl;
}
} }
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, bool> {QJsonObject()};
}).value();
std::unordered_map<std::string, std::string> 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) { void delete_link(std::string link_name) {
QJsonObject obj = globals::read_config().or_else([](bool err) { QJsonObject obj = globals::read_config().or_else([](bool err){
std::cout << "Could not read config file." << std::endl; std::cout << "Could not read config file." << std::endl;
exit(-1); exit(-1);
return std::expected<QJsonObject, bool> {QJsonObject()}; return std::expected<QJsonObject, bool> {QJsonObject()};
}).value(); }).value();
if(obj.find(QString::fromStdString(link_name)) != obj.end()) {
obj.remove(QString::fromStdString(link_name)); obj.remove(QString::fromStdString(link_name));
globals::overwrite_config(obj); globals::overwrite_config(obj);
std::cout << "Link: " << link_name << " deleted." << std::endl; std::cout << "Link: " << link_name << " deleted." << std::endl;
}
else {
std::cout << "Link: " << link_name << " does not exist in the links file." << std::endl;
}
} }
void create_link(std::string link_name, std::string dir) { void create_link(std::string link_name, std::string dir) {
@ -202,7 +172,6 @@ void goto_link(std::string link_name) {
std::cout << "Specified link: " << link_name << " does not exist in links file." << std::endl; std::cout << "Specified link: " << link_name << " does not exist in links file." << std::endl;
exit(-1); exit(-1);
} }
config["prev"] = QString::fromStdString(std::filesystem::current_path().string());
config["last"] = QString::fromStdString(place); config["last"] = QString::fromStdString(place);
globals::overwrite_config(config); globals::overwrite_config(config);
// int parent_id = get_parent_id(); // int parent_id = get_parent_id();
@ -242,18 +211,6 @@ void delete_file() {
} }
} }
void print_path(std::string link_name) {
QJsonObject config = globals::read_config().or_else([] (bool err){
std::cout << "Could not read conifg file." << std::endl;
exit(-1);
return std::expected<QJsonObject, bool> {QJsonObject()};
}).value();
std::string place = config[QString::fromStdString(link_name)].toString("Unknown link name.").toStdString();
std::cout << place << std::endl;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("goto"); QCoreApplication::setApplicationName("goto");
@ -265,7 +222,7 @@ int main(int argc, char *argv[]) {
} }
globals::config_path = globals::config_path / "links.json"; globals::config_path = globals::config_path / "links.json";
std::unordered_map<std::string, int> 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}, {"p", 7}, {"--print-path", 7}}; std::unordered_map<std::string, int> 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}};
int command_size = argc - 1; int command_size = argc - 1;
@ -328,23 +285,6 @@ int main(int argc, char *argv[]) {
delete_file(); delete_file();
exit(0); exit(0);
} }
case 6: {
if(command_size != 1) {
show_invalid();
exit(0);
}
print_links_only();
exit(0);
}
case 7: {
if(command_size != 2) {
show_invalid();
exit(0);
}
std::string link_name = argv[2];
print_path(link_name);
exit(0);
}
} }
} }