From 35c788f35a94f42d4492bb3dcb22e19a47b32d8e Mon Sep 17 00:00:00 2001 From: Parth Mehrotra Date: Sat, 2 Jan 2016 14:06:33 -0500 Subject: [PATCH 1/5] added an error message for no args, and wrong args --- src/tldr.cpp | 160 ++++++++++++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 72 deletions(-) diff --git a/src/tldr.cpp b/src/tldr.cpp index 6a2ac68..b104b95 100644 --- a/src/tldr.cpp +++ b/src/tldr.cpp @@ -47,81 +47,97 @@ void replaceAll(std::string& str, std::string const& from, int main(int argc, char* argv[]) { + if (argc <= 1) + { + std::cout << "usage: tldr " << std::endl; + std::cout << "Which page do you want to see?" << std::endl; + std::cout << "try: tldr tldr" << std::endl; + return EXIT_FAILURE; + } + struct utsname sys; uname(&sys); - if (argc > 1) - { - int i = 2; - std::string arg(argv[1]); - while (i < argc) { - arg += "-" + std::string(argv[i++]); - } - - std::string url = getUrlForArg(arg); - std::string urlForPlatform = getUrlForArgAndPlatform(arg, sys.sysname); - - std::string response = getContentForUrl(urlForPlatform); - if (response.empty()) { response = getContentForUrl(url); } - - replaceAll(response, "{{", ANSI_COLOR_CODE_PLACEHOLDER_FG); - replaceAll(response, "}}", ANSI_COLOR_RESET_FG); - - std::string const stripPrefix("#"); - std::string const explainPrefix(">"); - std::string const commentPrefix("-"); - std::string const codePrefix("`"); - std::stringstream ss(response); - std::string line; - bool firstComment = true; - - while (std::getline(ss, line, '\n')) - { - // Title - if (line.compare(0, stripPrefix.size(), stripPrefix) == 0) - { - replaceAll(line, "#", ANSI_COLOR_TITLE_FG); - std::cout << std::endl - << ANSI_BOLD_ON - << line - << ANSI_BOLD_OFF - << ANSI_COLOR_RESET_FG - << std::endl; - } - // Command explanation - else if (line.compare(0, explainPrefix.size(), explainPrefix) == 0) - { - replaceAll(line, explainPrefix, ANSI_COLOR_EXPLANATION_FG); - std::cout << line << ANSI_COLOR_RESET_FG << std::endl; - } - // Example comment - else if (line.compare(0, commentPrefix.size(), commentPrefix) == 0) - { - if (firstComment) - { - std::cout << std::endl; - firstComment = false; - } - - replaceAll(line, commentPrefix, ANSI_COLOR_COMMENT_FG); - std::cout << line << ANSI_COLOR_RESET_FG << std::endl; - } - // Code example - else if (line.compare(0, codePrefix.size(), codePrefix) == 0) - { - // Remove trailing backtick (`). - line = line.substr(0, line.size() - 1); - - // Replace first backtick (`) with three spaces for aligned indentation. - replaceAll(line, "`", " "); - std::cout << ANSI_COLOR_CODE_FG - << line - << ANSI_COLOR_RESET_FG - << std::endl - << std::endl; - } - } - } + int i = 2; + std::string arg(argv[1]); + while (i < argc) { + arg += "-" + std::string(argv[i++]); + } + + std::string url = getUrlForArg(arg); + std::string urlForPlatform = getUrlForArgAndPlatform(arg, sys.sysname); + + std::string response = getContentForUrl(urlForPlatform); + if (response.empty()) + { + response = getContentForUrl(url); + } + + if (response.empty()) + { + std::cout << "This page doesn't exist yet!" << std::endl; + std::cout << "Submit new pages here: https://github.com/tldr-pages/tldr" << std::endl; + return EXIT_FAILURE; + } + + replaceAll(response, "{{", ANSI_COLOR_CODE_PLACEHOLDER_FG); + replaceAll(response, "}}", ANSI_COLOR_RESET_FG); + + std::string const stripPrefix("#"); + std::string const explainPrefix(">"); + std::string const commentPrefix("-"); + std::string const codePrefix("`"); + std::stringstream ss(response); + std::string line; + bool firstComment = true; + + while (std::getline(ss, line, '\n')) + { + // Title + if (line.compare(0, stripPrefix.size(), stripPrefix) == 0) + { + replaceAll(line, "#", ANSI_COLOR_TITLE_FG); + std::cout << std::endl + << ANSI_BOLD_ON + << line + << ANSI_BOLD_OFF + << ANSI_COLOR_RESET_FG + << std::endl; + } + // Command explanation + else if (line.compare(0, explainPrefix.size(), explainPrefix) == 0) + { + replaceAll(line, explainPrefix, ANSI_COLOR_EXPLANATION_FG); + std::cout << line << ANSI_COLOR_RESET_FG << std::endl; + } + // Example comment + else if (line.compare(0, commentPrefix.size(), commentPrefix) == 0) + { + if (firstComment) + { + std::cout << std::endl; + firstComment = false; + } + + replaceAll(line, commentPrefix, ANSI_COLOR_COMMENT_FG); + std::cout << line << ANSI_COLOR_RESET_FG << std::endl; + } + // Code example + else if (line.compare(0, codePrefix.size(), codePrefix) == 0) + { + // Remove trailing backtick (`). + line = line.substr(0, line.size() - 1); + + // Replace first backtick (`) with three spaces for aligned indentation. + replaceAll(line, "`", " "); + std::cout << ANSI_COLOR_CODE_FG + << line + << ANSI_COLOR_RESET_FG + << std::endl + << std::endl; + } + } + return EXIT_SUCCESS; } From 4ab8c9a5cbcb7b1ae6c0cae25f5f3b07065f0fe4 Mon Sep 17 00:00:00 2001 From: Parth Mehrotra Date: Sun, 3 Jan 2016 12:44:31 -0500 Subject: [PATCH 2/5] I used the wrong formatting, replaced `\t` with ` ` --- src/tldr.cpp | 174 +++++++++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/src/tldr.cpp b/src/tldr.cpp index b104b95..6b1b1bd 100644 --- a/src/tldr.cpp +++ b/src/tldr.cpp @@ -47,97 +47,97 @@ void replaceAll(std::string& str, std::string const& from, int main(int argc, char* argv[]) { - if (argc <= 1) - { - std::cout << "usage: tldr " << std::endl; - std::cout << "Which page do you want to see?" << std::endl; - std::cout << "try: tldr tldr" << std::endl; - return EXIT_FAILURE; - } + if (argc <= 1) + { + std::cout << "usage: tldr " << std::endl; + std::cout << "Which page do you want to see?" << std::endl; + std::cout << "try: tldr tldr" << std::endl; + return EXIT_FAILURE; + } struct utsname sys; uname(&sys); - int i = 2; - std::string arg(argv[1]); - while (i < argc) { - arg += "-" + std::string(argv[i++]); - } - - std::string url = getUrlForArg(arg); - std::string urlForPlatform = getUrlForArgAndPlatform(arg, sys.sysname); - - std::string response = getContentForUrl(urlForPlatform); - if (response.empty()) - { - response = getContentForUrl(url); - } - - if (response.empty()) - { - std::cout << "This page doesn't exist yet!" << std::endl; - std::cout << "Submit new pages here: https://github.com/tldr-pages/tldr" << std::endl; - return EXIT_FAILURE; - } - - replaceAll(response, "{{", ANSI_COLOR_CODE_PLACEHOLDER_FG); - replaceAll(response, "}}", ANSI_COLOR_RESET_FG); - - std::string const stripPrefix("#"); - std::string const explainPrefix(">"); - std::string const commentPrefix("-"); - std::string const codePrefix("`"); - std::stringstream ss(response); - std::string line; - bool firstComment = true; - - while (std::getline(ss, line, '\n')) - { - // Title - if (line.compare(0, stripPrefix.size(), stripPrefix) == 0) - { - replaceAll(line, "#", ANSI_COLOR_TITLE_FG); - std::cout << std::endl - << ANSI_BOLD_ON - << line - << ANSI_BOLD_OFF - << ANSI_COLOR_RESET_FG - << std::endl; - } - // Command explanation - else if (line.compare(0, explainPrefix.size(), explainPrefix) == 0) - { - replaceAll(line, explainPrefix, ANSI_COLOR_EXPLANATION_FG); - std::cout << line << ANSI_COLOR_RESET_FG << std::endl; - } - // Example comment - else if (line.compare(0, commentPrefix.size(), commentPrefix) == 0) - { - if (firstComment) - { - std::cout << std::endl; - firstComment = false; - } - - replaceAll(line, commentPrefix, ANSI_COLOR_COMMENT_FG); - std::cout << line << ANSI_COLOR_RESET_FG << std::endl; - } - // Code example - else if (line.compare(0, codePrefix.size(), codePrefix) == 0) - { - // Remove trailing backtick (`). - line = line.substr(0, line.size() - 1); - - // Replace first backtick (`) with three spaces for aligned indentation. - replaceAll(line, "`", " "); - std::cout << ANSI_COLOR_CODE_FG - << line - << ANSI_COLOR_RESET_FG - << std::endl - << std::endl; - } - } - return EXIT_SUCCESS; + int i = 2; + std::string arg(argv[1]); + while (i < argc) { + arg += "-" + std::string(argv[i++]); + } + + std::string url = getUrlForArg(arg); + std::string urlForPlatform = getUrlForArgAndPlatform(arg, sys.sysname); + + std::string response = getContentForUrl(urlForPlatform); + if (response.empty()) + { + response = getContentForUrl(url); + } + + if (response.empty()) + { + std::cout << "This page doesn't exist yet!" << std::endl; + std::cout << "Submit new pages here: https://github.com/tldr-pages/tldr" << std::endl; + return EXIT_FAILURE; + } + + replaceAll(response, "{{", ANSI_COLOR_CODE_PLACEHOLDER_FG); + replaceAll(response, "}}", ANSI_COLOR_RESET_FG); + + std::string const stripPrefix("#"); + std::string const explainPrefix(">"); + std::string const commentPrefix("-"); + std::string const codePrefix("`"); + std::stringstream ss(response); + std::string line; + bool firstComment = true; + + while (std::getline(ss, line, '\n')) + { + // Title + if (line.compare(0, stripPrefix.size(), stripPrefix) == 0) + { + replaceAll(line, "#", ANSI_COLOR_TITLE_FG); + std::cout << std::endl + << ANSI_BOLD_ON + << line + << ANSI_BOLD_OFF + << ANSI_COLOR_RESET_FG + << std::endl; + } + // Command explanation + else if (line.compare(0, explainPrefix.size(), explainPrefix) == 0) + { + replaceAll(line, explainPrefix, ANSI_COLOR_EXPLANATION_FG); + std::cout << line << ANSI_COLOR_RESET_FG << std::endl; + } + // Example comment + else if (line.compare(0, commentPrefix.size(), commentPrefix) == 0) + { + if (firstComment) + { + std::cout << std::endl; + firstComment = false; + } + + replaceAll(line, commentPrefix, ANSI_COLOR_COMMENT_FG); + std::cout << line << ANSI_COLOR_RESET_FG << std::endl; + } + // Code example + else if (line.compare(0, codePrefix.size(), codePrefix) == 0) + { + // Remove trailing backtick (`). + line = line.substr(0, line.size() - 1); + + // Replace first backtick (`) with three spaces for aligned indentation. + replaceAll(line, "`", " "); + std::cout << ANSI_COLOR_CODE_FG + << line + << ANSI_COLOR_RESET_FG + << std::endl + << std::endl; + } + } + return EXIT_SUCCESS; } From a6a019506dc51e511be4a091011d150f468e0280 Mon Sep 17 00:00:00 2001 From: Parth Mehrotra Date: Sun, 3 Jan 2016 15:47:36 -0500 Subject: [PATCH 3/5] fixed hardcoded string --- src/tldr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tldr.cpp b/src/tldr.cpp index 6b1b1bd..eb5b71c 100644 --- a/src/tldr.cpp +++ b/src/tldr.cpp @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) { if (argc <= 1) { - std::cout << "usage: tldr " << std::endl; + std::cout << "usage: " << argv[0] << " " << std::endl; std::cout << "Which page do you want to see?" << std::endl; std::cout << "try: tldr tldr" << std::endl; return EXIT_FAILURE; From 64a8640a0075bdcb6d05a5511569881663c75217 Mon Sep 17 00:00:00 2001 From: Parth Mehrotra Date: Sun, 3 Jan 2016 23:02:26 -0500 Subject: [PATCH 4/5] missed a hardcoded string --- src/tldr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tldr.cpp b/src/tldr.cpp index eb5b71c..bed3c78 100644 --- a/src/tldr.cpp +++ b/src/tldr.cpp @@ -51,7 +51,7 @@ int main(int argc, char* argv[]) { std::cout << "usage: " << argv[0] << " " << std::endl; std::cout << "Which page do you want to see?" << std::endl; - std::cout << "try: tldr tldr" << std::endl; + std::cout << "try: " << arg[0] << " tldr" << std::endl; return EXIT_FAILURE; } From 331d1ee87f68965c6ee595ea43772d480cf3ac0d Mon Sep 17 00:00:00 2001 From: Parth Mehrotra Date: Mon, 4 Jan 2016 11:02:44 -0500 Subject: [PATCH 5/5] fixed typo --- src/tldr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tldr.cpp b/src/tldr.cpp index bed3c78..b7cb903 100644 --- a/src/tldr.cpp +++ b/src/tldr.cpp @@ -51,7 +51,7 @@ int main(int argc, char* argv[]) { std::cout << "usage: " << argv[0] << " " << std::endl; std::cout << "Which page do you want to see?" << std::endl; - std::cout << "try: " << arg[0] << " tldr" << std::endl; + std::cout << "try: " << argv[0] << " tldr" << std::endl; return EXIT_FAILURE; }