terminal.cpp
cstdlib
string
ftxui/screen/terminal.hpp
sys/ioctl.h
unistd.h
ftxui/screen/terminal.hpp
src/ftxui/screen/terminal.cpp
cstdlib
string
sys/ioctl.h
unistd.h
ftxui
ftxui::Terminal

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<cstdlib>//forgetenv
    #include<string>//forstring,allocator

    #include"ftxui/screen/terminal.hpp"

    #ifdefined(_WIN32)
    #defineWIN32_LEAN_AND_MEAN

    #ifndefNOMINMAX
    #defineNOMINMAX
    #endif

    #include<windows.h>
    #else
    #include<sys/ioctl.h>//forwinsize,ioctl,TIOCGWINSZ
    #include<unistd.h>//forSTDOUT_FILENO
    #endif

    namespaceftxui{

    namespace{

    boolg_cached=false;//NOLINT
    Terminal::Colorg_cached_supported_color;//NOLINT

    Dimensions&FallbackSize(){
    #ifdefined(__EMSCRIPTEN__)
    //Thisdimensionwaschosenarbitrarilytobeabletodisplay:
    //https://arthursonzogni.com/FTXUI/examples
    //Thiswillhavetobeimprovedwhensomeonehastimetoimplementandneed
    //it.
    constexprintfallback_width=140;
    constexprintfallback_height=43;
    #else
    //TheterminalsizeinVT100was80x24.Itisstillusednowadaysby
    //defaultinmanyterminalemulator.That'sagoodchoiceforafallback
    //value.
    constexprintfallback_width=80;
    constexprintfallback_height=24;
    #endif
    staticDimensionsg_fallback_size{
    fallback_width,
    fallback_height,
    };
    returng_fallback_size;
    }

    constchar*Safe(constchar*c){
    return(c!=nullptr)?c:"";
    }

    boolContains(conststd::string&s,constchar*key){
    returns.find(key)!=std::string::npos;
    }

    Terminal::ColorComputeColorSupport(){
    #ifdefined(__EMSCRIPTEN__)
    returnTerminal::Color::TrueColor;
    #endif

    std::stringCOLORTERM=Safe(std::getenv("COLORTERM"));//NOLINT
    if(Contains(COLORTERM,"24bit")||Contains(COLORTERM,"truecolor")){
    returnTerminal::Color::TrueColor;
    }

    std::stringTERM=Safe(std::getenv("TERM"));//NOLINT
    if(Contains(COLORTERM,"256")||Contains(TERM,"256")){
    returnTerminal::Color::Palette256;
    }

    #ifdefined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
    //Microsoftterminalsdonotproperlydeclarethemselvesupportingtrue
    //colors:https://github.com/microsoft/terminal/issues/1040
    //Asafallback,assumemicrosoftterminalaretheonesnotsettingthose
    //variables,andenabletruecolors.
    if(TERM.empty()&&COLORTERM.empty()){
    returnTerminal::Color::TrueColor;
    }
    #endif

    returnTerminal::Color::Palette16;
    }

    }//namespace

    namespaceTerminal{

    ///@briefGettheterminalsize.
    ///@returnTheterminalsize.
    ///@ingroupscreen
    DimensionsSize(){
    #ifdefined(__EMSCRIPTEN__)
    //Thisdimensionwaschosenarbitrarilytobeabletodisplay:
    //https://arthursonzogni.com/FTXUI/examples
    //Thiswillhavetobeimprovedwhensomeonehastimetoimplementandneed
    //it.
    returnFallbackSize();
    #elifdefined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFOcsbi;

    if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&csbi)){
    returnDimensions{csbi.srWindow.Right-csbi.srWindow.Left+1,
    csbi.srWindow.Bottom-csbi.srWindow.Top+1};
    }

    returnFallbackSize();
    #else
    winsizew{};
    constintstatus=ioctl(STDOUT_FILENO,TIOCGWINSZ,&w);//NOLINT
    //Theioctlreturnvalueresultshouldbechecked.Someoperatingsystems
    //don'tsupportTIOCGWINSZ.
    if(w.ws_col==0||w.ws_row==0||status<0){
    returnFallbackSize();
    }
    returnDimensions{w.ws_col,w.ws_row};
    #endif
    }

    ///@briefOverrideterminalsizeincaseauto-detectionfails
    ///@paramfallbackSizeTerminaldimensionstofallbackto
    voidSetFallbackSize(constDimensions&fallbackSize){
    FallbackSize()=fallbackSize;
    }

    ///@briefGetthecolorsupportoftheterminal.
    ///@ingroupscreen
    ColorColorSupport(){
    if(!g_cached){
    g_cached=true;
    g_cached_supported_color=ComputeColorSupport();
    }
    returng_cached_supported_color;
    }

    ///@briefOverrideterminalcolorsupportincaseauto-detectionfails
    ///@ingroupdom
    voidSetColorSupport(Colorcolor){
    g_cached=true;
    g_cached_supported_color=color;
    }

    }//namespaceTerminal
    }//namespaceftxui