Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73e893124f | ||
|
|
7a2825aeb7 | ||
|
|
72f4c3d7b8 | ||
|
|
c688946c83 | ||
|
|
d1d060f5e9 | ||
|
|
a9e4ab9e98 | ||
|
|
8979439c4d | ||
|
|
d671096a0b | ||
|
|
b5e9cb8c86 | ||
|
|
b229a40278 |
3 changed files with 107 additions and 18 deletions
|
|
@ -9,6 +9,8 @@ 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)
|
||||||
|
|
|
||||||
41
README.md
41
README.md
|
|
@ -1,16 +1,24 @@
|
||||||
### Simple Goto script written in cpp.
|
### Simple Goto script written in cpp.
|
||||||
Usage: goto [OPTION|LINK_NAME]
|
#### Usage:
|
||||||
Options:
|
goto [OPTION|LINK_NAME]
|
||||||
|
#### 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
|
||||||
-d --delete_link : Delete an exisiting link e.g: goto -d [LINK_NAME]
|
-lo --links-only : List the name of the links only (This is mostly for autocompletion).
|
||||||
-rf --delete_file : Delete the existing link file completely.
|
-p --print-path : Prints the path of the specified link and exits. e.g: goto -p [LINK_NAME]
|
||||||
|
-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"
|
|
||||||
|
|
||||||
### To use add a function in bash like:
|
#### Reserved link names:
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -25,5 +33,24 @@ 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
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
74
main.cpp
74
main.cpp
|
|
@ -73,11 +73,14 @@ 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 << " -d --delete_link : Delete an exisiting link e.g: goto -d [LINK_NAME]" << std::endl;
|
ss << " -lo --links-only : List the name of the links only (This is mostly for autocompletion)." << std::endl;
|
||||||
ss << " -rf --delete_file : Delete the existing link file completely." << std::endl;
|
ss << " -p --print-path : Prints the path of the specified link and exits. e.g: goto -p [LINK_NAME]" << 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 << " [LINK_NAME] : Rules for creating [LINK_NAME] name must be BASH variable and must not start with \"-\"" << std::endl;
|
ss << "Reserved link names: " << std::endl;
|
||||||
ss << " [LINK_NAME] : [LINK_NAME] cannot be \"last\"" << std::endl;
|
ss << " last : Stores the last navigated link directory" << std::endl;
|
||||||
|
ss << " prev : Stores the directory from which last navigation was performed" << std::endl;
|
||||||
std::cout << ss.str();
|
std::cout << ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,24 +104,51 @@ 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")
|
if(key != "last" && key != "prev")
|
||||||
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 place --> " << link_list["last"] << std::endl;
|
std::cout << iter++ << ". last --> " << 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) {
|
||||||
if(dir == ".") {
|
if(dir == ".") {
|
||||||
|
|
@ -172,6 +202,7 @@ 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();
|
||||||
|
|
@ -211,6 +242,18 @@ 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");
|
||||||
|
|
@ -222,7 +265,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}};
|
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}};
|
||||||
|
|
||||||
|
|
||||||
int command_size = argc - 1;
|
int command_size = argc - 1;
|
||||||
|
|
@ -285,6 +328,23 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue