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

.dreeignore usage guide and automation #54

Open
wants to merge 1 commit into
base: dev
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
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ dree 5 -f "file_name"

Dree includes a `.dreeignore` file that enlists some commonly ignored files and directories. This can be overriden be the use of the `-a` flag.

### Add or remove file or folder from .dreeignore

```shell
#eg. Add a file or folder to .dreeignore
dree -w "file or folder name"
#eg. Remove a file or folder from .dreeignore
dree -r "file or folder name"
```

![image](https://github.com/Ruberald/Dree/assets/31573113/6437731d-c799-4c8e-b939-9e6ef8984d87)

![image](https://github.com/Ruberald/Dree/assets/31573113/3e28ebdd-4311-4801-87aa-a08e2e8014d3)
Expand Down
80 changes: 72 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
void help() {
cout << "\tdree: "
<< "dree "
<< "[dep] [-f [dir|file]][-a]"
<< "[dep] [-f [dir|file]][-a][-w][-r]"
<< "\n";
cout << "\tVisualize directories until certain depth"
<< "\n";
Expand All @@ -26,6 +26,12 @@ void help() {
cout << "\t -a \t"
<< "Show hidden files"
<< "\n";
cout << "\t -w \t"
<< "Add files to .dreeignore file"
<< "\n";
cout << "\t -r \t"
<< "Remove files from .dreeignore file"
<< "\n";
}
bool isNumber(string line) {
char *p;
Expand Down Expand Up @@ -56,7 +62,58 @@ void search(int argc, char *argv[]) {
filesystem::path directoryPath(currentPath);
builder.SearchDirectory(currentPath, depth, query);
}

void update_dreeignore(string file_name){
ifstream in("./.dreeignore");
ofstream out("./temp.txt");
if(!in.is_open()){
cout<<"Error opening dreeignore file.\n";
}
string line;
while(getline(in,line)){
if(line==file_name){
cout<<"Successfully added.\n";
return ;
}
}
in.close();
in.open("./.dreeignore");
while(getline(in,line)){
out<<line+'\n';
}
out<<file_name;
cout<<"Successfully added.\n";
remove("./.dreeignore");
rename("./temp.txt","./.dreeignore");
out.close();
in.close();
return ;
}
void remove_file(string file_name){
string line;
ifstream in("./.dreeignore");
if(!in.is_open()){
cout<<"Error opening dreeignore file.\n";
}
ofstream out("./temp.txt");
bool sw=true;;
while(getline(in,line)){
if(line!=file_name){
if(sw){
sw=false;
}
else{
out<<'\n';
}
out<<line;
}
}
in.close();
out.close();
remove("./.dreeignore");
rename("./temp.txt","./.dreeignore");
cout<<"Removed successfully.\n";
return ;
}
void dree(int argc, char *argv[]) {
if (!(argc == 3 || argc == 4)) {
cout << "Missing args" << std::endl;
Expand All @@ -67,25 +124,32 @@ void dree(int argc, char *argv[]) {
bool showHidden = false;
int depth = -1;
if (argc == 4 || (argc == 3 && isNumber(argv[2]))) {
depth = stoi(argv[2]);
// TODO:add check to prevent overflow
if (depth >= 60) {
cout << "mask overflow!!";
return;
}
showHidden = false;
if (argc == 4) {
string flag = argv[3];
if (flag == "-a") {
if (string(argv[3])== "-a") {
depth = stoi(argv[2]);
showHidden = true;
} else {
}
else if(string(argv[2]) == "-w"){
update_dreeignore(argv[3]);
return ;
}
else if(string(argv[2]) == "-r"){
remove_file(argv[3]);
return ;
}
else {
cout << "Unknown flags specified.";
return;
}
}
} else if (argc == 3) {
string flag = argv[2];
if (flag == "--help") {
if (string(argv[2]) == "--help") {
help();
return;
}
Expand Down