-
Notifications
You must be signed in to change notification settings - Fork 0
UI: Simple ncurses window with ls-like output #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0c5bfaf
feab7cd
27167fc
b4a7b03
13fbf3e
821a8bd
d91556c
ce45cba
524826a
2cde7b1
ab403cb
3a243de
629d678
01fcb87
c93400d
3042533
20b23df
69b5ef7
e5f148c
8c23f55
8afbfc9
3094bdf
a8ffa77
e612460
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #ifndef __BUFFER_HPP | ||
| #define __BUFFER_HPP | ||
|
|
||
| #include <filesystem> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace fs = std::filesystem; | ||
|
|
||
| namespace rang | ||
| { | ||
| class buffer | ||
| { | ||
| public: | ||
| std::vector<std::string> contents; | ||
|
|
||
| buffer() = default; | ||
|
|
||
| virtual ~buffer() = default; | ||
|
|
||
| virtual void update() = 0; | ||
| }; | ||
|
|
||
| class dummy : buffer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class does not contain any logic, therefore it remains abstract, so we couldn't use it as "dummy" class. |
||
| { | ||
| }; | ||
|
|
||
| class directory_listing : public buffer | ||
| { | ||
| public: | ||
| fs::path directory; | ||
|
|
||
| directory_listing(fs::path _directory); | ||
|
|
||
| void update(); | ||
| }; | ||
| } // namespace rang | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #ifndef __EVLOOP_HPP | ||
| #define __EVLOOP_HPP | ||
|
|
||
| #include <boost/asio.hpp> | ||
| #include <functional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #undef timeout | ||
|
|
||
| namespace rang | ||
| { | ||
| class event_loop | ||
| { | ||
| private: | ||
| boost::asio::io_context io_context; | ||
| boost::asio::signal_set signals; | ||
| boost::asio::posix::stream_descriptor input; | ||
|
|
||
| public: | ||
| event_loop(); | ||
|
|
||
| template <class duration> | ||
| void add_regular_job(const std::function<void()> callback, | ||
| const duration &interval); // TODO implementation | ||
|
|
||
| template <class time_point> | ||
| void add_job(const std::function<void()> callback, time_point &time); // TODO implementation | ||
|
|
||
| void set_input_reaction(const std::function<void(int)> callback); | ||
|
|
||
| void run(); | ||
|
|
||
| void stop(); | ||
|
|
||
| private: | ||
| void handle_input(const std::function<void(int)> &callback); | ||
| }; | ||
| } // namespace rang | ||
| #endif |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #ifndef __RWINDOW_HPP | ||
| #define __RWINDOW_HPP | ||
|
|
||
| #include "buffer.hpp" | ||
| #include "console_io.hpp" | ||
|
|
||
| namespace rang | ||
| { | ||
| class window | ||
| { | ||
| private: | ||
| console_io::window win; | ||
| buffer &tied_buf; | ||
|
|
||
| public: | ||
| int offset_x = 0, offset_y = 0; | ||
| window(console_io::window &&_win, buffer &_tied_buf); | ||
|
|
||
| void refresh(); | ||
|
|
||
| void scroll_viewport(int shift); | ||
| }; | ||
| } // namespace rang | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| add_executable (rang rang.cpp) | ||
| add_subdirectory(console_io) | ||
|
|
||
| add_executable (rang evloop.cpp buffer.cpp rwindow.cpp rang.cpp) | ||
|
|
||
| find_package(Boost 1.60.0 REQUIRED COMPONENTS system thread) | ||
|
|
||
| target_compile_features(rang PUBLIC cxx_std_17) | ||
| target_include_directories(rang PUBLIC ${CURSES_INCLUDE_DIRS}) | ||
| target_link_libraries(rang PUBLIC ${CURSES_LIBRARIES}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we should make a CMake module which will export the Curses imported target and use it (like |
||
| target_link_libraries(rang PUBLIC ${CURSES_LIBRARIES} ${Boost_LIBRARIES} console_io) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also prefer the FindBoost's exported targets here: Boost::system Boost::thread. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #include "buffer.hpp" | ||
| #include <algorithm> | ||
|
|
||
| using namespace rang; | ||
|
|
||
| directory_listing::directory_listing(fs::path _directory) : directory(_directory) | ||
| { | ||
| } | ||
|
|
||
| void directory_listing::update() | ||
| { | ||
| std::vector<fs::directory_entry> filtered; | ||
| fs::directory_iterator it(directory); | ||
| std::copy_if(fs::begin(it), fs::end(it), std::back_insert_iterator(filtered), | ||
| [](fs::directory_entry entry) -> bool { | ||
| return entry.path().filename().string().front() != '.'; | ||
| }); | ||
| std::sort(filtered.begin(), filtered.end(), | ||
| [](fs::directory_entry entry1, fs::directory_entry entry2) -> bool { | ||
| if (entry1.is_directory() != entry2.is_directory()) { | ||
| return entry1.is_directory(); | ||
| } | ||
| return entry1.path().filename().string() < | ||
| entry2.path().filename().string(); | ||
| }); | ||
| contents.resize(filtered.size()); | ||
| transform(filtered.begin(), filtered.end(), contents.begin(), | ||
| [](fs::directory_entry entry) -> std::string { | ||
| return entry.path().filename().string(); | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| cmake_minimum_required(VERSION 3.17) | ||
| project(console_io) | ||
|
|
||
| add_library(console_io OBJECT src/window.cpp src/base.cpp) | ||
|
|
||
| target_include_directories(console_io PUBLIC include) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef __SCREEN_HPP | ||
| #define __SCREEN_HPP | ||
|
|
||
| #include "window.hpp" | ||
| #include <ncurses.h> | ||
|
|
||
| namespace console_io | ||
| { | ||
| class ncurses : public window | ||
| { | ||
| public: | ||
| ncurses(); | ||
|
|
||
| ~ncurses(); | ||
| }; | ||
| } // namespace console_io | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #ifndef __CONSOLE_IO_HPP | ||
| #define __CONSOLE_IO_HPP | ||
| #include "base.hpp" | ||
| #include "window.hpp" | ||
|
|
||
| #undef timeout | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to add comment why this |
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #ifndef __WINDOW_HPP | ||
| #define __WINDOW_HPP | ||
|
|
||
| #include <memory> | ||
| #include <ncurses.h> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace console_io | ||
| { | ||
| class window | ||
| { | ||
| protected: | ||
| WINDOW *win_ptr = nullptr; | ||
| int size_x = 0; | ||
| int size_y = 0; | ||
| int offset_x = 0; | ||
| int offset_y = 0; | ||
|
|
||
| public: | ||
| window *parent = nullptr; | ||
| std::vector<window> subwindows; | ||
|
|
||
| window() = default; | ||
|
|
||
| window(int _size_x, int _size_y, int _offset_x, int _offset_y, window *_parent = nullptr); | ||
|
|
||
| ~window(); | ||
|
|
||
| bool operator==(const window &other) const; | ||
|
|
||
| int get_size_x() const; | ||
| int get_size_y() const; | ||
| int get_offset_x() const; | ||
| int get_offset_y() const; | ||
|
|
||
| void refresh() const; | ||
|
|
||
| void move(int x, int y) const; | ||
|
|
||
| void output(std::string s) const; | ||
|
|
||
| void move_and_output(int x, int y, std::string s) const; | ||
|
|
||
| void outputln(int y, std::string s) const; | ||
|
|
||
| window subwindow(int _size_x, int _size_y, int offset_x, int offset_y); | ||
|
|
||
| int set_keypad(bool value) const; | ||
| }; | ||
| } // namespace console_io | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #include "base.hpp" | ||
| #include <algorithm> | ||
|
|
||
| using namespace console_io; | ||
|
|
||
| ncurses::ncurses() | ||
| { | ||
| win_ptr = initscr(); | ||
| if (win_ptr == nullptr) { | ||
| throw 1; | ||
| } | ||
| size_x = COLS; | ||
| size_y = LINES; | ||
| } | ||
|
|
||
| ncurses::~ncurses() | ||
| { | ||
| if (win_ptr == stdscr) { // guarantee Liskov substitution | ||
| endwin(); | ||
| } else { | ||
| if (parent != nullptr) { | ||
| parent->subwindows.erase( | ||
| std::find(parent->subwindows.begin(), parent->subwindows.end(), *this)); | ||
| } | ||
| delwin(win_ptr); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.