From 56ffc310670f5b6586f92e4e13db4dfe56bde633 Mon Sep 17 00:00:00 2001 From: InDieTasten Date: Sun, 13 Oct 2024 18:04:56 +0200 Subject: [PATCH] Extract stty calls into separate module (#30) Closes #29 --- lib/stty.lua | 19 +++++++++++++++++++ lib/term.lua | 10 ++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 lib/stty.lua diff --git a/lib/stty.lua b/lib/stty.lua new file mode 100644 index 0000000..8ef8120 --- /dev/null +++ b/lib/stty.lua @@ -0,0 +1,19 @@ +Stty = {} + +function Stty.enableRawMode() + os.execute("/bin/stty raw") +end + +function Stty.disableRawMode() + os.execute("/bin/stty sane") +end + +function Stty.disableEcho() + os.execute("/bin/stty -echo") +end + +function Stty.enableEcho() + os.execute("/bin/stty echo") +end + +return Stty diff --git a/lib/term.lua b/lib/term.lua index dd9ef93..1c534a3 100644 --- a/lib/term.lua +++ b/lib/term.lua @@ -1,3 +1,5 @@ +local stty = require("lib/stty") + Term = {} Term.internalInputBuffer = "" Term.internalOutputBuffer = "" @@ -145,8 +147,8 @@ end --- This will disable echo and line buffering --- It will also switch to the alternate screen buffer function Term.enterRawMode() - os.execute("/bin/stty raw") - os.execute("/bin/stty -echo") + stty.enableRawMode() + stty.disableEcho() io.write("\27[?1049h") io.flush() end @@ -157,8 +159,8 @@ end function Term.leaveRawMode() io.write("\27[?1049l") io.flush() - os.execute("/bin/stty echo") - os.execute("/bin/stty sane") + stty.enableEcho() + stty.disableRawMode() end --- Requests the cursor position from the terminal.