<commit_before>#!/usr/bin/lua

function string:split( inSplitPattern, outResults )
   if not outResults then
      outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
      table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
      theStart = theSplitEnd + 1
      theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

local data_path = string.sub(arg[0], 0, -5) .. "_data"

local dateformat = "(%a+) (%d+) (%d+):(%d+)" 
local months = { Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12 }

local now = os.time()
local now_string = os.date("%Y-%m-%d", now)

local apps = {}

local count = 0

-- iterate over calendars (arguments)
for i,v in ipairs(arg) do
    -- read calendar data
    local handle = 0
    if (count > 4) then
        local oldest_key = 0
        for t,d in pairs(apps) do
            if (oldest_key < t) then
                oldest_key = t
            end
        end

        local max_date = os.date("%Y-%m-%d", oldest_key)
        handle = io.popen("google calendar list --cal " .. v .. " --date " .. now_string .. "," .. max_date .. " --delimiter ';' | grep ';'")
        --print("For calendar " .. v .. " max date " .. max_date)
    else
        handle = io.popen("google calendar list --cal " .. v .. " --delimiter ';' | grep ';'")
        --print("For calendar " .. v .. " no max date")
    end

    local data = handle:read("*a")
    handle:close()
    local split = data:split("\n")

    -- iterate over appointments in calendar
    for j = 1, #split-1 do
        -- parse time of appointment
        local line = split[j]:split(";")
        local month,day,hour,minute = line[2]:match(dateformat)
        month = months[month]
        local time = os.time({ year = os.date("%Y"), month = month, day = day, hour = hour, minute = minute })

        -- appointment is only interesting if it's not in the past
        if (time >= now) then
            -- if we have less than 3 appointments, add it
            if (count < 5) then
                -- if another appointment at the same time exists, increment time by 1
                local time_present = false
                for t,d in pairs(apps) do
                    if (t == time) then
                        time_present = true
                        break
                    end
                end
                if (time_present) then
                    time = time + 1
                end
                
                apps[time] = { v, line[1], line[2] }
                count = count + 1
            else
                -- else, check whether there is a later appointment to kick out
                local oldest_key = 0
    
                for t,d in pairs(apps) do
                    if (oldest_key < t) then
                        oldest_key = t
                    end
                end
                
                if (time < oldest_key) then
                    -- later appointment found, replace it
                    apps[oldest_key] = nil
                   
                    -- if another appointment at the same time exists, increment time by 1
                    local time_present = false
                    for t,d in pairs(apps) do
                        if (t == time) then
                            time_present = true
                            break
                        end
                    end
                    if (time_present) then
                        time = time + 1
                    end
                    apps[time] = { v, line[1], line[2] }
                else
                    -- we will only get older events, break loop
                    break
                end
            end
        end
    end
end

