aboutsummaryrefslogtreecommitdiff
path: root/bible.cc
diff options
context:
space:
mode:
Diffstat (limited to 'bible.cc')
-rw-r--r--bible.cc58
1 files changed, 31 insertions, 27 deletions
diff --git a/bible.cc b/bible.cc
index d82d15f..7c8a89f 100644
--- a/bible.cc
+++ b/bible.cc
@@ -6,6 +6,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include <iostream>
+#include "utf8.h"
using namespace std;
@@ -107,33 +108,45 @@ void removeMod(string mod) {
// Stolen from O'Reilly
void textWrap(istream& in, ostream& out, size_t width) {
- string tmp;
+ string word;
+ string line;
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(isspace(cur)) {
+ word.clear();
}
if(cur == '\n') {
- i = 0;
+ out << line << '\n';
+ line.clear();
+ word.clear();
+ continue;
+ }
+ word += cur;
+ line += cur;
+ // Anything matching \033.*?m doesn't count
+ size_t credits = 0;
+ size_t found = -1;
+ while((found = line.find("\033", found+1)) != string::npos) {
+ size_t first = line.find_first_of("m", found);
+ if(first != string::npos) {
+ credits += first - found + 1;
+ } else {
+ credits += line.size() - found;
+ }
}
- if(cur == '\033') {
- i -= 6;
+ string::iterator end_it = utf8::find_invalid(line.begin(), line.end());
+ i = utf8::distance(line.begin(), end_it) - credits;
+ //printf("Word: %s, i: %ld\n", word.c_str(), i);
+ if(i > width) {
+ word.erase(0, word.find_first_not_of(" "));
+ line.erase(line.find_last_of(" "));
+ out << line << '\n';
+ line = word;
}
- tmp += cur;
- last = cur;
}
- out << tmp;
+ out << line;
}
int main(int argc, char* argv[]) {
@@ -224,7 +237,6 @@ int main(int argc, char* argv[]) {
stringstream out;
for(auto tex : text) {
if(!omitVerseNums && tex.chapter != chapter) {
- //printf("\n%s Chapter %d:\n", tex.book.c_str(), tex.chapter);
out << "\n" << tex.book << " Chapter " << tex.chapter << ":\n";
}
bool isParagraph = false;
@@ -245,31 +257,25 @@ int main(int argc, char* argv[]) {
if(isIndent) {
isParagraph = false;
if(isNewline) {
- //printf(indent);
out << indent;
}
}
if(isParagraph) {
- //printf(indent);
out << indent;
}
if(isDivineName) {
transform(tex.text.begin(), tex.text.end(), tex.text.begin(), ::toupper);
}
if(isJesus) {
- //printf("\033[;31m");
out << "\033[;31m";
}
if(omitVerseNums && tex.verse != verse) {
- //printf(" ");
out << " ";
} else if(!omitVerseNums && tex.verse != verse) {
- //printf(" (%d) ", tex.verse);
out << " (" << tex.verse << ") ";
}
chapter = tex.chapter;
verse = tex.verse;
- //printf(tex.text.c_str());
out << tex.text;
if(tex.text.back() == '\n') {
isNewline = true;
@@ -277,11 +283,9 @@ int main(int argc, char* argv[]) {
isNewline = false;
}
if(isJesus) {
- //printf("\033[0m");
out << "\033[0m";
}
}
- //printf("\n");
out << "\n";
// Get window size