
% a coloured inttype label with \item in the payload table
\newcommand{\cfieldx}[1]{{\color{RubineRed} \texttt{#1}}}
\newcommand{\cfield}[1]{\item \cfieldx{#1}\,} % \tabto{1cm}

\newcommand{\cname}[1]{\textbf{#1}\newline}

% https://tex.stackexchange.com/questions/157389/how-to-center-column-values-in-a-table
% P will be a centered column that can have width
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

% this is put before a response that follows a request
% this is needed for nice looking spacing
\newcommand*{\cjoin}{\null\vspace*{-2pt}}

% This is a table of commands of events
\newenvironment{cmdlist}
{
	\tabulinesep=5pt
	\begin{longtabu} to \textwidth {P{2.2em} X[3] X[3,l]}			
		\toprule		
		\textbf{Code} & \textbf{Function} & \textbf{Structure} \\
		\midrule
		\endhead
		
		\bottomrule	
		\endfoot
}{
	\end{longtabu}
}

% a list of generic payload fields
\newenvironment{pldlist}
{
	\begin{itemize}[
		leftmargin=.7cm,
		rightmargin=1mm,
		nosep		
	]
}
{
	\end{itemize}
}


% a list of request fields, with a caption
\newenvironment{cmditemlistenv}[1]
{
	\begin{minipage}[t]{\linewidth}
		
	\begin{flushleft} % fix weird spacing in wrapped lines
	
	\textit{#1} % the caption, like Request: or Response:
	\begin{pldlist}	
}
{
	\end{pldlist}
	\end{flushleft}

	% the minipage somehow fixes vertical alignment 
	% and also removes mysterious spacing around the itemize
	\end{minipage}
}


% a list of request fields, with a caption
\newenvironment{cmdreq}
{
	\begin{cmditemlistenv}{Request:}	
}
{
	\end{cmditemlistenv}	
}

% a list of request fields, with a caption
\newenvironment{cmdresp}
{
	\begin{cmditemlistenv}{Response:}	
}
{
	\end{cmditemlistenv}
}

% a list of payload fields, with a caption
\newenvironment{cmdpld}
{
	\begin{cmditemlistenv}{Payload:}		
}
{
	\end{cmditemlistenv}
}


%% hack from https://tex.stackexchange.com/questions/145812/using-fbox-in-a-newenvironment
% to allow using fbox inside a environment
\newsavebox{\mybox}

% a list of payload fields, with a caption
\newenvironment{boxedpayload}[1][]
{
	\begingroup\setlength{\fboxsep}{0pt}
	\indent
	\begin{lrbox}{\mybox}
	\begin{minipage}{\textwidth - \parindent*2}\null
	\ifthenelse{\equal{#1}{}}{
		\vspace{3pt} % no title
	}{
		% with title
		\hspace{6pt}\textit{#1}
		\vspace{-6pt}\newline
		\noindent\rule{\textwidth}{.4pt}		
		\vspace{-\baselineskip}
		\vspace{-4pt}
	}
	\begin{itemize}[
		itemsep=0pt, % this is not actually 0, for some reason. that's okay
		leftmargin=24pt,
		rightmargin=1mm
	]
}
{
	\end{itemize}
	\vspace{-\baselineskip}
	\vspace{14pt}
	\end{minipage}
	\end{lrbox}\fbox{\usebox{\mybox}}
	\endgroup
}
