<commit_before>package totoro.yui.actions

import totoro.yui.client.Command
import totoro.yui.client.IRCClient
import totoro.yui.util.F
import totoro.yui.util.api.Lua

class LuaAction : SensitivityAction("lua", "l", "lua=", "l=") {
    private val errorRegex = "^input:\\d*:\\s.*$".toRegex()

    override fun handle(client: IRCClient, command: Command): Boolean {
        val code = when {
            command.name?.endsWith('=') == true -> "=${command.content}"
            else -> command.content
        }
        val result = Lua.run(code)
        if (result == null)
            client.send(command.chan, "${F.Gray}something went wrong${F.Reset}")
        else
            client.send(command.chan, "${if (result.matches(errorRegex)) F.Red else F.Yellow}$result${F.Reset}")
        return true
    }<commit_msg>limit the maximum number of output lines of the `lua` command<commit_after>package totoro.yui.actions

import totoro.yui.client.Command
import totoro.yui.client.IRCClient
import totoro.yui.util.F
import totoro.yui.util.api.Lua

class LuaAction : SensitivityAction("lua", "l", "lua=", "l=") {
    private val maxOutputLines = 3
    private val errorRegex = "^input:\\d*:\\s.*$".toRegex()

    override fun handle(client: IRCClient, command: Command): Boolean {
        val code = when {
            command.name?.endsWith('=') == true -> "=${command.content}"
            else -> command.content
        }
        val result = Lua.run(code)
        if (result == null)
            client.send(command.chan, "${F.Gray}something went wrong${F.Reset}")
        else {
            val lines = result.split('\n')
            lines.take(maxOutputLines).forEach { line ->
                client.send(command.chan, "${if (line.matches(errorRegex)) F.Red else F.Yellow}$line${F.Reset}")
            }
            if (lines.size > maxOutputLines)
                client.send(command.chan, "${F.Yellow}(and ${lines.size - maxOutputLines} more...)${F.Reset}")
        }

        return true
    }
{'fraction_non_alphanumeric': 0.1545, 'fraction_numerical': 0.0005, 'mean_word_length': 2.5541740674955595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7051661', 'n_tokens_mistral': 622, 'n_tokens_neox': 610, 'n_words': 135}