interface.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * BIBISH Is [a] Bible Interactive SHell, a front-end for the SWORD Project
  3. * inspired by Debian's bible package
  4. * Copyright (C) 2015 David "Judah's Shadow" Blue <yudahsshadow@gmx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. //Standard/STL includes
  21. #include <iostream>
  22. #include <string>
  23. #include <list>
  24. //SWORD Project includes
  25. #include <swmgr.h>
  26. #include <markupfiltmgr.h>
  27. //Project includes
  28. #include "interface.h"
  29. #include "display.h"
  30. #include "pager.h"
  31. #include "../back/passage.h"
  32. #include "../back/library.h"
  33. #include "../back/parser.h"
  34. #include "../back/types.h"
  35. #include "../back/search.h"
  36. void Interface::initalize() {
  37. configLines();
  38. std::cout << "Initializing SWORD, please wait..." << std::endl;
  39. this->swordLibrary = new sword::SWMgr(new sword::MarkupFilterMgr
  40. (sword::FMT_PLAIN));
  41. library.setSwordLibrary(*swordLibrary);
  42. library.passage.setLibrary(*swordLibrary);
  43. std::cout << "Initialized, proceeding to shell..." << std::endl;
  44. // this->display = new Display;
  45. }
  46. void Interface::configLines() {
  47. uint maxLine = 1000;
  48. uint lineCount = 0;
  49. for(int i = maxLine; i >= 1; i--) {
  50. std::cout << i << std::endl;
  51. }
  52. std::cout << "Enter the number at the top of the screen: ";
  53. std::string input;
  54. std::getline(std::cin, input);
  55. lineCount = std::stoi(input);
  56. display.setSize(lineCount + 1);
  57. }
  58. std::string Interface::processCommand(std::string command) {
  59. std::string validCommands[6];
  60. std::string text = "";
  61. std::string ref = "";
  62. Library library;
  63. Parser commandParser;
  64. Parser worksParser;
  65. std::string commandPart;
  66. std::list<std::string> modules;
  67. std::list<std::string> parsedCommand;
  68. parsedCommand = commandParser.parseCommand(command);
  69. commandPart = parsedCommand.front();
  70. parsedCommand.pop_front();
  71. std::string tempBibles;
  72. validCommands[0] = "quit";
  73. validCommands[1] = "show";
  74. validCommands[2] = "?";
  75. validCommands[3] = "list";
  76. validCommands[4] = "select";
  77. validCommands[5] = "search";
  78. if(commandPart == validCommands[0]) {
  79. //since we're quitting do nothing here
  80. return commandPart;
  81. } else if(commandPart == validCommands[1]) {
  82. int errSpaces = 0;
  83. if(parsedCommand.empty()) {
  84. display.displayHeader();
  85. std::cout << "No reference Specified";
  86. std::cout << std::endl;
  87. errSpaces++;
  88. } else {
  89. ref = parsedCommand.front();
  90. }
  91. if(selectedVersion == "") {
  92. errSpaces++;
  93. std::cerr << "Error: No version selected. (Try select)";
  94. std::cerr << std::endl;
  95. display.displaySpacer(errSpaces);
  96. return commandPart;
  97. }
  98. library.passage.setVersion(selectedVersion);
  99. text = library.passage.getText(ref);
  100. Pager textPager;
  101. std::list<page> pagedText;
  102. textPager.setSize(display.getSize());
  103. pagedText = textPager.getPagedText(text);
  104. display.displayPages(pagedText);
  105. return commandPart;
  106. } else if(commandPart == validCommands[2]) {
  107. display.displayHelp();
  108. return commandPart;
  109. } else if(commandPart == validCommands[3]) {
  110. int numModules = 0;
  111. if(parsedCommand.front() == "bibles") {
  112. modules = library.getBibles();
  113. }
  114. else if (parsedCommand.front() == "commentaries") {
  115. modules = library.getCommentaries();
  116. }
  117. if(modules.empty()) {
  118. std::cerr << "No modules of type ";
  119. std::cerr << parsedCommand.front();
  120. std:: cerr << " found. Please install in another front-end.";
  121. std::cerr << std::endl;
  122. display.displaySpacer(1);
  123. return "-3";
  124. }
  125. else {
  126. numModules = modules.size();
  127. }
  128. std::string curBible;
  129. while(!modules.empty()) {
  130. curBible = modules.front();
  131. std::cout << curBible;
  132. std::cout << std::endl;
  133. modules.pop_front();
  134. }
  135. display.displaySpacer(numModules);
  136. return commandPart;
  137. } else if (commandPart == validCommands[4]) {
  138. //TODO: Stop assuming bibles here then handle actual arguments
  139. std::string selectedWork;
  140. if(parsedCommand.empty()) {
  141. std::cerr << "No module provided (Try list)" << std::endl;
  142. display.displaySpacer(1);
  143. }
  144. else {
  145. selectedWork = parsedCommand.front();
  146. //Check to make sure the module is, in fact, valid before
  147. //continuing on to prevent crashing later
  148. //<fife>Nip it in the bud!</fife>
  149. if(library.isModuleValid(selectedWork)) {
  150. selectedVersion = selectedWork;
  151. display.displaySpacer();
  152. }
  153. else {
  154. //Module didn't come up, alert the user and bail out early.'
  155. std::cerr << "Module Name is invalid (Try list)" << std::endl;
  156. return commandPart;
  157. }
  158. }
  159. return commandPart;
  160. }
  161. else if(commandPart == validCommands[5]) {
  162. if(selectedVersion != "") {
  163. Pager resultsPager;
  164. std::list<page> pagedResults;
  165. std::string results = "";
  166. std::string searchTerms = "";
  167. resultsPager.setSize(display.getSize());
  168. library.searcher.setModule(selectedVersion);
  169. library.searcher.setDisplay(display);
  170. //If no argument is provided to the command, prompt for the
  171. //search terms, otherwise recombine the arguments into a string
  172. if (parsedCommand.empty()) {
  173. display.displayHeader();
  174. display.displaySpacer(2);
  175. std::cout << "Enter a word or phrase to search for: ";
  176. std::getline(std::cin,searchTerms);
  177. }
  178. else {
  179. searchTerms = parsedCommand.front();
  180. }
  181. //TODO: Make this more than references or an option to do text or
  182. //reference results or both.
  183. results = library.searcher.search(searchTerms);
  184. pagedResults = resultsPager.getPagedText(results);
  185. display.clearScreen();
  186. display.displayHeader();
  187. display.displayPages(pagedResults);
  188. return commandPart;
  189. }
  190. else {
  191. std::cerr << "Error: No Module Selected" << std::endl;
  192. std::cerr << "Try select." << std::endl;
  193. return command;
  194. }
  195. }
  196. else {
  197. //Invalid command head out.
  198. return "-1";
  199. }
  200. }
  201. int Interface::runInterface() {
  202. int returnCode = 0;
  203. std::string command = "";
  204. //Initialize the interface
  205. initalize();
  206. display.clearScreen();
  207. display.displayHeader();
  208. display.displaySpacer();
  209. display.displayPrompt();
  210. std::getline(std::cin, command);
  211. //main program loop keep going until a quit command is given
  212. while(command != "quit") {
  213. display.clearScreen();
  214. display.displayHeader();
  215. command = processCommand(command);
  216. if(command == "-1") {
  217. std::cerr << "Error! invalid command! (Try ?)" << std::endl;
  218. display.displaySpacer(1);
  219. } else if(command == "-2") {
  220. //Specified module not found, since we can't install yet bail out
  221. std::cerr << "Module not found. Aborting.." << std::endl;
  222. returnCode = -1;
  223. break;
  224. } else if(command == "-3") {
  225. returnCode = -2;
  226. std::cerr << "No relevant modules found. Aborting.." << std::endl;
  227. break;
  228. }
  229. display.displayPrompt();
  230. std::getline(std::cin, command);
  231. }
  232. delete swordLibrary;
  233. return returnCode;
  234. }