Add caching
This commit is contained in:
		@@ -11,6 +11,8 @@ add_executable(${PROJECT_NAME}
 | 
				
			|||||||
        Tui.h
 | 
					        Tui.h
 | 
				
			||||||
        Command.h
 | 
					        Command.h
 | 
				
			||||||
        Command.cpp
 | 
					        Command.cpp
 | 
				
			||||||
 | 
					        Pager.cpp
 | 
				
			||||||
 | 
					        Pager.hpp
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
target_link_libraries(${PROJECT_NAME} PRIVATE LibXml2::LibXml2 cpr::cpr)
 | 
					target_link_libraries(${PROJECT_NAME} PRIVATE LibXml2::LibXml2 cpr::cpr)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,6 +32,7 @@ public:
 | 
				
			|||||||
    Page &operator-=(int);
 | 
					    Page &operator-=(int);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [[nodiscard]] std::string str_pretty() const;
 | 
					    [[nodiscard]] std::string str_pretty() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [[nodiscard]] std::string str() const;
 | 
					    [[nodiscard]] std::string str() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool refresh();
 | 
					    bool refresh();
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										50
									
								
								src/Pager.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/Pager.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					//  _____         _  _____
 | 
				
			||||||
 | 
					// |_   _|____  _| ||_   _|_   __
 | 
				
			||||||
 | 
					//   | |/ _ \ \/ / __|| | \ \ / /
 | 
				
			||||||
 | 
					//   | |  __/>  <| |_ | |  \ V /
 | 
				
			||||||
 | 
					//   |_|\___/_/\_\\__||_|   \_/
 | 
				
			||||||
 | 
					// Author: Love Billenius <lovebillenius@disroot.org>
 | 
				
			||||||
 | 
					// License: GPL-3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "Pager.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Page &Pager::seek(const int number) {
 | 
				
			||||||
 | 
					    Page &page = get_page_at(number);
 | 
				
			||||||
 | 
					    m_page_number = number;
 | 
				
			||||||
 | 
					    return page;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Pager &Pager::operator+=(const int number) {
 | 
				
			||||||
 | 
					    seek(m_page_number + number);
 | 
				
			||||||
 | 
					    return *this;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Pager &Pager::operator-=(const int number) {
 | 
				
			||||||
 | 
					    seek(m_page_number - number);
 | 
				
			||||||
 | 
					    return *this;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Page &Pager::operator*() {
 | 
				
			||||||
 | 
					    return get_page_at(m_page_number);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Page *Pager::operator->() {
 | 
				
			||||||
 | 
					    return &get_page_at(m_page_number);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Pager::set(const int number) {
 | 
				
			||||||
 | 
					    m_page_number = number;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Pager::clear() {
 | 
				
			||||||
 | 
					    m_pages.clear();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Page &Pager::get_page_at(const int number) {
 | 
				
			||||||
 | 
					    if (!m_pages.contains(number)) {
 | 
				
			||||||
 | 
					        const Page p(number);
 | 
				
			||||||
 | 
					        m_pages[number] = p;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return m_pages.at(number);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										41
									
								
								src/Pager.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/Pager.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					//  _____         _  _____
 | 
				
			||||||
 | 
					// |_   _|____  _| ||_   _|_   __
 | 
				
			||||||
 | 
					//   | |/ _ \ \/ / __|| | \ \ / /
 | 
				
			||||||
 | 
					//   | |  __/>  <| |_ | |  \ V /
 | 
				
			||||||
 | 
					//   |_|\___/_/\_\\__||_|   \_/
 | 
				
			||||||
 | 
					// Author: Love Billenius <lovebillenius@disroot.org>
 | 
				
			||||||
 | 
					// License: GPL-3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <unordered_map>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "Page.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Pager {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    Pager() = default;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Page &seek(int number);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Pager &operator+=(int number);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Pager &operator-=(int number);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Page &operator*();
 | 
				
			||||||
 | 
					    Page *operator->();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void set(int number);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Page &get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    Page &get_page_at(int number);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    std::unordered_map<int, Page> m_pages{};
 | 
				
			||||||
 | 
					    int m_page_number = 100;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
							
								
								
									
										22
									
								
								src/Tui.cpp
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/Tui.cpp
									
									
									
									
									
								
							@@ -14,11 +14,11 @@
 | 
				
			|||||||
#include <utility>
 | 
					#include <utility>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "Command.h"
 | 
					#include "Command.h"
 | 
				
			||||||
 | 
					#include "Pager.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Tui::Tui(Page page) : m_page(std::move(page)) {
 | 
					Tui::Tui(Pager pager) : m_pager(std::move(pager)) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
std::optional<int> get_page_number(const std::string_view line) {
 | 
					std::optional<int> get_page_number(const std::string_view line) {
 | 
				
			||||||
    const size_t space = line.find_first_of(' ');
 | 
					    const size_t space = line.find_first_of(' ');
 | 
				
			||||||
    if (space == std::string::npos)
 | 
					    if (space == std::string::npos)
 | 
				
			||||||
@@ -50,7 +50,7 @@ void printHelp() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Tui::run() {
 | 
					void Tui::run() {
 | 
				
			||||||
    std::cout << m_page.str_pretty() << std::endl;
 | 
					    std::cout << m_pager->str_pretty() << std::endl;
 | 
				
			||||||
    for (;;) {
 | 
					    for (;;) {
 | 
				
			||||||
        std::cout << "Enter a command: ";
 | 
					        std::cout << "Enter a command: ";
 | 
				
			||||||
        std::flush(std::cout);
 | 
					        std::flush(std::cout);
 | 
				
			||||||
@@ -71,23 +71,23 @@ void Tui::run() {
 | 
				
			|||||||
                std::cout << "Invalid command!" << std::endl;
 | 
					                std::cout << "Invalid command!" << std::endl;
 | 
				
			||||||
            } else if constexpr (std::is_same_v<T, Command::Next>) {
 | 
					            } else if constexpr (std::is_same_v<T, Command::Next>) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    m_page += 1;
 | 
					                    m_pager += 1;
 | 
				
			||||||
                } catch (const std::runtime_error &e) {
 | 
					                } catch (const std::runtime_error &e) {
 | 
				
			||||||
                    std::cout << e.what() << std::endl;
 | 
					                    std::cout << e.what() << std::endl;
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                std::cout << m_page.str_pretty() << std::endl;
 | 
					                std::cout << m_pager->str_pretty() << std::endl;
 | 
				
			||||||
            } else if constexpr (std::is_same_v<T, Command::Previous>) {
 | 
					            } else if constexpr (std::is_same_v<T, Command::Previous>) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    m_page -= 1;
 | 
					                    m_pager -= 1;
 | 
				
			||||||
                } catch (const std::runtime_error &e) {
 | 
					                } catch (const std::runtime_error &e) {
 | 
				
			||||||
                    std::cout << e.what() << std::endl;
 | 
					                    std::cout << e.what() << std::endl;
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                std::cout << m_page.str_pretty() << std::endl;
 | 
					                std::cout << m_pager->str_pretty() << std::endl;
 | 
				
			||||||
            } else if constexpr (std::is_same_v<T, Command::Refresh>) {
 | 
					            } else if constexpr (std::is_same_v<T, Command::Refresh>) {
 | 
				
			||||||
                m_page.refresh();
 | 
					                m_pager.clear();
 | 
				
			||||||
                std::cout << m_page.str_pretty() << std::endl;
 | 
					                std::cout << m_pager->str_pretty() << std::endl;
 | 
				
			||||||
            } else if constexpr (std::is_same_v<T, Command::Exit>) {
 | 
					            } else if constexpr (std::is_same_v<T, Command::Exit>) {
 | 
				
			||||||
                should_exit = true;
 | 
					                should_exit = true;
 | 
				
			||||||
            } else if constexpr (std::is_same_v<T, Command::Help>) {
 | 
					            } else if constexpr (std::is_same_v<T, Command::Help>) {
 | 
				
			||||||
@@ -96,12 +96,12 @@ void Tui::run() {
 | 
				
			|||||||
                const Command::Seek &seek = arg;
 | 
					                const Command::Seek &seek = arg;
 | 
				
			||||||
                const int number = seek.number;
 | 
					                const int number = seek.number;
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    m_page = Page(number);
 | 
					                    m_pager.seek(number);
 | 
				
			||||||
                } catch (const std::runtime_error &e) {
 | 
					                } catch (const std::runtime_error &e) {
 | 
				
			||||||
                    std::cout << e.what() << std::endl;
 | 
					                    std::cout << e.what() << std::endl;
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                std::cout << m_page.str_pretty() << std::endl;
 | 
					                std::cout << m_pager->str_pretty() << std::endl;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        std::visit(visitor, command);
 | 
					        std::visit(visitor, command);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,15 +7,15 @@
 | 
				
			|||||||
// License: GPL-3
 | 
					// License: GPL-3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
#include "Page.hpp"
 | 
					#include "Pager.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Tui {
 | 
					class Tui {
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    Tui(Page page);
 | 
					    Tui(Pager pager);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void run();
 | 
					    void run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    Page m_page;
 | 
					    Pager m_pager;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,10 +6,9 @@
 | 
				
			|||||||
// Author: Love Billenius <lovebillenius@disroot.org>
 | 
					// Author: Love Billenius <lovebillenius@disroot.org>
 | 
				
			||||||
// License: GPL-3
 | 
					// License: GPL-3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "Page.hpp"
 | 
					 | 
				
			||||||
#include "Tui.h"
 | 
					#include "Tui.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[]) {
 | 
					int main(int argc, char *argv[]) {
 | 
				
			||||||
    Tui tui = Page();
 | 
					    Tui tui(Pager{});
 | 
				
			||||||
    tui.run();
 | 
					    tui.run();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user