Tuesday, March 31, 2009

Copy an existing MySQL table to a new table

From here:

To make a copy of the table recipes which is in a different database called production into a new table called recipes_new in the currently selected database, use these two commands:

CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;

The first command creates the new table recipes_new by duplicating the structure of the existing table. The second command copies the data from old to new.

Saturday, March 14, 2009

Read csl_status twitter feeds in ruby (via RSS)

The Cal Poly computer science lab admins started a twitter page with updates about downed servers, etc. The following ruby script grabs the most recent entries (via RSS), and formats the dates purty.


#!/usr/bin/ruby
require 'rss'
require 'open-uri'

$BOLD = "\e[1m"
$NORMAL = "\e[m"

num = ARGV.length > 0 ? ARGV[0].to_i : 1

# Read cls_status twitter RSS feed
source = "http://twitter.com/statuses/user_timeline/19618428.rss"
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)

(0..(num-1)).each do |i|
diff = Time.now - rss.items[i].date
days = diff.to_i / 86400
hrs = (diff % 86400).to_i / 3600
mins,secs = *((diff % 3600).divmod(60))

print "#{$BOLD}"
print "#{days} days " if (days > 0)
print "#{hrs} hrs " if (hrs > 0)
print "#{mins} mins " if (days == 0 and mins > 0)
print "#{mins} secs " if (mins == 0)
puts "ago"

#puts "#{$BOLD}#{rss.items[i].date}"
puts "#{$NORMAL}#{rss.items[i].title}"
#puts rss.items[i].description
#puts rss.items[i].link
end

Cron entry for upcheck on Spot

Note to self: cron entry for "upcheck", which runs every 10 minutes and outputs any errors to a file

0,10,20,30,40,50 * * * * ruby /home/iggames/bin/upcheck.rb >> /home/iggames/bin/upcheck.log

Tuesday, March 10, 2009

wmii-like window movement in FVWM

In the wmii window manager, you use Alt + the vim movement keys (h/j/k/l) to switch windows relative to your current window (ie: Alt+J to select the window below you).

Turns out you can do the same thing in FVWM:
Key K A M Direction North (CurrentDesk) FlipFocus
Key J A M Direction South (CurrentDesk) FlipFocus
Key H A M Direction West (CurrentDesk) FlipFocus
Key L A M Direction East (CurrentDesk) FlipFocus


I'm not exactly sure what FlipFocus does vs just Focus, but it works the way I'd expect. (Selects relative to current window, choosing only among windows on the current desktop)

Saturday, March 7, 2009

The Z-buffer equation

I found an explanation for how OpenGL calculates z values here. It's handy if you want to do your own custom rendering in shaders and match OpenGLs depths for other objects.

Here's a function I wrote to compute it, based on the given distance:

float getDepth(float dist) {
float zNear = 0.1, zFar = 1000; // from main.cpp
float a = zFar / (zFar - zNear);
float b = zFar * zNear / (zNear - zFar);
float z = dist;
return a + b / z;
}