esv: split utility functions into separate module
This commit is contained in:
parent
1ed36260f3
commit
d8e267d5ba
3 changed files with 95 additions and 76 deletions
78
esv.d
78
esv.d
|
@ -27,11 +27,12 @@ import std.format : format;
|
|||
import std.getopt : getopt, GetOptException;
|
||||
import std.path : baseName, dirName, expandTilde, isValidPath;
|
||||
import std.process : environment, executeShell;
|
||||
import std.stdio : writef, writeln, writefln, File;
|
||||
import std.stdio : writeln, writefln;
|
||||
import std.string : splitLines;
|
||||
|
||||
import esvapi;
|
||||
import initial;
|
||||
import util;
|
||||
|
||||
import cf = config;
|
||||
|
||||
|
@ -48,13 +49,6 @@ bool rFlag, RFlag; /* passage references */
|
|||
string sFlag; /* search passages */
|
||||
bool VFlag; /* show version */
|
||||
|
||||
string[] mainArgs;
|
||||
File stderr;
|
||||
|
||||
version (OpenBSD) {
|
||||
immutable(char) *promises;
|
||||
}
|
||||
|
||||
int
|
||||
main(string[] args)
|
||||
{
|
||||
|
@ -63,18 +57,7 @@ main(string[] args)
|
|||
INIUnit ini;
|
||||
ESVApi esv;
|
||||
|
||||
mainArgs = args;
|
||||
|
||||
version (OpenBSD) () @trusted {
|
||||
import core.sys.openbsd.unistd : pledge;
|
||||
import std.string : toStringz;
|
||||
|
||||
promises = toStringz("stdio rpath wpath cpath inet dns tty proc exec prot_exec");
|
||||
pledge(promises, null);
|
||||
}();
|
||||
|
||||
/* @safe way of opening stderr on Unix */
|
||||
stderr = File("/dev/stderr", "w");
|
||||
sharedInit(args);
|
||||
|
||||
/* Parse command-line options */
|
||||
try {
|
||||
|
@ -93,7 +76,7 @@ main(string[] args)
|
|||
"V", &VFlag,
|
||||
);
|
||||
} catch (GetOptException e) {
|
||||
string opt = extractOpt(e);
|
||||
string opt = extractFlag(e);
|
||||
|
||||
enforceDie(!e.msg.startsWith("Unrecognized option"),
|
||||
"unknown option " ~ opt);
|
||||
|
@ -245,51 +228,6 @@ key = %s
|
|||
return 0;
|
||||
}
|
||||
|
||||
private void
|
||||
warn(string mesg)
|
||||
{
|
||||
stderr.writeln(baseName(mainArgs[0]) ~ ": " ~ mesg);
|
||||
}
|
||||
|
||||
private void
|
||||
die(string mesg) @trusted
|
||||
{
|
||||
import core.runtime : Runtime;
|
||||
import core.stdc.stdlib : exit;
|
||||
|
||||
warn(mesg);
|
||||
Runtime.terminate();
|
||||
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)
|
||||
{
|
||||
import std.format : format;
|
||||
|
||||
if (!cond)
|
||||
die(format(fmt, a));
|
||||
}
|
||||
|
||||
private string
|
||||
extractOpt(in GetOptException e)
|
||||
{
|
||||
import std.regex : matchFirst;
|
||||
|
||||
return e.msg.matchFirst("-.")[0];
|
||||
}
|
||||
|
||||
private void
|
||||
onLineLength(string flag, string value)
|
||||
{
|
||||
|
@ -299,11 +237,3 @@ onLineLength(string flag, string value)
|
|||
catch (ConvException e)
|
||||
die("illegal argument to -l option -- must be a positive integer");
|
||||
}
|
||||
|
||||
private string
|
||||
parseBook(in string book)
|
||||
{
|
||||
import std.string : tr;
|
||||
|
||||
return book.tr("-_", " ");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue