function len = showfitinfo(doheader,doval,dosc,dofitalpha0,dofitalphas,...
    iter,telapsed,relDeltaObjective,ppl_train,ppl_val,alpha0,nalpha_iter)
%showfitinfo Takes parameters for the iterative display, displays fit info
% in the command window, and returns the number of characters in the
% displayed line.

%   Copyright 2017 The MathWorks, Inc.

tab12 = sprintf("%12s|","");

%
% Header
%
hline1 = "|";
hline2 = "|";
hline3 = "|";

if doheader
    % Iteration/minibatch index
    if dosc
        add1 = " Minibatch  |";
    else
        add1 = " Iteration  |";
    end
    hline1 = hline1 + add1;
    hline2 = hline2 + tab12;
    hline3 = hline3 + tab12;
    
    % Time per iteration/minibatch
    hline1 = hline1 + "  Time per  |";
    if dosc
        add2 = " minibatch  |";
    else
        add2 = " iteration  |";
    end
    hline2 = hline2 + add2;
    hline3 = hline3 + " (seconds)  |";

    % Change in likelihood
    hline1 = hline1 + "  Relative  |";
    hline2 = hline2 + " change in  |";
    hline3 = hline3 + "   log(L)   |";

    % Training perplexity
    if dosc
        add1 = " Minibatch  |";
    else
        add1 = "  Training  |";
    end
    hline1 = hline1 + add1;
    hline2 = hline2 + " perplexity |";
    hline3 = hline3 + tab12;

    % Validation perplexity
    if doval
        hline1 = hline1 + " Validation |";
        hline2 = hline2 + " perplexity |";
        hline3 = hline3 + tab12;
    end

    % Topic concentration
    if dofitalpha0
        hline1 = hline1 + "     Topic     |";
        hline2 = hline2 + " concentration |";
        tab15 = sprintf("%15s|","");
        hline3 = hline3 + tab15;
    end
    
    % dofitalpha0=true && dofitalphas=false has been ruled out before we
    % got here. No need to address this case here.

    % Topic concentration/probability iterations
    if dofitalphas
        hline1 = hline1 + "     Topic     |";
        hline2 = hline2 + " concentration |";
        hline3 = hline3 + "   iterations  |";        
    end
    
    len = strlength(hline1);
    
    hline1 = hline1 + "\n";
    hline2 = hline2 + "\n";
    hline3 = hline3 + "\n";
    separator = string(repmat('=',1,len)) + '\n';
    fprintf(separator);
    fprintf(hline1);
    fprintf(hline2);
    fprintf(hline3);
    fprintf(separator);
end

%
% Data
%
dataline = "|";

% Iteration/minibatch index
dataline = dataline + sprintf(' %10i |',iter);

% Time per iteration/minibatch
dataline = dataline + sprintf(' %10.2f |',telapsed);

% Change in likelihood
if isempty(relDeltaObjective)
    dataline = dataline + tab12;
else
    dataline = dataline + sprintf(' %10.4e |',relDeltaObjective);
end

% Training perplexity
dataline = dataline + sprintf(' %10.3e |',ppl_train);

% Validation perplexity
if doval
    dataline = dataline + sprintf(' %10.3e |',ppl_val);
end

% Topic concentration
if dofitalpha0
    dataline = dataline + sprintf(' %13.3f |',alpha0);
end

% Topic concentration/probability iterations
if dofitalphas
    dataline = dataline + sprintf(' %13i |',nalpha_iter);
end

% Get length
len = strlength(dataline);

% End of line
dataline = dataline + "\n";

% Show
fprintf(dataline);

end
