create limit on the size of a book

This commit is contained in:
flux 2023-05-12 09:44:08 -07:00
parent e102f112d7
commit ec73d73ec5
No known key found for this signature in database
GPG key ID: 9333B27816848A15
5 changed files with 39 additions and 10 deletions

View file

@ -648,6 +648,7 @@ stds.minetest = {
stds.book_runner = { stds.book_runner = {
globals = { globals = {
"book_runner", "book_runner",
minetest = {fields = {chat_send_player}},
}, },
read_globals = { read_globals = {
"default", "default",

View file

@ -10,6 +10,20 @@ repos:
- repo: local - repo: local
hooks: 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 - id: luacheck
name: luacheck name: luacheck
language: system language: system
@ -17,9 +31,4 @@ repos:
pass_filenames: true pass_filenames: true
types: [ file, lua ] types: [ file, lua ]
args: [ -q ] args: [ -q ]
- id: stylua fail_fast: true
name: stylua
language: system
entry: stylua
pass_filenames: true
types: [ file, lua ]

View file

@ -1,9 +1,13 @@
local f = string.format
local strip_colors = minetest.strip_colors local strip_colors = minetest.strip_colors
local strip_translation = futil.strip_translation local strip_translation = futil.strip_translation
local S = default.get_translator local S = default.get_translator
local max_book_length = book_runner.settings.max_book_length
minetest.register_chatcommand("run_in_book", { minetest.register_chatcommand("run_in_book", {
params = "<command> [<args>]", params = "<command> [<args>]",
description = "run the command; put the output in the book's body", description = "run the command; put the output in the book's body",
@ -38,11 +42,21 @@ minetest.register_chatcommand("run_in_book", {
end end
local orig_chat_send_player = minetest.chat_send_player local orig_chat_send_player = minetest.chat_send_player
local length = 0
local skipped_lines = 0
local too_long = false
local received_messages = {} local received_messages = {}
-- luacheck: push globals minetest.chat_send_player
function minetest.chat_send_player(name2, message) function minetest.chat_send_player(name2, message)
if name == name2 then 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 else
orig_chat_send_player(name2, message) orig_chat_send_player(name2, message)
end end
@ -54,7 +68,10 @@ minetest.register_chatcommand("run_in_book", {
end end
minetest.chat_send_player = orig_chat_send_player 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 text = table.concat(received_messages, "\n")
local written_book = ItemStack("default:book_written") local written_book = ItemStack("default:book_written")

View file

@ -5,6 +5,6 @@ website = https://content.minetest.net/packages/rheo/book_runner/
author = fluxionary author = fluxionary
license = AGPL-3.0-or-later license = AGPL-3.0-or-later
media_license = CC-BY-SA-4.0 media_license = CC-BY-SA-4.0
version = 2022-11-27 version = 2023-05-12
min_minetest_version = 5.6.1 min_minetest_version = 5.6.1
depends = default, fmod, futil depends = default, fmod, futil

2
settingtypes.txt Normal file
View file

@ -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