Learn and Discover Open Source with Daily Genuine Experience. From Coding, Design, Package Management Tweak to Desktop Customization.
 
   

Preface

Goal: Modularized Conky Configuration Using Lua Script.

A member in dotfiles group once ask me, how to change gregorian date in his conky, into hijri date. The answer is simply, using external script.

The issue is, I like the new conky Lua format. So here it is a step by step, conky with lua scripting.


The Original Code

He gave a file in telegram group showing something like this one:

#Author url = http://dobbie03.deviantart.com/art/My-First-Conky-Config-327206399
#Modified by Akza

alignment top_middle
background no
border_margin 5
border_width 5
default_color ffffff  #413536 # grey 5f5f5f 3F3F3F 183149 3B3B3B 26211F
double_buffer yes
draw_borders no
draw_outline no
draw_shades no
gap_x 0
gap_y 150
maximum_width 2000
minimum_size 0 0
no_buffers yes
override_utf8_locale yes
own_window yes
own_window_title conky
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
own_window_transparent yes
own_window_type conky
text_buffer_size 8000
total_run_times 0
update_interval 1
uppercase no
use_xft yes
xftalpha 1
xftfont Freesans:pixelsize=9

#				fonts
#	Blue Highway
#	Zegoe Light - U
#	Zekton
#	Calibri
#	Engebrechtre
#	Opeln2001
#	Pricedown

own_window_argb_value 0
own_window_argb_visual yes
own_window_colour 000000
TEXT
${font BankGothic Md BT:pixelsize=15}${alignc}${time [ %A, %I:%M:%S ]}${font}
${font BankGothic Md BT:pixelsize=15}${alignc}${time %d %B, %Y}${font}
${font BankGothic Md BT:pixelsize=45}${alignc}Be A Good Moslem or Die As Syuhada${font}

This is the old plain conky format.

Command Line

You can run this by issue this command:

$ conky -c conky/my.conkyrc 

Notice that he original code is longer than that above.

Source

From the link above, I can track that the original code is from Dobbie.


Convert to Lua Code

My fist attempt is change the config, for minimal output. Just to change if this works. I also change the font to suit my environment.

-- Author url = http://dobbie03.deviantart.com/art/My-First-Conky-Config-327206399
-- Modified by Akza
-- 11 June 2019: Modified by epsi

-- vim: ts=4 sw=4 noet ai cindent syntax=lua

--[[
Conky, a system monitor, based on torsmo
]]

conky.config = {
-- common
  alignment = 'middle_middle',
  background = false,
  double_buffer = true,
  total_run_times = 0,
  update_interval = 1,
  default_color = '#ffffff',

-- window
  own_window = true,
  own_window_title = 'conky',
  own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',
  own_window_transparent = true,
  own_window_type = 'normal',
  own_window_argb_value = 0,
  own_window_argb_visual = true,
  own_window_colour = '#000000',
  
-- font  
  use_xft = true
}

conky.text = [[
${font CabinSketch-Bold:pixelsize=15}${alignc}${time [ %A, %I:%M:%S ]}${font}
${font CabinSketch-Bold:pixelsize=15}${alignc}${time %d %B, %Y}${font}
${font CabinSketch-Bold:pixelsize=45}${alignc}Let's Get Married!${font}
]]

This is the new Lua conky format.

Command Line

You can run this by issue this command:

$ conky -c conky/conkyrc-01.lua

Preview

Conky: Lua Format

If you see any shadow, this is because of my compton setup.

How does it Works?

All you nee to know is these two variables:

conky.config
conky.text

Modularized

Configuration

I like short code, and put long configuration code somewhere else. Assuming this code will not be changed frequently.

configuration = {
-- common
  alignment = 'middle_middle',
  background = false,
  double_buffer = true,
  total_run_times = 0,
  update_interval = 1,

-- border
  border_inner_margin = 5,
  border_outer_margin = 5,
  border_width = 5,

-- color
  -- #413536 # grey 5f5f5f 3F3F3F 183149 3B3B3B 26211F
  default_color = '#ffffff',

-- draw options
  draw_borders = false,
  draw_outline = false,
  draw_shades = false,

-- positioning
  gap_x = 0,
  gap_y = 0,
  maximum_width = 2000,
  minimum_height = 0,
  minimum_width = 0,

  no_buffers = true,
  override_utf8_locale = true,

-- window
  own_window = true,
  own_window_title = 'conky',
  own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',
  own_window_transparent = true,

-- 'normal', 'dock', 'panel', 'desktop', 'override'
  own_window_type = 'normal',

  own_window_argb_value = 0,
  own_window_argb_visual = true,
  own_window_colour = '#000000',

-- text
  text_buffer_size = 8000,
  uppercase = false,
  
-- font  
  use_xft = true,
  xftalpha = 1,
  xftfont = 'Freesans:pixelsize=9'

--				fonts
--	Blue Highway
--	Zegoe Light - U
--	Zekton
--	Calibri
--	Engebrechtre
--	Opeln2001
--	Pricedown
}

And in main file, we can call as below:

home = os.getenv("HOME")
dofile(home .. '/conky/config.lua')
conky.config = configuration

Text

Why don’t I also put the long text code separately? Because conky do not detect any changes outside that main file. If I separate the text file, I have to change main code and save, just to see the effect in conky. Of course you can freely refactor whenever your code get more complex.

Consider keep the text in main script. But change each lines of the text into separate Lua variables.

home = os.getenv("HOME")
dofile(home .. '/conky/config.lua')
conky.config = configuration

datetime = ''
.. '${font CabinSketch-Bold:pixelsize=15}'
.. '${alignc}'
.. '${time [ %A, %I:%M:%S ]}'
.. '\n'
.. '${font CabinSketch-Bold:pixelsize=20}'
.. '${alignc}'
.. '${time %d %B, %Y}'
.. '\n'

