examples/dom/package_manager.cpp

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<chrono>//foroperator""s,chrono_literals
    #include<ftxui/dom/elements.hpp>//foroperator|,text,Element,hbox,bold,color,filler,separator,vbox,window,gauge,Fit,size,dim,EQUAL,WIDTH
    #include<ftxui/screen/screen.hpp>//forFull,Screen
    #include<iostream>//forcout,endl,ostream
    #include<list>//forlist,operator==,_List_iterator,_List_iterator<>::_Self
    #include<memory>//forallocator,shared_ptr,allocator_traits<>::value_type
    #include<string>//forstring,operator<<,to_string
    #include<thread>//forsleep_for
    #include<utility>//formove
    #include<vector>//forvector

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

    intmain(){
    usingnamespaceftxui;

    structTask{
    std::stringname;
    intnumber_of_threads;
    intdownloaded;
    intsize;
    };

    std::list<Task>remaining_tasks={
    {"contactserver",10,0,6*25},
    {"downloadindex.html",10,0,9*25},
    {"downloadscript.js",1,0,3*25},
    {"downloadstyle.js",1,0,4*25},
    {"downloadimage.png",1,0,5*25},
    {"downloadbig_1.png",1,0,30*25},
    {"downloadicon_1.png",1,0,7*25},
    {"downloadicon_2.png",1,0,8*25},
    {"downloadbig_2.png",1,0,30*25},
    {"downloadsmall_1.png",1,0,10*25},
    {"downloadsmall_2.png",1,0,11*25},
    {"downloadsmall_3.png",1,0,12*25},
    };

    std::list<Task>displayed_task;

    intremaining_threads=12;

    intnb_queued=(int)remaining_tasks.size();
    intnb_active=0;
    intnb_done=0;

    autoto_text=[](intnumber){
    returntext(std::to_string(number))|size(WIDTH,EQUAL,3);
    };

    autorenderTask=[&](constTask&task){
    autostyle=(task.downloaded==task.size)?dim:bold;
    returnhbox({
    text(task.name)|style,
    separator(),
    to_text(task.downloaded),
    text("/"),
    to_text(task.size),
    separator(),
    gauge(task.downloaded/float(task.size)),
    });
    };

    autorenderSummary=[&](){
    autosummary=vbox({
    hbox({
    text("-done:"),
    to_text(nb_done)|bold,
    })|color(Color::Green),
    hbox({
    text("-active:"),
    to_text(nb_active)|bold,
    })|color(Color::RedLight),
    hbox({
    text("-queue:"),
    to_text(nb_queued)|bold,
    })|color(Color::Red),
    });

    returnwindow(text("Summary"),summary);
    };

    autorender=[&](){
    std::vector<Element>entries;
    for(auto&task:displayed_task)
    entries.push_back(renderTask(task));

    returnvbox({
    //Listoftasks.
    window(text("Task"),vbox(std::move(entries))),

    //Summary.
    hbox({
    renderSummary(),
    filler(),
    }),
    });
    };

    autoupdateModel=[&](){
    for(auto&task:displayed_task){
    if(task.downloaded!=task.size){
    task.downloaded++;
    }elseif(task.number_of_threads){
    remaining_threads+=task.number_of_threads;
    task.number_of_threads=0;
    nb_active--;
    nb_done++;
    }
    }

    if(remaining_tasks.size()&&
    remaining_tasks.front().number_of_threads<=remaining_threads){
    remaining_threads-=remaining_tasks.front().number_of_threads;
    displayed_task.push_back(remaining_tasks.front());
    remaining_tasks.pop_front();
    nb_queued--;
    nb_active++;
    }
    };

    std::stringreset_position;
    for(;;){
    //Draw.
    autodocument=render();
    autoscreen=Screen::Create(Dimension::Full(),Dimension::Fit(document));
    Render(screen,document);
    std::cout<<reset_position;
    screen.Print();
    reset_position=screen.ResetPosition();

    //Simulatetime.
    usingnamespacestd::chrono_literals;
    std::this_thread::sleep_for(0.01s);

    //Exit
    if(nb_active+nb_queued==0)
    break;

    //Updatethemodelforthenextframe.
    updateModel();
    }
    std::cout<<std::endl;
    }