examples/dom/graph.cpp

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<chrono>//foroperator""s,chrono_literals
    #include<cmath>//forsin
    #include<ftxui/dom/elements.hpp>//forgraph,operator|,separator,color,Element,vbox,flex,inverted,operator|=,Fit,hbox,size,border,GREATER_THAN,HEIGHT
    #include<ftxui/screen/screen.hpp>//forFull,Screen
    #include<functional>//forref,reference_wrapper
    #include<iostream>//forcout,ostream
    #include<memory>//forshared_ptr
    #include<string>//foroperator<<,string
    #include<thread>//forsleep_for
    #include<utility>//forignore
    #include<vector>//forvector

    #include"ftxui/dom/node.hpp"//forRender
    #include"ftxui/screen/color.hpp"//forColor,Color::BlueLight,Color::RedLight,Color::YellowLight,ftxui

    classGraph{
    public:
    std::vector<int>operator()(intwidth,intheight)const{
    std::vector<int>output(width);
    for(inti=0;i<width;++i){
    floatv=0;
    v+=0.1f*sin((i+shift)*0.1f);//NOLINT
    v+=0.2f*sin((i+shift+10)*0.15f);//NOLINT
    v+=0.1f*sin((i+shift)*0.03f);//NOLINT
    v*=height;//NOLINT
    v+=0.5f*height;//NOLINT
    output[i]=static_cast<int>(v);
    }
    returnoutput;
    }
    intshift=0;
    };

    std::vector<int>triangle(intwidth,intheight){
    std::vector<int>output(width);
    for(inti=0;i<width;++i){
    output[i]=i%(height-4)+2;
    }
    returnoutput;
    }

    intmain(){
    usingnamespaceftxui;
    usingnamespacestd::chrono_literals;

    Graphmy_graph;

    std::stringreset_position;
    for(inti=0;;++i){
    std::ignore=i;
    autodocument=hbox({
    vbox({
    graph(std::ref(my_graph)),
    separator(),
    graph(triangle)|inverted,
    })|flex,
    separator(),
    vbox({
    graph(std::ref(my_graph))|color(Color::BlueLight),
    separator(),
    graph(std::ref(my_graph))|color(Color::RedLight),
    separator(),
    graph(std::ref(my_graph))|color(Color::YellowLight),
    })|flex,
    });

    document|=border;

    constintmin_width=40;
    document|=size(HEIGHT,GREATER_THAN,min_width);

    autoscreen=Screen::Create(Dimension::Full(),Dimension::Fit(document));
    Render(screen,document);
    std::cout<<reset_position;
    screen.Print();
    reset_position=screen.ResetPosition();

    constautosleep_time=0.03s;
    std::this_thread::sleep_for(sleep_time);
    my_graph.shift++;
    }

    return0;
    }