-- find first and last appointment
local lowest = nil
local highest = 0
for k,v in pairs(apps) do
    -- correct hour component for UK time zone
    local split = apps[k][3]:split(":")
    local hour1 = split[1]:split(" ")[3]
    local hour2 = split[2]:split(" ")[5]
   
    local newhr1 = tonumber(hour1) - 1
    if newhr1 < 10 then
        newhr1 = "0" .. newhr1
    end
    local newhr2 = tonumber(hour2) - 1
    if newhr2 < 10 then
        newhr2 = "0" .. newhr2
    end

    local joined = split[1]:sub(1, #split[1] - #hour1) .. newhr1 .. ":" .. split[2]:sub(1, #split[2] - #hour2) .. newhr2 .. ":" .. split[3]

    apps[k][3] = joined


    if (highest < k) then
        highest = k
    end

    --print(apps[k][1] .. apps[k][2] .. apps[k][3])
    
    if #apps[k][2] > 24 then
        apps[k][2] = apps[k][2]:sub(1, 20) .. "..."
    end

    if (lowest == nil or lowest > k) then
        lowest = k
    end
end

-- if no new data was found (offline?), print last data
if (lowest == nil) then
    local oldhandle = io.open(data_path, "r")
    if (oldhandle ~= nil) then
        local d = oldhandle:read("*a")
        --print("old data")<commit_msg>updated calendar output to handle all-day events better<commit_after>#!/usr/bin/lua

function string:split( inSplitPattern, outResults )
   if not outResults then
      outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
      table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
      theStart = theSplitEnd + 1
      theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

local data_path = string.sub(arg[0], 0, -5) .. "_data"

local dateformat = "(%a+) (%d+) (%d+):(%d+)" 
local months = { Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12 }

local now = os.time()
local now_string = os.date("%Y-%m-%d", now)

local apps = {}

local count = 0

-- iterate over calendars (arguments)
for i,v in ipairs(arg) do
    -- read calendar data
    local handle = 0
    if (count > 4) then
        local oldest_key = 0
        for t,d in pairs(apps) do
            if (oldest_key < t) then
                oldest_key = t
            end
        end

        local max_date = os.date("%Y-%m-%d", oldest_key)
        handle = io.popen("google calendar list --cal " .. v .. " --date " .. now_string .. "," .. max_date .. " --delimiter ';' | grep ';'")
        --print("For calendar " .. v .. " max date " .. max_date)
    else
        handle = io.popen("google calendar list --cal " .. v .. " --delimiter ';' | grep ';'")
        --print("For calendar " .. v .. " no max date")
    end

    local data = handle:read("*a")
    handle:close()
    local split = data:split("\n")

    -- iterate over appointments in calendar
    for j = 1, #split-1 do
        -- parse time of appointment
        local line = split[j]:split(";")
        local month,day,hour,minute = line[2]:match(dateformat)
        month = months[month]
        local time = os.time({ year = os.date("%Y"), month = month, day = day, hour = hour, minute = minute })

        -- appointment is only interesting if it's not in the past
        if (time >= now) then
            -- if we have less than 3 appointments, add it
            if (count < 5) then
                -- if another appointment at the same time exists, increment time by 1
                local time_present = false
                for t,d in pairs(apps) do
                    if (t == time) then
                        time_present = true
                        break
                    end
                end
                if (time_present) then
                    time = time + 1
                end
                
                apps[time] = { v, line[1], line[2] }
                count = count + 1
            else
                -- else, check whether there is a later appointment to kick out
                local oldest_key = 0
    
                for t,d in pairs(apps) do
                    if (oldest_key < t) then
                        oldest_key = t
                    end
                end
                
                if (time < oldest_key) then
                    -- later appointment found, replace it
                    apps[oldest_key] = nil
                   
                    -- if another appointment at the same time exists, increment time by 1
                    local time_present = false
                    for t,d in pairs(apps) do
                        if (t == time) then
                            time_present = true
                            break
                        end
                    end
                    if (time_present) then
                        time = time + 1
                    end
                    apps[time] = { v, line[1], line[2] }
                else
                    -- we will only get older events, break loop
                    break
                end
            end
        end
    end
end

-- find first and last appointment
local lowest = nil
local highest = 0
for k,v in pairs(apps) do
    -- correct hour component for UK time zone
    local split = apps[k][3]:split(":")
    local hour1 = split[1]:split(" ")[3]
    local hour2 = split[2]:split(" ")[5]

    if hour1 ~= "00" and hour2 ~= "00" then
        local newhr1 = tonumber(hour1) - 1
        if newhr1 < 10 then
            newhr1 = "0" .. newhr1
        end
        local newhr2 = tonumber(hour2) - 1
        if newhr2 < 10 then
            newhr2 = "0" .. newhr2
        end

        local joined = split[1]:sub(1, #split[1] - #hour1) .. newhr1 .. ":" .. split[2]:sub(1, #split[2] - #hour2) .. newhr2 .. ":" .. split[3]

        apps[k][3] = joined
    else
        -- correct end date of all-day event (reduce by 1)
        local newday2 = tonumber(split[2]:sub(#split[2] - 4, #split[2] - 3)) - 1

        apps[k][3] = split[1]:sub(1, #split[1] - #hour1) .. "00:00 - " .. split[2]:sub(6, #split[2] - #hour2 - 3) .. newday2  .. " 23:59"
    end

    if (highest < k) then
        highest = k
    end

    --print(apps[k][1] .. apps[k][2] .. apps[k][3])
    
    if #apps[k][2] > 24 then
        apps[k][2] = apps[k][2]:sub(1, 20) .. "..."
    end

    if (lowest == nil or lowest > k) then
        lowest = k
    end
end

-- if no new data was found (offline?), print last data
if (lowest == nil) then
    local oldhandle = io.open(data_path, "r")
    if (oldhandle ~= nil) then
        local d = oldhandle:read("*a")
        --print("old data")
{'fraction_non_alphanumeric': 0.1205733558178752, 'fraction_numerical': 0.01976765973393292, 'mean_word_length': 1.4545872614394113, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 11, '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': '1519558', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3105, 'n_words': 1046}