123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*
- * BIBISH Is [a] Bible Interactive SHell, a front-end for the SWORD Project
- * inspired by Debian's bible package
- * Copyright (C) 2015 David "Judah's Shadow" Blue <yudahsshadow@gmx.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation version 2.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
- //Standard/STL includes
- #include <iostream>
- #include <string>
- #include <list>
- //SWORD Project includes
- #include <swmgr.h>
- #include <markupfiltmgr.h>
- //Project includes
- #include "interface.h"
- #include "display.h"
- #include "pager.h"
- #include "../back/passage.h"
- #include "../back/library.h"
- #include "../back/parser.h"
- #include "../back/types.h"
- #include "../back/search.h"
- void Interface::initalize() {
- configLines();
- std::cout << "Initializing SWORD, please wait..." << std::endl;
- this->swordLibrary = new sword::SWMgr(new sword::MarkupFilterMgr
- (sword::FMT_PLAIN));
-
- library.setSwordLibrary(*swordLibrary);
- library.passage.setLibrary(*swordLibrary);
-
- std::cout << "Initialized, proceeding to shell..." << std::endl;
- // this->display = new Display;
- }
- void Interface::configLines() {
- uint maxLine = 1000;
- uint lineCount = 0;
- for(int i = maxLine; i >= 1; i--) {
- std::cout << i << std::endl;
- }
- std::cout << "Enter the number at the top of the screen: ";
- std::string input;
- std::getline(std::cin, input);
- lineCount = std::stoi(input);
- display.setSize(lineCount + 1);
- }
- std::string Interface::processCommand(std::string command) {
- std::string validCommands[6];
- std::string text = "";
- std::string ref = "";
- Library library;
- Parser commandParser;
- Parser worksParser;
- std::string commandPart;
- std::list<std::string> modules;
- std::list<std::string> parsedCommand;
- parsedCommand = commandParser.parseCommand(command);
- commandPart = parsedCommand.front();
- parsedCommand.pop_front();
- std::string tempBibles;
- validCommands[0] = "quit";
- validCommands[1] = "show";
- validCommands[2] = "?";
- validCommands[3] = "list";
- validCommands[4] = "select";
- validCommands[5] = "search";
- if(commandPart == validCommands[0]) {
- //since we're quitting do nothing here
- return commandPart;
- } else if(commandPart == validCommands[1]) {
- int errSpaces = 0;
- if(parsedCommand.empty()) {
- display.displayHeader();
- std::cout << "No reference Specified";
- std::cout << std::endl;
- errSpaces++;
- } else {
- ref = parsedCommand.front();
- }
- if(selectedVersion == "") {
- errSpaces++;
- std::cerr << "Error: No version selected. (Try select)";
- std::cerr << std::endl;
- display.displaySpacer(errSpaces);
- return commandPart;
- }
- library.passage.setVersion(selectedVersion);
- text = library.passage.getText(ref);
-
- Pager textPager;
- std::list<page> pagedText;
- textPager.setSize(display.getSize());
- pagedText = textPager.getPagedText(text);
- display.displayPages(pagedText);
- return commandPart;
- } else if(commandPart == validCommands[2]) {
- display.displayHelp();
- return commandPart;
- } else if(commandPart == validCommands[3]) {
- int numModules = 0;
- if(parsedCommand.front() == "bibles") {
- modules = library.getBibles();
- }
- else if (parsedCommand.front() == "commentaries") {
- modules = library.getCommentaries();
- }
- if(modules.empty()) {
- std::cerr << "No modules of type ";
- std::cerr << parsedCommand.front();
- std:: cerr << " found. Please install in another front-end.";
- std::cerr << std::endl;
- display.displaySpacer(1);
- return "-3";
- }
- else {
- numModules = modules.size();
- }
- std::string curBible;
- while(!modules.empty()) {
- curBible = modules.front();
- std::cout << curBible;
- std::cout << std::endl;
- modules.pop_front();
- }
- display.displaySpacer(numModules);
- return commandPart;
- } else if (commandPart == validCommands[4]) {
- //TODO: Stop assuming bibles here then handle actual arguments
- std::string selectedWork;
- if(parsedCommand.empty()) {
- std::cerr << "No module provided (Try list)" << std::endl;
- display.displaySpacer(1);
- }
- else {
- selectedWork = parsedCommand.front();
-
- //Check to make sure the module is, in fact, valid before
- //continuing on to prevent crashing later
- //<fife>Nip it in the bud!</fife>
- if(library.isModuleValid(selectedWork)) {
- selectedVersion = selectedWork;
- display.displaySpacer();
- }
- else {
- //Module didn't come up, alert the user and bail out early.'
- std::cerr << "Module Name is invalid (Try list)" << std::endl;
- return commandPart;
- }
- }
- return commandPart;
- }
- else if(commandPart == validCommands[5]) {
- if(selectedVersion != "") {
- Pager resultsPager;
- std::list<page> pagedResults;
- std::string results = "";
- std::string searchTerms = "";
-
- resultsPager.setSize(display.getSize());
-
- library.searcher.setModule(selectedVersion);
- library.searcher.setDisplay(display);
-
- //If no argument is provided to the command, prompt for the
- //search terms, otherwise recombine the arguments into a string
- if (parsedCommand.empty()) {
- display.displayHeader();
- display.displaySpacer(2);
- std::cout << "Enter a word or phrase to search for: ";
- std::getline(std::cin,searchTerms);
- }
- else {
- searchTerms = parsedCommand.front();
- }
- //TODO: Make this more than references or an option to do text or
- //reference results or both.
- results = library.searcher.search(searchTerms);
- pagedResults = resultsPager.getPagedText(results);
-
- display.clearScreen();
- display.displayHeader();
- display.displayPages(pagedResults);
-
- return commandPart;
- }
- else {
- std::cerr << "Error: No Module Selected" << std::endl;
- std::cerr << "Try select." << std::endl;
- return command;
- }
- }
- else {
- //Invalid command head out.
- return "-1";
- }
- }
- int Interface::runInterface() {
- int returnCode = 0;
- std::string command = "";
- //Initialize the interface
- initalize();
- display.clearScreen();
- display.displayHeader();
- display.displaySpacer();
- display.displayPrompt();
- std::getline(std::cin, command);
- //main program loop keep going until a quit command is given
- while(command != "quit") {
- display.clearScreen();
- display.displayHeader();
- command = processCommand(command);
- if(command == "-1") {
- std::cerr << "Error! invalid command! (Try ?)" << std::endl;
- display.displaySpacer(1);
- } else if(command == "-2") {
- //Specified module not found, since we can't install yet bail out
- std::cerr << "Module not found. Aborting.." << std::endl;
- returnCode = -1;
- break;
- } else if(command == "-3") {
- returnCode = -2;
- std::cerr << "No relevant modules found. Aborting.." << std::endl;
- break;
- }
- display.displayPrompt();
- std::getline(std::cin, command);
- }
- delete swordLibrary;
- return returnCode;
- }
|