reasonably ok multithreading
This commit is contained in:
parent
cfafe0498e
commit
cb89a9927b
@ -1,6 +1,7 @@
|
||||
#include <locale.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include "cli.hpp"
|
||||
#include "bash_history.yy.hpp"
|
||||
#include "storage.hpp"
|
||||
@ -67,17 +68,10 @@ void export_result(const char * const result) {
|
||||
}
|
||||
|
||||
void * async(void * arg) {
|
||||
entry_t entry;
|
||||
|
||||
while (true) {
|
||||
while (do_run) {
|
||||
tui_take_input();
|
||||
if (is_input_changed) {
|
||||
query(rl_line_buffer, entry_lines, selection_offset);
|
||||
is_input_changed = false;
|
||||
} else {
|
||||
requery();
|
||||
}
|
||||
while (entry = get_entry(), entry.command != NULL) {
|
||||
tui_append_back(entry);
|
||||
cancel_all_queries();
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,7 +79,6 @@ void * async(void * arg) {
|
||||
}
|
||||
|
||||
signed main(const int argc, const char * const * const argv) {
|
||||
extern void testtest(void);
|
||||
// NOTE: never returns on error
|
||||
parse_arguments(argc, argv);
|
||||
|
||||
@ -96,12 +89,23 @@ signed main(const int argc, const char * const * const argv) {
|
||||
pthread_t query_thread;
|
||||
pthread_create(&query_thread, NULL, async, NULL);
|
||||
while (do_run) {
|
||||
tui_take_input();
|
||||
entry_t entry;
|
||||
if (do_redisplay) {
|
||||
do_redisplay = false;
|
||||
if (is_input_changed) {
|
||||
testtest();
|
||||
is_input_changed = false;
|
||||
query(rl_line_buffer, entry_lines, selection_offset);
|
||||
} else {
|
||||
requery();
|
||||
}
|
||||
while (entry = get_entry(), entry.command != NULL) {
|
||||
tui_append_back(entry);
|
||||
}
|
||||
tui_refresh();
|
||||
}
|
||||
usleep(1000);
|
||||
}
|
||||
pthread_join(query_thread, NULL);
|
||||
|
||||
query(rl_line_buffer, 1, selection_offset + selection_relative);
|
||||
export_result(get_entry().command);
|
||||
|
@ -137,7 +137,7 @@ void requery(void) {
|
||||
sqlite3_reset(*stmt);
|
||||
}
|
||||
|
||||
void testtest(void) {
|
||||
void cancel_all_queries(void) {
|
||||
sqlite3_interrupt(db);
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ extern int deinit_storage(void);
|
||||
extern int insert_entry(const entry_t entry);
|
||||
extern void query(const char * const string, const size_t limit, const size_t offset);
|
||||
extern void requery(void);
|
||||
extern void cancel_all_queries(void);
|
||||
extern entry_t get_entry(void);
|
||||
|
||||
#endif
|
||||
|
@ -5,8 +5,10 @@
|
||||
#include <readline/readline.h>
|
||||
|
||||
extern bool do_execute;
|
||||
|
||||
size_t entry_lines;
|
||||
bool is_input_changed = true;
|
||||
bool do_redisplay = true;
|
||||
|
||||
static WINDOW * main_window;
|
||||
static WINDOW * entry_window;
|
||||
@ -30,7 +32,7 @@ int init_tui(void) {
|
||||
// Ncurses
|
||||
initscr();
|
||||
nonl();
|
||||
halfdelay(1);
|
||||
cbreak(); //halfdelay(1);
|
||||
noecho();
|
||||
curs_set(0);
|
||||
keypad(stdscr, TRUE);
|
||||
@ -136,6 +138,7 @@ void tui_take_input(void) {
|
||||
case CTRL('k'): {
|
||||
if (selection_relative != entry_lines-1) {
|
||||
++selection_relative;
|
||||
do_redisplay = true;
|
||||
} else {
|
||||
++selection_offset;
|
||||
is_input_changed = true;
|
||||
@ -146,6 +149,7 @@ void tui_take_input(void) {
|
||||
case CTRL('j'): {
|
||||
if (selection_relative != 0) {
|
||||
--selection_relative;
|
||||
do_redisplay = true;
|
||||
} else {
|
||||
if (selection_offset != 0) {
|
||||
--selection_offset;
|
||||
@ -162,13 +166,15 @@ void tui_take_input(void) {
|
||||
case CTRL('d'): {
|
||||
if (selection_offset == 0) {
|
||||
selection_relative = 0;
|
||||
do_redisplay = true;
|
||||
} else
|
||||
if (selection_offset > paging_size) {
|
||||
selection_offset -= paging_size;
|
||||
is_input_changed = true;
|
||||
} else {
|
||||
selection_offset = 0;
|
||||
}
|
||||
is_input_changed = true;
|
||||
}
|
||||
} break;
|
||||
case CTRL('q'): {
|
||||
do_execute = false;
|
||||
@ -184,4 +190,8 @@ void tui_take_input(void) {
|
||||
is_input_changed = true;
|
||||
} break;
|
||||
}
|
||||
|
||||
if (is_input_changed) {
|
||||
do_redisplay = true;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,9 @@ extern "C" char * rl_line_buffer;
|
||||
extern size_t entry_lines;
|
||||
extern size_t selection_offset;
|
||||
extern size_t selection_relative;
|
||||
|
||||
extern bool is_input_changed;
|
||||
extern bool do_redisplay;
|
||||
|
||||
extern int init_tui(void);
|
||||
extern int deinit_tui(void);
|
||||
|
Loading…
x
Reference in New Issue
Block a user