Skip to content

Commit

Permalink
Handle terminal resize
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurum committed Nov 21, 2015
1 parent 47eae18 commit ef69566
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/file_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ bool FileStream::nextFile() {

void FileStream::readCb(ev::io &w, int revents) {
if (!file_reader.read(**current_file)) {
size_t block_lines =
(config.block_lines < 0) ? terminal.getHeight() : config.block_lines;
if (file_reader.linesRead() >= block_lines) {
io_watcher.stop();
on_read(file_reader);
Expand Down Expand Up @@ -122,6 +120,8 @@ void FileStream::read(std::function<void(const Text &text)> _on_read,
on_read = _on_read;
on_end = _on_end;
direction = _direction;
block_lines =
(config.block_lines < 0) ? terminal.getHeight() : config.block_lines;

(**current_file).newPage(direction);
file_reader.reset(direction);
Expand Down
1 change: 1 addition & 0 deletions src/file_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FileStream {
std::function<void()> on_end;
FileIO::Direction direction;
bool end_reached = false;
size_t block_lines;

void readCb(ev::io &w, int revents);
bool nextFile();
Expand Down
14 changes: 11 additions & 3 deletions src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ Terminal::Terminal(const Config &config)

color_pairs[std::make_pair(default_fg, default_bg)] = 0;
}
onKeyPress([this](int cmd) {
if (cmd == KEY_RESIZE) {
getmaxyx(stdscr, height, width);
}
});
}

Terminal::~Terminal() {
Expand Down Expand Up @@ -211,16 +216,19 @@ void Terminal::stop() const {
}

void Terminal::inputCb(ev::io &w, int revents) {
on_key_press(getch());
int key = getch();
for (auto cb : on_key_press) {
cb(key);
}
}

void Terminal::onKeyPress(std::function<void(int)> _on_key_press) const {
if (!_on_key_press) {
return;
}
if (!on_key_press) {
if (!on_key_press.size()) {
io_watcher.set<Terminal, &Terminal::inputCb>(const_cast<Terminal *>(this));
io_watcher.start(tty_fd, ev::READ);
}
on_key_press = _on_key_press;
on_key_press.push_back(_on_key_press);
}
2 changes: 1 addition & 1 deletion src/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Terminal {
int tty_fd;
int stdin_fd = STDIN_FILENO;
mutable ev::io io_watcher;
mutable std::function<void(int)> on_key_press;
mutable std::vector<std::function<void(int)>> on_key_press;

short getColor(short color) const;
short getColorPair(short fg, short bg) const;
Expand Down

0 comments on commit ef69566

Please sign in to comment.