Skip to content

Commit

Permalink
Add flag --sub
Browse files Browse the repository at this point in the history
  • Loading branch information
marchersimon committed Sep 2, 2021
1 parent 6ab101b commit 9ea64c0
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Options:
-a, --all When used with --update, this updates all languages, instead of just the installed ones
-f, --find Perform a full-text search through all pages. Modifiers are --name, --description and --example
, --stem In case you don't need fancy English. Enjoy pure information. (This version of the page will be searched in with --find)
, --sub Get a list of all documented sub-commands
-h, --help: Display help about tldr-cpp
```

Expand Down
68 changes: 67 additions & 1 deletion src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,70 @@ void cache::stat(string name) {
}
std::cout << std::endl;
}
}
}

cache::CommandPreview::CommandPreview(string name, string description) {
this->description = description;
this->name = name;
}

void cache::printSubcommands(string baseCommand) {
vector<CommandPreview> subcommands;
uint maxNameLen = 0;
bool isBase;
for(const auto & entry : std::filesystem::recursive_directory_iterator(global::tldrPath + "pages/")) {
if(entry.is_directory()) {
continue;
}
std::size_t pos = entry.path().string().find_last_of('/');
string fileName;
if(pos != string::npos) {
fileName = entry.path().string().substr(pos + 1, entry.path().string().size() - pos - 4);
}
if(fileName == baseCommand) {
isBase = true;
} else if(!fileName.starts_with(baseCommand + "-")) {
continue;
}
std::ifstream fileStream(entry.path());
string file, temp;
while(std::getline(fileStream, temp)) {
file += temp + '\n';
}
fileStream.close();
Page page(file);
page.name.erase(0, 2);
// double check if the command is really a subcommand (unlike e.g. ssh-keygen)
string commandName = page.name;
if(!commandName.starts_with(baseCommand + " ") && !isBase) {
continue;
}
// get first line of description
page.description.erase(0, 2);
pos = page.description.find('\n');
string descr = page.description.substr(0, pos);
page.formatBackticks(descr);

if(commandName.length() > maxNameLen) {
maxNameLen = commandName.length();
}
subcommands.push_back({commandName, descr});
isBase = false;
}

if(subcommands.size() == 1) {
std::cout << baseCommand << " doesn't seem to have documented sub-commands." << std::endl;
return;
} else if(subcommands.size() == 0) {
std::cout << baseCommand << " does not even have documentation itself." << std::endl;
}

maxNameLen += 2;

std::sort(subcommands.begin(), subcommands.end(), [](const CommandPreview & x, const CommandPreview & y) {return x.name < y.name;});

for(const auto & command : subcommands) {
std::cout << command.name << string(maxNameLen - command.name.length(), ' ');
std::cout << command.description << std::endl;
}
}
9 changes: 9 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace cache {
bool contains(Target target);
};

class CommandPreview {
public:
CommandPreview(string name, string language);
string name;
string description;
};

void verify();

void findPlatforms();
Expand All @@ -35,4 +42,6 @@ namespace cache {
Index getFromIndex(string name);

void stat(string name);

void printSubcommands(string baseCommand);
}
1 change: 1 addition & 0 deletions src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace global {
bool description = false;
bool examples = false;
bool stem = false;
bool sub = false;
}

string tldrPath;
Expand Down
1 change: 1 addition & 0 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace global {
extern bool description;
extern bool examples;
extern bool stem;
extern bool sub;
}

extern string tldrPath;
Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ int main(int argc, char *argv[]) {

trycatch(global::init());

if(global::opts::sub) {
cache::printSubcommands(global::opts::file);
return 0;
}

if(global::opts::find) {
find(global::opts::search_terms);
/*for(auto & s : global::opts::search_terms) {
Expand Down
6 changes: 5 additions & 1 deletion src/opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void opts::parse(int argc, char* argv[]) {
{"description", no_argument, NULL, 'd'},
{"examples", no_argument, NULL, 'e'},
{"stem", no_argument, NULL, 't'},
{"sub", no_argument, NULL, 'b'},
{NULL, 0, NULL, 0},
};

Expand Down Expand Up @@ -121,9 +122,12 @@ void opts::parse(int argc, char* argv[]) {
examples = true;
findOverrideDefaults = true;
break;
case 't': //stem
case 't':
stem = true;
break;
case 'b':
sub = true;
break;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ void Page::format() {
description.erase(matches.position() + matches.length() - 2, 1);
description.erase(matches.position(), 1);
}
formatBackticks(&description);
formatBackticks(description);

for (auto & example : examples) {
formatBackticks(&example.description);
formatBackticks(example.description);
example.command.replace(0, 1, global::color::command);
example.command.replace(example.command.length() - 1, 1, global::color::dfault);
formatTokenSyntax(example.command);
}
}

void Page::formatBackticks(string* str) {
void Page::formatBackticks(string & str) {
std::regex reg("(\\`([^\\`].*?)\\`)");
std::smatch matches;
while(std::regex_search(*str, matches, reg)) {
str->replace(matches.position() + matches.length() - 1, 1, global::color::dfault);
str->replace(matches.position(), 1, global::color::backtick);
while(std::regex_search(str, matches, reg)) {
str.replace(matches.position() + matches.length() - 1, 1, global::color::dfault);
str.replace(matches.position(), 1, global::color::backtick);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class Page {

void print();
void format();
void formatBackticks(string* str);
void formatBackticks(string & str);
static void formatTokenSyntax(string & str);
};

0 comments on commit 9ea64c0

Please sign in to comment.