When I mentioned that I wanted a timer back in May, I said that I wanted "I'd like something that fits with everything else". I'm not entirely sure how many purely hypothetical beers I drank before writing that thought, but looking back it sounds more like the mutterings of the local town loony than anything important. I assure you, however, that this strange little collection of words will make sense if you read the following two paragraphs. So, y'know, you can just skip them if you're up for a challenge.
So, if you've been reading these posts religiously then you'll know that I use Awesome to manage my desktop. This gives me lots of control and customization options, but also makes using "traditional" desktop apps cumbersome at times. In this case, a quick Google search will find you endless pages of linux timer apps, coming in exactly two flavors:
- Pretty (useless) dialog windows that take up half your screen with fancy Large Text
- Icons in your system tray
Pretty much all of those nice little UI bits are custom. So, I can make my own "Option 2" that fits with the visual style of my desktop and does exactly what I want.
If you're still following along with my little scheme to make a timer but you're not sure it's quick or feasible, then I have some good news: It's done. I secretly went and did it this morning. Normally, I write and code together, but I just wasn't in the mood for 6 AM blogging. What was in the mood for was Runescape (???), and I didn't want to miss any deadlines in the process. I suppose you could say I was motivated by laziness, but I'm not about to complain!
The Implementation
The result of my mad morning keyboard mashing was just under 100 lines of Lua code. Most of that is UI boilerplate, but there's about 40 lines of actual logic in there that might interest someone, so let's give that a look:52 local timer = gears.timer { 53 timeout = 1, 54 autostart = false, 55 callback = function() 56 seconds_left = seconds_left - 1 57 if seconds_left == 0 then 58 awful.spawn({"mplayer", "/home/df458/assets/se/Notify01.wav"}); 59 60 local message = string.format("Your timer of %02d:%02d has ended", math.floor(seconds_total/3600), math.floor(seconds_total%3600/60)) 61 if seconds_total < 3600 then 62 message = string.format("Your timer of %02d:%02d has ended", math.floor(seconds_total/60), seconds_total%60) 63 end 64 naughty.notify { 65 title = "Time's up!", 66 text = message, 67 timeout = 300 68 } 69 elseif seconds_left < 0 then 70 widget:set_visible(false) 71 timer:stop() 72 end 73 progress:set_value(1 - (seconds_left / seconds_total)) 74 if seconds_left < 3600 then 75 label:set_markup(string.format("%02d:%02d", math.floor(seconds_left/60), seconds_left%60)) 76 else 77 label:set_markup(string.format("%02d:%02d", math.floor(seconds_left/3600), math.floor(seconds_left%3600/60))) 78 end 79 end 80 } 81 82 function activate_timer(seconds) 83 seconds_left = seconds 84 seconds_total = seconds 85 widget:set_visible(true) 86 progress:set_value(0) 87 if seconds_left < 3600 then 88 label:set_markup(string.format("%02d:%02d", math.floor(seconds_left/60), seconds_left%60)) 89 else 90 label:set_markup(string.format("%02d:%02d", math.floor(seconds_left/3600), math.floor(seconds_left%3600/60))) 91 end 92 timer:start() 93 endAs you can see, this little code block could probably use some cleaning. With that said, it works pretty well and demonstrates just how easy it is to make a timer. All I really do is set two numbers to how many seconds I want to wait, then subtract one from the 1st number every second. The second number isn't even necessary at all, but I use it to add a progress indicator. Beyond that, the rest is just making a nice text representation that switches between hour:minute to minute:second as necessary. When the timer's running, my little info are looks like this:
It's simple, sure, but it works quite well! Next time, I'll be wrapping up this series with some final touches to task management.
No comments:
Post a Comment