esv: detect passage line length based on terminal width

Implements: https://todo.sr.ht/~jeremy/esv/2
This commit is contained in:
Jeremy Baxter 2024-06-18 18:41:30 +12:00
parent d68b83b722
commit 982273b9d7

32
esv.d
View file

@ -44,6 +44,7 @@ string CFlag; /* config */
bool fFlag, FFlag; /* footnotes */
bool hFlag, HFlag; /* headings */
int lFlag; /* line length */
bool lFlagSpecified;
bool nFlag, NFlag; /* verse numbers */
bool rFlag, RFlag; /* passage references */
string sFlag; /* search passages */
@ -70,7 +71,7 @@ main(string[] args)
import core.sys.openbsd.unistd : pledge;
import std.string : toStringz;
promises = toStringz("stdio rpath wpath cpath inet dns proc exec prot_exec");
promises = toStringz("stdio rpath wpath cpath inet dns tty proc exec prot_exec");
pledge(promises, null);
}();
@ -87,7 +88,7 @@ main(string[] args)
"C", &CFlag,
"F", &FFlag, "f", &fFlag,
"H", &HFlag, "h", &hFlag,
"l", &lFlag,
"l", &onLineLength,
"N", &NFlag, "n", &nFlag,
"R", &RFlag, "r", &rFlag,
"s", &sFlag,
@ -100,8 +101,6 @@ main(string[] args)
"missing argument for option " ~ e.extractOpt());
die(e.msg); /* catch-all */
} catch (ConvException e) {
die("illegal argument to -l option -- must be integer");
}
if (VFlag) {
@ -225,7 +224,8 @@ key = %s
/* Get line-length ([passage]) */
try
esv.opts.i["line-length"] =
ini["passage"].keyAs!int("line-length", 0);
lFlagSpecified ? lFlag :
ini["passage"].keyAs!int("line-length", terminalColumns());
catch (INITypeException e)
die(configPath ~ ": " ~ e.msg);
@ -237,7 +237,6 @@ key = %s
if (HFlag) esv.opts.b["include-headings"] = false;
if (NFlag) esv.opts.b["include-verse-numbers"] = false;
if (RFlag) esv.opts.b["include-passage-references"] = false;
if (lFlag != 0) esv.opts.i["line-length"] = lFlag;
try
writeln(esv.getPassage(args[1].parseBook(), args[2]));
@ -263,6 +262,17 @@ die(string mesg) @trusted
exit(1);
}
private ushort
terminalColumns() @trusted
{
import core.sys.posix.sys.ioctl;
winsize w;
ioctl(1, TIOCGWINSZ, &w);
return w.ws_col > 72 ? 72 : w.ws_col;
}
private void
enforceDie(A...)(bool cond, string fmt, A a)
{
@ -280,6 +290,16 @@ extractOpt(in GetOptException e)
return e.msg.matchFirst("-.")[0];
}
private void
onLineLength(string flag, string value)
{
lFlagSpecified = true;
try
lFlag = value.to!int();
catch (ConvException e)
die("illegal argument to -l option -- must be a positive integer");
}
private string
parseBook(in string book)
{