lnav/test/drive_readline_curses.cc

155 lines
4.3 KiB
C++
Raw Normal View History

2013-05-03 06:02:03 +00:00
/**
* Copyright (c) 2007-2012, Timothy Stack
*
* All rights reserved.
2022-03-16 22:38:08 +00:00
*
2013-05-03 06:02:03 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2022-03-16 22:38:08 +00:00
*
2013-05-03 06:02:03 +00:00
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2022-03-16 22:38:08 +00:00
*
2013-05-03 06:02:03 +00:00
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2022-03-16 22:38:08 +00:00
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2013-05-03 06:02:03 +00:00
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2009-09-14 01:07:32 +00:00
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
2022-03-16 22:38:08 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "config.h"
2009-09-14 01:07:32 +00:00
2014-01-13 03:58:18 +00:00
#ifdef HAVE_SYS_TTYDEFAULTS_H
2022-03-16 22:38:08 +00:00
# include <sys/ttydefaults.h>
2014-01-13 03:58:18 +00:00
#endif
2012-04-19 02:30:53 +00:00
2009-09-14 01:07:32 +00:00
#include <algorithm>
2022-03-16 22:38:08 +00:00
#include "../src/lnav_util.hh"
#include "lnav_util.hh"
2009-09-14 01:07:32 +00:00
#include "readline_curses.hh"
using namespace std;
static readline_context::command_map_t COMMANDS;
static struct {
bool dd_active;
2022-03-16 22:38:08 +00:00
readline_curses* dd_rl_view;
2009-09-14 01:07:32 +00:00
volatile sig_atomic_t dd_looping;
} drive_data;
2022-03-16 22:38:08 +00:00
static void
rl_callback(readline_curses* rc)
2009-09-14 01:07:32 +00:00
{
string line = rc->get_value().get_string();
2009-09-14 01:07:32 +00:00
if (line == "quit")
2022-03-16 22:38:08 +00:00
drive_data.dd_looping = false;
2009-09-14 01:07:32 +00:00
fprintf(stderr, "callback\n");
drive_data.dd_active = false;
}
2022-03-16 22:38:08 +00:00
static void
rl_timeout(readline_curses* rc)
2009-09-14 01:07:32 +00:00
{
fprintf(stderr, "timeout\n");
}
2022-03-16 22:38:08 +00:00
int
main(int argc, char* argv[])
2009-09-14 01:07:32 +00:00
{
int c, fd, retval = EXIT_SUCCESS;
2009-09-14 01:07:32 +00:00
2022-03-16 22:38:08 +00:00
fd = open("/tmp/lnav.err", O_WRONLY | O_CREAT | O_APPEND, 0666);
2009-09-14 01:07:32 +00:00
dup2(fd, STDERR_FILENO);
close(fd);
fprintf(stderr, "startup\n");
2022-03-16 22:38:08 +00:00
2009-09-14 01:07:32 +00:00
while ((c = getopt(argc, argv, "h")) != -1) {
2022-03-16 22:38:08 +00:00
switch (c) {
case 'h':
break;
default:
break;
}
2009-09-14 01:07:32 +00:00
}
2022-03-16 22:38:08 +00:00
auto psuperv = std::make_shared<pollable_supervisor>();
readline_context context("test", &COMMANDS);
readline_curses rlc(psuperv);
2009-09-14 01:07:32 +00:00
rlc.add_context(1, context);
rlc.start();
drive_data.dd_rl_view = &rlc;
screen_curses sc;
2022-03-16 22:38:08 +00:00
2009-09-14 01:07:32 +00:00
keypad(stdscr, TRUE);
nonl();
cbreak();
noecho();
nodelay(sc.get_window(), 1);
2022-03-16 22:38:08 +00:00
2009-09-14 01:07:32 +00:00
rlc.set_window(sc.get_window());
rlc.set_y(-1);
rlc.set_perform_action(rl_callback);
rlc.set_timeout_action(rl_timeout);
2009-09-14 01:07:32 +00:00
drive_data.dd_looping = true;
while (drive_data.dd_looping) {
vector<struct pollfd> pollfds;
2022-03-16 22:38:08 +00:00
int rc;
2009-09-14 01:07:32 +00:00
2022-03-16 22:38:08 +00:00
pollfds.push_back((struct pollfd){STDIN_FILENO, POLLIN, 0});
psuperv->update_poll_set(pollfds);
2022-03-16 22:38:08 +00:00
rlc.do_update();
refresh();
rc = poll(&pollfds[0], pollfds.size(), -1);
if (rc > 0) {
if (pollfd_ready(pollfds, STDIN_FILENO)) {
int ch;
while ((ch = getch()) != ERR) {
switch (ch) {
case CEOF:
case KEY_RESIZE:
break;
default:
if (drive_data.dd_active) {
rlc.handle_key(ch);
} else if (ch == ':') {
rlc.focus(1, ":");
drive_data.dd_active = true;
}
break;
}
}
}
psuperv->check_poll_set(pollfds);
2022-03-16 22:38:08 +00:00
}
2009-09-14 01:07:32 +00:00
}
2022-03-16 22:38:08 +00:00
2009-09-14 01:07:32 +00:00
return retval;
}