message = ''
.. "${font CabinSketch-Bold:pixelsize=45}"
.. "${alignc}"
.. "Let's Get Married!"
.. "\n"

conky.text = ''
.. datetime
.. message 

Command Line

You can run this by issue this command:

$ conky -c conky/conkyrc-02.lua

Preview

The same as previous image.


Finishing

Path

How about relative path? We can do this by using code below:

local dirname  = debug.getinfo(1).source:match("@?(.*/)")
dofile(dirname .. '/config.lua')
conky.config = configuration

Hijri Date

As I said before, use external script.

datetime = ''
.. '${font CabinSketch-Bold:pixelsize=15}'
.. '${alignc}'
.. '${time [ %A, %I:%M:%S ]}'
.. '\n'
.. '${font CabinSketch-Bold:pixelsize=20}'
.. '${alignc}'
.. '${exec ~/conky/conkyhijri.sh}'
.. '\n'

Notice the exec ~/conky/conkyhijri.sh code.

Hijri Script

Since this is not my script, I won’t put it in my blog. But you can have a look at my dotfiles.

Monitoring Code

I also port the rest code, to monitor the status of my system. My intention is to have a more clear indentation for conditional script.

cpu_usage = 'CPU Usage: ${cpu}% - RAM Usage: ${mem}'

root = 'Root: ${fs_free /} / ${fs_size /}'

battery = 'Battery: ${battery_percent BAT0}% -'
.. ' Remaining Time: ${battery_time BAT0} '

user = 'User: ${exec users} - System Uptime: ${uptime_short}'

wlan = 'wlp3s0'

net_up = 'Net Up: '
.. '${if_existing /proc/net/route ' .. wlan .. '}'
..   '${upspeed ' .. wlan .. '}'
.. '${else}'
..   '${if_existing /proc/net/route eth0}'
..     '${upspeed ' .. wlan .. '}'
..   '${endif}'
.. '${endif}'

net_down = 'Net Down: '
.. '${if_existing /proc/net/route ' .. wlan .. '}'
..   '${downspeed ' .. wlan .. '}'
.. '${else}'
..   '${if_existing /proc/net/route eth0}'
..     '${downspeed ' .. wlan .. '}'
..   '${endif}'
.. '${endif}'

monitor = ''
.. '${font CabinSketch-Bold:pixelsize=12}'
.. '${alignc}[ ' .. cpu_usage .. ' ]\n'
.. '${alignc}[ ' .. root .. ' ]\n'
-- '${alignc}[ ' .. battery .. ' ]\n'
.. '${alignc}[ ' .. user .. ' ]\n'
.. '${alignc}[ ' .. net_up .. ' ' ..net_down .. ' ]\n'

Notice, how I manage the wlan name. You may change it to suit your system.

The Text

Now you need to add this monitor variable to the text.

conky.text = ''
.. datetime
.. message 
.. ''
.. monitor

Preview

Conky: Modularized Lua

Command Line

You can run this by issue this command:

$ conky -c conky/conkyrc-03.lua

Complete Script

And finally, our summary:

-- Author url = http://dobbie03.deviantart.com/art/My-First-Conky-Config-327206399
-- Modified by Akza
-- 11 June 2019: Modified by epsi

-- vim: ts=4 sw=4 noet ai cindent syntax=lua

--[[
Conky, a system monitor, based on torsmo
]]

local dirname  = debug.getinfo(1).source:match("@?(.*/)")
dofile(dirname .. '/config.lua')
conky.config = configuration

datetime = ''
.. '${font CabinSketch-Bold:pixelsize=15}'
.. '${alignc}'
.. '${time [ %A, %I:%M:%S ]}'
.. '\n'
.. '${font CabinSketch-Bold:pixelsize=20}'
.. '${alignc}'
.. '${exec ~/conky/conkyhijri.sh}'
.. '\n'

message = ''
.. "${font CabinSketch-Bold:pixelsize=45}"
.. "${alignc}"
.. "Let's Get Married!"
.. "\n"

cpu_usage = 'CPU Usage: ${cpu}% - RAM Usage: ${mem}'

root = 'Root: ${fs_free /} / ${fs_size /}'

battery = 'Battery: ${battery_percent BAT0}% -'
.. ' Remaining Time: ${battery_time BAT0} '

user = 'User: ${exec users} - System Uptime: ${uptime_short}'

wlan = 'wlp3s0'

net_up = 'Net Up: '
.. '${if_existing /proc/net/route ' .. wlan .. '}'
..   '${upspeed ' .. wlan .. '}'
.. '${else}'
..   '${if_existing /proc/net/route eth0}'
..     '${upspeed ' .. wlan .. '}'
..   '${endif}'
.. '${endif}'

net_down = 'Net Down: '
.. '${if_existing /proc/net/route ' .. wlan .. '}'
..   '${downspeed ' .. wlan .. '}'
.. '${else}'
..   '${if_existing /proc/net/route eth0}'
..     '${downspeed ' .. wlan .. '}'
..   '${endif}'
.. '${endif}'

monitor = ''
.. '${font CabinSketch-Bold:pixelsize=12}'
.. '${alignc}[ ' .. cpu_usage .. ' ]\n'
.. '${alignc}[ ' .. root .. ' ]\n'
-- '${alignc}[ ' .. battery .. ' ]\n'
.. '${alignc}[ ' .. user .. ' ]\n'
.. '${alignc}[ ' .. net_up .. ' ' ..net_down .. ' ]\n'

conky.text = ''
.. datetime
.. message 
.. ''
.. monitor

You can see that the whole code is looks completely different, compared with the original code.


After all this is just a config. You may modify this config to suit your needs.