helper.lua 2.04 KB
Newer Older
epsi sayidina's avatar
epsi sayidina committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
-- vim: ts=4 sw=4 noet ai cindent syntax=lua-- global

helper = {}

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---

-- Lua Function Demo 
-- https://github.com/brndnmtthws/conky/issues/62

function helper.exec(command)
    local file = assert(io.popen(command, 'r'))
    local s = file:read('*all')
    file:close()

    s = string.gsub(s, '^%s+', '') 
    s = string.gsub(s, '%s+$', '') 
    s = string.gsub(s, '[\n\r]+', ' ')

    return s
end


function helper.gototopleft()
  return helper.exec('tput cup 0 0') 
--  return ''
end

function helper.newline()
  return helper.exec('echo "\n\n\n\n"') 
end

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---

-- template variables
local ansiPresetDark = {
  icon      = ansiFgRed,
  label     = ansiFgBlack .. ansiBoldOn,
  value     = ansiFgBlue .. ansiBoldOff
}

local ansiPresetBright = {
  icon      = ansiFgRed,
  label     = ansiFgBlack .. ansiBoldOn,
  value     = ansiFgBlack .. ansiBoldOff
}

local ansiPreset = ansiPresetBright

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---

function helper.icon(text, ansi)
  ansi = ansi or ansiPreset.icon
 
  return ansi .. ' ' .. text .. ' '
end

function helper.label(text, ansi)
  ansi = ansi or ansiPreset.label

  return ansi .. text
end

function helper.value(text, ansi)
  ansi = ansi or ansiPreset.value

  return ansi .. text 
end

function helper.common(icon, label, value)
  text=''
  
  if icon  then text = text .. ' ' .. helper.icon(icon)   end  
  if label then text = text .. ' ' .. helper.label(label) end  
  if value then text = text .. ' ' .. helper.value(value) end

  return text
end

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---
-- miscellanous

function helper.progressbar(value)
  -- Predefined
  maximum=100
  width=25
  symbol="=" 

  -- number of block
  count = (value * width) // maximum
    
  -- Create the progress bar string.
  row=''
  for i=1,count do row = row .. symbol end
  for i=count,width do row = row .. ' ' end

  return ' [' .. row .. '] '
end