Skip to content

Instantly share code, notes, and snippets.

@zachwhaley
Last active July 31, 2018 21:33
Show Gist options
  • Save zachwhaley/75a3898ffa5ace9bd4219407c9fcaab1 to your computer and use it in GitHub Desktop.
Save zachwhaley/75a3898ffa5ace9bd4219407c9fcaab1 to your computer and use it in GitHub Desktop.
Dumb pager program
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
int main(int argc, char* argv[])
{
int ret = 0;
struct termios t, save;
tcgetattr(STDIN_FILENO, &t);
save = t;
t.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
const int pagesize = w.ws_row;
std::ifstream ifs(argv[1]);
if (!ifs) {
std::perror("Error");
ret = errno;
goto exit;
}
while (!ifs.eof()) {
std::string line;
for (int i = 1; i < pagesize and std::getline(ifs, line); i++) {
std::cout << line << std::endl;
}
std::cin.get();
}
exit:
tcsetattr(STDIN_FILENO, TCSAFLUSH, &save);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment