Skip to content

Instantly share code, notes, and snippets.

@w495
Last active October 24, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w495/7328b76e76aee49657e0bd7a3b46c870 to your computer and use it in GitHub Desktop.
Save w495/7328b76e76aee49657e0bd7a3b46c870 to your computer and use it in GitHub Desktop.
bashline.sty — simple implementation of command line calls for [Xe]LaTeX.
% !TeX encoding = UTF-8
\ProvidesPackage{bashline}[2016/10/24 v. 0.1]
\makeatletter
\newcommand{\bashline@file@name}[1]{%
/tmp/${USER}-${HOSTNAME}-\jobname-#1.tex%
}
\newread\bashline@file
\newcommand{\bashline@command@one}[2][tmp]{%
\immediate\write18{#2 > \bashline@file@name{#1}}
\openin\bashline@file=\bashline@file@name{#1}
% The group localizes the change to \endlinechar
\bgroup
\endlinechar=-1
\read\bashline@file to \localline
% Since everything in the group is local,
% we have to explicitly make the assignment global
\global\let\bashline@result\localline
\egroup
\closein\bashline@file
% Clean up after ourselves
\immediate\write18{rm \bashline@file@name{#1}}
\bashline@result
}
\newcommand{\bashline@command@many}[2][tmp]{%
\immediate\write18{#2 > \bashline@file@name{#1}}
\openin\bashline@file=\bashline@file@name{#1}
% The group localizes the change to \endlinechar
\newcount\linecnt
\bgroup
\endlinechar=-1
\loop\unless\ifeof\bashline@file
\read\bashline@file to \localline%
\localline
\newline
\repeat
\egroup
\closein\bashline@file
% Clean up after ourselves
\immediate\write18{rm \bashline@file@name{#1}}
}
\newcommand{\bashline}[2][tmp]{%
\bashline@command@one[#1]{#2}%
}
\newcommand{\bashlines}[2][tmp]{%
\bashline@command@many[#1]{#2}%
}
\makeatother
\newcommand{\urandomstring}[1]{%
\bashline{cat /dev/urandom | tr -dc "A-Za-z0-9" | fold -c#1 | head -1}%
}
\newcommand{\bashdate}{%
\bashline{date --iso-8601}%
}
\newcommand{\bashdatetime}{%
\bashline{date --iso-8601=seconds}%
}
\newcommand{\commit}{%
\bashline{git describe --dirty }%
}
\newcommand{\commitlog}{%
\bashline{git log -1 --oneline}%
}
\newcommand{\branch}{%
\bashline{git describe --all}%
}
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment