I am using AwesomeWM v4.0-170-g6c24848-dirty, compiled against Lua 5.3.3; and I am starting to customise my widgets.
One of them is the clock, technically
wibox.widget.textclock()
format
ordinal
format
print("Hello " .. name .. ", the value of key " .. k .. " is " .. v .. "!")
mytextclock = wibox.widget.textclock(" %a %dth %B, %H:%M ", 60)
Background:
At first I laid out the problem with two options in mind:
1. Select the day from the whole output string: After being processed by the program, use some kind of Bash echo $2
(considering an output like dayoftheweek day month hh:mm
) equivalence in Lua to...
2. Treat the variable day
individually from the start: This would mean finding a way to get the variable without using the whole string, once we have it...
...process it later with an if-else
structure which would alter the output depending on its value.
For speed reasons, I used the second way. I found easier and cleaner to get the variable from the start instead of dedicating some lines of code to the extraction from the output.
So I started using %d
as my main variable to work, which is used in Lua to represent the day in a date. (source)
The main deal here was to convert the content of %d
into a string:
day = "%d" -- This is supposed to be an integer now.
daynumber = tostring(day) -- Converts it to a string.
lastdigit = tostring(day, -1)
print(lastdigit) -- Output: d.
BOOM! Failure. This is not working, I hope somebody can say why in comments. The output if I print the latest char (-1) is always d, and if I try with -2 I will get the whole day value.
My main theory bases on the fact typing:
a = "%d"
print(a)
in the Lua interpreter ($ lua
in your shell) just returns %d
, no integers at all; but it's just a supposition. What's more, as far as I've read %d
is used in a date context, not independently as the value of a variable.
A possible solution:
day = os.date("%d") -- First of all we grab the day from the system time.
-- As Lua displays the day with two digits, we are storing both of them in variables in order to process them separately later.
firstdigit = string.sub(day, 0, 1)
lastdigit = string.sub(day, -1)
-- We don't want Awesome to display '01st August' or '08th September'. We are going to suppress the '0'.
if firstdigit == "0" then
day = lastdigit
end
-- Now we want to display the day with its respective ordinal: 1st, 2nd, 3rd, 4th... we are going to process the last digit for this.
if lastdigit == "1" then
ordinal = "st"
elseif lastdigit == "2" then
ordinal = "nd"
elseif lastdigit == "3" then
ordinal = "rd"
else
ordinal = "th"
end
-- Finally, we display the final date.
mytextclock = wibox.widget.textclock(" %a " ..day..ordinal.. " %B %H:%M ", 60)
...so we get the following outputs: