aboutsummaryrefslogtreecommitdiff
path: root/bible.cc
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-02-17 20:16:29 -0500
committerYour Name <you@example.com>2021-02-17 20:16:29 -0500
commitbc4aab5d69cdab28ab94d3745a1e8c92ca749003 (patch)
tree4df904ea521f03da9e561a968562a6f8e8b175cb /bible.cc
parent3339dc7f29540758f59e2c20462d99643a8d225e (diff)
downloadlibbible-bc4aab5d69cdab28ab94d3745a1e8c92ca749003.tar.gz
libbible-bc4aab5d69cdab28ab94d3745a1e8c92ca749003.tar.bz2
libbible-bc4aab5d69cdab28ab94d3745a1e8c92ca749003.zip
Added line wrapping
Diffstat (limited to 'bible.cc')
-rw-r--r--bible.cc82
1 files changed, 73 insertions, 9 deletions
diff --git a/bible.cc b/bible.cc
index 2a282c7..d82d15f 100644
--- a/bible.cc
+++ b/bible.cc
@@ -1,7 +1,11 @@
#include "libbible.h"
#include <string>
+#include <sstream>
#include <algorithm>
#include <getopt.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <iostream>
using namespace std;
@@ -101,6 +105,37 @@ void removeMod(string mod) {
libbible::uninstallMod(mod);
}
+// Stolen from O'Reilly
+void textWrap(istream& in, ostream& out, size_t width) {
+ string tmp;
+ char cur = '\0';
+ char last = '\0';
+ size_t i = 0;
+
+ while(in.get(cur)) {
+ if((unsigned char) cur < 0x80 || (unsigned char) cur >= 0xC0) {
+ i++;
+ }
+ if(isspace(cur) && !isspace(last)) {
+ out << tmp;
+ tmp.clear();
+ } else if(i >= width) {
+ tmp.erase(0, tmp.find_first_not_of(" "));
+ out << '\n';
+ i = tmp.length();
+ }
+ if(cur == '\n') {
+ i = 0;
+ }
+ if(cur == '\033') {
+ i -= 6;
+ }
+ tmp += cur;
+ last = cur;
+ }
+ out << tmp;
+}
+
int main(int argc, char* argv[]) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
@@ -186,9 +221,11 @@ int main(int argc, char* argv[]) {
int verse = 0;
const char* indent = " ";
bool isNewline = true;
+ stringstream out;
for(auto tex : text) {
if(!omitVerseNums && tex.chapter != chapter) {
- printf("\n%s Chapter %d:\n", tex.book.c_str(), tex.chapter);
+ //printf("\n%s Chapter %d:\n", tex.book.c_str(), tex.chapter);
+ out << "\n" << tex.book << " Chapter " << tex.chapter << ":\n";
}
bool isParagraph = false;
bool isIndent = false;
@@ -208,35 +245,62 @@ int main(int argc, char* argv[]) {
if(isIndent) {
isParagraph = false;
if(isNewline) {
- printf(indent);
+ //printf(indent);
+ out << indent;
}
}
if(isParagraph) {
- printf(indent);
+ //printf(indent);
+ out << indent;
}
if(isDivineName) {
transform(tex.text.begin(), tex.text.end(), tex.text.begin(), ::toupper);
}
if(isJesus) {
- printf("\033[;31m");
+ //printf("\033[;31m");
+ out << "\033[;31m";
}
if(omitVerseNums && tex.verse != verse) {
- printf(" ");
+ //printf(" ");
+ out << " ";
} else if(!omitVerseNums && tex.verse != verse) {
- printf(" (%d) ", tex.verse);
+ //printf(" (%d) ", tex.verse);
+ out << " (" << tex.verse << ") ";
}
chapter = tex.chapter;
verse = tex.verse;
- printf(tex.text.c_str());
+ //printf(tex.text.c_str());
+ out << tex.text;
if(tex.text.back() == '\n') {
isNewline = true;
} else {
isNewline = false;
}
if(isJesus) {
- printf("\033[0m");
+ //printf("\033[0m");
+ out << "\033[0m";
}
}
- printf("\n");
+ //printf("\n");
+ out << "\n";
+
+ // Get window size
+ struct winsize size;
+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
+ // size.ws_col is number of columns, or 0 if it's a pipe
+ int cols = size.ws_col;
+ // If terminal is too small, treat it like a pipe
+ if(cols < 10) {
+ cols = 0;
+ }
+
+ // Now print
+ if(cols == 0) {
+ cout << out.str();
+ } else {
+ stringstream out2;
+ textWrap(out, out2, cols);
+ cout << out2.str();
+ }
return 0;
}