From 5af58fc69d03e26e76aa6ab8ba86f7a4c21f449c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 16 May 2024 14:13:01 +0300 Subject: [PATCH] console history --- res/layouts/console.xml | 9 ++++++++- res/layouts/console.xml.lua | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/res/layouts/console.xml b/res/layouts/console.xml index 1764e93a..bbe869d0 100644 --- a/res/layouts/console.xml +++ b/res/layouts/console.xml @@ -11,5 +11,12 @@ gravity="bottom-left" > - + + diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index 41d75430..3e5b0f70 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -1,3 +1,6 @@ +history = session.get_entry("console_history") +history_pointer = #history + function setup_variables() local x,y,z = player.get_pos(hud.get_player()) console.set('pos.x', x) @@ -5,7 +8,29 @@ function setup_variables() console.set('pos.z', z) end +function on_history_up() + if history_pointer == 0 then + return + end + document.prompt.text = history[history_pointer] + history_pointer = history_pointer - 1 +end + +function on_history_down() + if history_pointer == #history-1 then + return + end + history_pointer = history_pointer + 1 + document.prompt.text = history[history_pointer + 1] +end + +function add_to_history(text) + table.insert(history, text) + history_pointer = #history +end + function submit(text) + add_to_history(text) setup_variables() local status, result = pcall(function() return console.execute(text) end)