From 389fe57a9fcd4c9971bb9cebc8dfeb67cac6498a Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 5 Mar 2024 12:22:07 -0500 Subject: Flush input buffer before prompt. --- src/lpm.c | 11 +++++++++++ src/lpm.lua | 1 + 2 files changed, 12 insertions(+) diff --git a/src/lpm.c b/src/lpm.c index ba228a9..17054e4 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -8,6 +8,7 @@ #include #include #include + #include #define MAX_PATH PATH_MAX #endif @@ -155,6 +156,15 @@ static int lpm_hash(lua_State* L) { return 1; } +static int lpm_tcflush(lua_State* L) { + int stream = luaL_checkinteger(L, 1); + #ifndef _WIN32 + if (isatty(stream)) + tcflush(stream, TCIOFLUSH); + #endif + return 0; +} + static int lpm_symlink(lua_State* L) { #ifndef _WIN32 @@ -1279,6 +1289,7 @@ static const luaL_Reg system_lib[] = { { "mkdir", lpm_mkdir }, // Makes a directory. { "rmdir", lpm_rmdir }, // Removes a directory. { "hash", lpm_hash }, // Returns a hex sha256 hash. + { "tcflush", lpm_tcflush }, // Flushes an terminal stream. { "symlink", lpm_symlink }, // Creates a symlink. { "chmod", lpm_chmod }, // Chmod's a file. { "init", lpm_init }, // Initializes a git repository with the specified remote. diff --git a/src/lpm.lua b/src/lpm.lua index 5307a0a..c990407 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -524,6 +524,7 @@ local function log_progress_action(message) end end local function prompt(message) + system.tcflush(0) if not ASSUME_YES or not JSON then io.stderr:write(colorize(message .. " [Y/n]: ", "cyan")) if ASSUME_YES then io.stderr:write("Y\n") end -- cgit v1.2.3