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
89
util.d
Normal file
89
util.d
Normal file
|
@ -0,0 +1,89 @@
|
|||
module util;
|
||||
|
||||
import std.getopt : GetOptException;
|
||||
import std.stdio : File;
|
||||
|
||||
public @safe:
|
||||
|
||||
File stderr;
|
||||
|
||||
private {
|
||||
string[] mainArgs;
|
||||
|
||||
version (OpenBSD) {
|
||||
immutable(char) *promises;
|
||||
}
|
||||
}
|
||||
|
||||
/++
|
||||
+ Common initialisation function shared between esv and esvsearch
|
||||
+/
|
||||
void
|
||||
sharedInit(string[] args)
|
||||
{
|
||||
mainArgs = args;
|
||||
stderr = File("/dev/stderr", "w");
|
||||
|
||||
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);
|
||||
}();
|
||||
}
|
||||
|
||||
void
|
||||
enforceDie(A...)(bool cond, string fmt, A a)
|
||||
{
|
||||
import std.format : format;
|
||||
|
||||
if (!cond)
|
||||
die(format(fmt, a));
|
||||
}
|
||||
|
||||
string
|
||||
extractFlag(in GetOptException e)
|
||||
{
|
||||
import std.regex : matchFirst;
|
||||
|
||||
return e.msg.matchFirst("-.")[0];
|
||||
}
|
||||
|
||||
string
|
||||
parseBook(in string book)
|
||||
{
|
||||
import std.string : tr;
|
||||
|
||||
return book.tr("-_", " ");
|
||||
}
|
||||
|
||||
ushort
|
||||
terminalColumns() @trusted
|
||||
{
|
||||
import core.sys.posix.sys.ioctl;
|
||||
|
||||
winsize w;
|
||||
|
||||
ioctl(1, TIOCGWINSZ, &w);
|
||||
return w.ws_col > 72 ? 72 : w.ws_col;
|
||||
}
|
||||
|
||||
void
|
||||
warn(string mesg)
|
||||
{
|
||||
import std.path : baseName;
|
||||
|
||||
stderr.writeln(baseName(mainArgs[0]) ~ ": " ~ mesg);
|
||||
}
|
||||
|
||||
void
|
||||
die(string mesg) @trusted
|
||||
{
|
||||
import core.runtime : Runtime;
|
||||
import core.stdc.stdlib : exit;
|
||||
|
||||
warn(mesg);
|
||||
Runtime.terminate();
|
||||
exit(1);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue