diff --git a/.luacheckrc b/.luacheckrc index 681a68b..b4cb29d 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -648,6 +648,7 @@ stds.minetest = { stds.book_runner = { globals = { "book_runner", + minetest = {fields = {chat_send_player}}, }, read_globals = { "default", diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3d311a8..94c1c38 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,6 +10,20 @@ repos: - repo: local hooks: + - id: detect_debug + name: detect debug + language: pygrep + entry: DEBUG + pass_filenames: true + exclude: .pre-commit-config.yaml + fail_fast: true + - id: stylua + name: stylua + language: system + entry: stylua + pass_filenames: true + types: [ file, lua ] + fail_fast: true - id: luacheck name: luacheck language: system @@ -17,9 +31,4 @@ repos: pass_filenames: true types: [ file, lua ] args: [ -q ] - - id: stylua - name: stylua - language: system - entry: stylua - pass_filenames: true - types: [ file, lua ] + fail_fast: true diff --git a/command.lua b/command.lua index f102ab3..1c08062 100644 --- a/command.lua +++ b/command.lua @@ -1,9 +1,13 @@ +local f = string.format + local strip_colors = minetest.strip_colors local strip_translation = futil.strip_translation local S = default.get_translator +local max_book_length = book_runner.settings.max_book_length + minetest.register_chatcommand("run_in_book", { params = " []", description = "run the command; put the output in the book's body", @@ -38,11 +42,21 @@ minetest.register_chatcommand("run_in_book", { end local orig_chat_send_player = minetest.chat_send_player + local length = 0 + local skipped_lines = 0 + local too_long = false local received_messages = {} - -- luacheck: push globals minetest.chat_send_player function minetest.chat_send_player(name2, message) if name == name2 then - table.insert(received_messages, strip_colors(strip_translation(message))) + if too_long then + skipped_lines = skipped_lines + 1 + elseif length + #message + 1 > max_book_length then + too_long = true + skipped_lines = skipped_lines + 1 + else + table.insert(received_messages, strip_colors(strip_translation(message))) + length = length + #message + 1 + end else orig_chat_send_player(name2, message) end @@ -54,7 +68,10 @@ minetest.register_chatcommand("run_in_book", { end minetest.chat_send_player = orig_chat_send_player - -- luacheck: pop + + if too_long then + table.insert(received_messages, f("WARNING: too much output, %i lines omitted", skipped_lines)) + end local text = table.concat(received_messages, "\n") local written_book = ItemStack("default:book_written") diff --git a/mod.conf b/mod.conf index df86ea7..8a8bb2b 100644 --- a/mod.conf +++ b/mod.conf @@ -5,6 +5,6 @@ website = https://content.minetest.net/packages/rheo/book_runner/ author = fluxionary license = AGPL-3.0-or-later media_license = CC-BY-SA-4.0 -version = 2022-11-27 +version = 2023-05-12 min_minetest_version = 5.6.1 depends = default, fmod, futil diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..7f0d961 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,2 @@ +# output will be truncated after this point, with a short message. +book_runner:max_book_length (maximum length of the book) int 100000 0 640000