Home /Modules/common-rails/CommonRailsPrinting.cpp

CommonRailsPrinting.cpp

common-rails/heritage/printing/cpp/CommonRailsPrinting.cpp · 23 line(s) · c

#include "CommonRailsPrinting.hpp"
#include <iomanip>
#include <sstream>
namespace commonrails { namespace printing {
/* Group a non-negative QUANTITY with thousands separators (441 -> "441",
 * 12345 -> "12,345") for human reading. Used only for counted quantities (the
 * progress cell count/total), never for identifier tokens such as the
 * zero-padded Object ID -- those are strings-of-digits, not quantities, and are
 * emitted verbatim. */
static std::string grp(long long v){
    std::string s=std::to_string(v<0?-v:v), out; int n=(int)s.size();
    for(int i=0;i<n;++i){ if(i>0 && (n-i)%3==0) out.push_back(','); out.push_back(s[(size_t)i]); }
    return v<0 ? "-"+out : out;
}
std::string PrintField::pad(const std::string&s,int w){if((int)s.size()>=w)return s;return s+std::string(w-s.size(),' ');}
void PrintLine::write(std::ostream&o,const std::string&s,int w){if(s.size()<=static_cast<size_t>(w)){o<<s<<std::string(w-s.size(),' ')<<'\n';return;}size_t p=0;while(p<s.size()){size_t rem=s.size()-p,n=std::min<size_t>(w,rem);if(rem>static_cast<size_t>(w)){size_t c=n;while(c>0&&s[p+c-1]!=' '&&(p+c>=s.size()||s[p+c]!=' '))--c;if(c)n=c;}o<<s.substr(p,n)<<std::string(w-n,' ')<<'\n';p+=n;while(p<s.size()&&s[p]==' ')++p;}}
int PrintProgress::clamp(int p){return std::clamp(p,0,100);} int PrintProgress::cells(int p){return clamp(p)*PrintLayout::cells/100;}
void PrintProgress::write(std::ostream&o,int p){p=clamp(p);o<<"  progress "<<p<<"% ("<<grp(cells(p))<<"/"<<grp(PrintLayout::cells)<<" cells)\n";}
void PrintComponent::write(std::ostream&o,const std::string&n,unsigned long id,unsigned long d,const std::string&m){std::ostringstream s;s<<"-- : [Object ID: "<<std::setw(10)<<std::setfill('0')<<id<<"] [Date: "<<d<<"] [Current: @"<<n<<"] . "<<m<<" .";PrintLine::write(o,s.str());}
void PrintFormatter::status(std::ostream&o,PrintState s,const std::string&m){const char*n="ERROR";switch(s){case PrintState::START:n="START";break;case PrintState::WORKING:n="WORKING";break;case PrintState::PROGRESS:n="PROGRESS";break;case PrintState::COMPLETE:n="COMPLETE";break;case PrintState::WARN:n="WARN";break;default:break;}PrintLine::write(o,std::string("[")+n+"] "+m);}
void PrintRenderer::square(std::ostream&o,int f){f=std::clamp(f,0,441);for(int r=0;r<21;r++){for(int c=0;c<21;c++){int idx=(20-r)*21+(20-c);o<<(idx<f?PrintGlyphs::full:PrintGlyphs::empty);}o<<'\n';}}
}}