Skip to content

Instantly share code, notes, and snippets.

@streetturtle
Created October 14, 2020 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streetturtle/b0d2ea97eb48567b581fffecc0c4b41c to your computer and use it in GitHub Desktop.
Save streetturtle/b0d2ea97eb48567b581fffecc0c4b41c to your computer and use it in GitHub Desktop.
Lua helper functions
local function ellipsize(text, length)
return (text:len() > length and length > 0)
and text:sub(0, length - 3) .. '...'
or text
end
--- Converts seconds to "time ago" represenation, like '1 hour ago'
local function to_time_ago(seconds)
local days = seconds / 86400
if days > 1 then
days = math.floor(days + 0.5)
return days .. (days == 1 and ' day' or ' days') .. ' ago'
end
local hours = (seconds % 86400) / 3600
if hours > 1 then
hours = math.floor(hours + 0.5)
return hours .. (hours == 1 and ' hour' or ' hours') .. ' ago'
end
local minutes = ((seconds % 86400) % 3600) / 60
if minutes > 1 then
minutes = math.floor(minutes + 0.5)
return minutes .. (minutes == 1 and ' minute' or ' minutes') .. ' ago'
end
end
--- Convert degrees Celsius to Fahrenheit
local function celsius_to_fahrenheit(c) return c * 9 / 5 + 32 end
-- Convert degrees Fahrenheit to Celsius
local function fahrenheit_to_celsius(f) return (f - 32) * 5 / 9 end
local function hex2rgb(hex)
if hex == '#ebedf0' then hex = empty_color end
hex = tostring(hex):gsub("#","")
return tonumber("0x" .. hex:sub(1, 2)),
tonumber("0x" .. hex:sub(3, 4)),
tonumber("0x" .. hex:sub(5, 6))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment