<commit_before>-- Displays announcements in the DFHack console

VERSION = '0.1.2'

if world_loaded == nil then
    world_loaded = dfhack.isWorldLoaded()
    enabled = false
    next_annc_id = 0
    timeout_interval = 2
end

function usage()
    print [[
Usage:
    annc-monitor start: Begin monitoring
    annc-monitor stop: End monitoring
    annc-monitor interval NUMBER: Set poll interval (frames)
]]
end

function set_timeout()
    dfhack.timeout(timeout_interval, 'frames', check_announcements)
end

function log(s, color)
    dfhack.color(color)
    print(dfhack.df2utf(s))
    dfhack.color(COLOR_RESET)
end

function check_announcements()
    if world_loaded then
        local annc_total = #df.global.world.status.reports
        if annc_total > next_annc_id then
            for i = next_annc_id, annc_total - 1 do
                local annc = df.global.world.status.reports[i]
                local color = annc.color + (annc.bright and 8 or 0)
                log(annc.text, color)
            end
            next_annc_id = annc_total
        end
    end
    if enabled then set_timeout() end
end

args = {...}
if #args >= 1 then
    if args[1] == 'start' then
        enabled = true
        set_timeout()
    elseif args[1] == 'stop' then
        enabled = false
    elseif args[1] == 'interval' then
        local n = tonumber(args[2])
        if n == nil or n < 1 or n ~= math.floor(n) then
            qerror('"' .. args[2] .. '" is not an integer!')
        end
        timeout_interval = n
    else
        usage()
    end
else
    usage()
end

dfhack.onStateChange.annc_monitor = function(state)
    if state == SC_WORLD_LOADED then
        world_loaded = true
        next_annc_id = #df.global.world.status.reports
    elseif state == SC_WORLD_UNLOADED then
        world_loaded = false
    end
end<commit_msg>Rewrite annc-monitor to use EventManager<commit_after>-- Displays announcements in the DFHack console

VERSION = '0.2'

eventful = require 'plugins.eventful'

if enabled == nil then
    enabled = false
end

function usage()
    print [[
Usage:
    annc-monitor start: Begin monitoring
    annc-monitor stop: End monitoring
]]
end

function log(s, color)
    dfhack.color(color)
    print(dfhack.df2utf(s))
    dfhack.color(COLOR_RESET)
end

local reports = df.global.world.status.reports
function annc_handler(id)
    if enabled and dfhack.isWorldLoaded() then
        local annc = reports[id - reports[0].id]
        local color = annc.color + (annc.bright and 8 or 0)
        log(annc.text, color)
    end
end

args = {...}
if #args >= 1 then
    if args[1] == 'start' then
        enabled = true
    elseif args[1] == 'stop' then
        enabled = false
    else
        usage()
    end
else
    usage()
end

eventful.enableEvent(eventful.eventType.REPORT, 1)
eventful.onReport.annc_monitor = annc_handler
{'fraction_non_alphanumeric': 0.09689922480620156, 'fraction_numerical': 0.009161381254404511, 'mean_word_length': 2.517967781908302, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5168624', 'n_tokens_mistral': 954, 'n_tokens_neox': 924, 'n_words': 260}