Posts for 2009 July
mocp rand
Post by Nico Brailovsky @ 2009-07-23 | Permalink | Leave a comment
I'm quite sure everyone reading this must have a respectable, if not massive, music collection. In this days and age is difficult finding someone who doesn't. It's also difficult to choose one, and only one, disk to listen at any given moment. Until we're upgraded to support concurrent music listening we're better of with a random disk selector, which is exactly what this little script does:
#!/bin/bash
SEARCH_DIR="/home/nico/Música"
START_RANDOM=1
RAND_MAX=32767
while (( 1 )); do
NUM_DISCS=$(find $SEARCH_DIR -type d | wc -l)
RAND=$(($NUM_DISCS * $RANDOM / $RAND_MAX))
RAND_DISC=$(find $SEARCH_DIR -type d | head -n $RAND | tail -n 1)
# Wake up moc
mocp -FS 2>/dev/null >/dev/null &
mocp -pca "$RAND_DISC" &
echo "Playing $RAND_DISC"
# Start from a random file?
if (( $START_RANDOM )); then
mocp --on shuffle &
mocp -f &
mocp --off shuffle &
fi
read
done
Of course, it requires mocp, my favorite music (on console) player. And obviously, you'll have to configure SEARCH_DIR but I'm sure some bash hacking is not that hard.
Beware though, using this + cron may have the undesired effect of awakening to the pleasant music of Cannibal Corpse.
In reply to this post, Nicolás Brailovsky » Blog Archive » Sorting by random in bash and mocp random updated commented @ 2010-09-09T09:04:03.000+02:00:
[...] And now you can sort by random your output using sort -R. Why would this be useful? Well, I updated my mocp random script with a [...]
Original published here.
C++: Magic member callbacks
Post by Nico Brailovsky @ 2009-07-21 | Permalink | Leave a comment
Short post about C++ this time - though calling it a request would be more appropiate. I'm trying to create some kind of magic callback to do this:
class Caller {
Callback c;
public:
Caller(Callback c) : c(c) {}
void doit(){ c(this); }
};
Shouldn't be too difficult, right?
There are some hidden complexities, of course, mostly regarding the callback parameter type, but the idea is simple, keep the caller dependant only in the callback, not in the callee.
Templates are not a valid solution as the callee may have more than one callback (i.e. expect more than a single object to finish and call the callback) so the whole idea of this is having the callback "bind" to a member method when created, doesn't matter which one.
I have a solution, tough I'm not too happy about it for now. I'll post it next week, unless someone comes up with a better idea (you know how to submit it if you do, right?).
In reply to this post, Leonardo Secotaro commented @ 2009-07-31T14:25:20.000+02:00:
This is my implementation
/----[CallBack.h]----/
class CallBack {
/ ... /
public:
virtual void operator() (void) = 0;
};
/----[Objeto.h]----/
#include "CallBack.h"
#include "Cain.h"
#include "Abel.h"
class Objeto: public CallBack{
/ ... /
public:
void Execute(){
Abel pObj = new Abel( this );
Cain pObj2 = new Cain( this );
pObj->execute();
pObj2->execute();
delete pObj;
delete pObj2;
}
/////////////////////////////////////////////////////
void Doit_Abel(Abel Who){
/ ... /
printf("I am a good Kidn");
}
//////////////////////////////////////////////////////
void Doit_Cain(Cain Who){
/ ... /
printf("I am a killern");
}
///////////////////////////////////////////////////////
void operator() (void Who) {
Adan pFather = (Adan) Who;
//EntryPoint
if (pFather->WhoamI() == CAIN )
Doit_Cain( (Cain)Who);
else if (pFather->WhoamI() == ABEL )
Doit_Abel( (Abel)Who);
}
};
/----[Adan.h]----/
enum Family{CAIN,ABEL};
class Adan{
public:
virtual Family WhoamI()=0;
};
/----[Abel.h]----/
#include "CallBack.h"
#include "Adan.h"
class Abel: public Adan{
/ ... /
CallBack _c;
public:
Abel(CallBack c): _c(c){}
Family WhoamI(){ return ABEL;}
void execute(){
/.../
printf("Abel::executen");
(_c)((void)this);
}
};
/----[Cain.h]----/
#include "CallBack.h"
class Cain: public Adan{
/ ... /
CallBack _c;
public:
Cain(CallBack c): _c(c){}
Family WhoamI(){ return CAIN;}
void execute(){
/.../
printf("Cain::executen");
(_c)((void)this);
}
};
/----[Main.h]----/
int main(void) {
Objeto pObjeto = new Objeto();
pObjeto->Execute();
delete pObjeto;
return EXIT_SUCCESS;
}
/prints/
Abel::execute
I am a good Kid
Cain::execute
I am a killer
/--------------------------------/
Original published here.
In reply to this post, nico commented @ 2009-08-02T22:30:14.000+02:00:
It does work but you're missing part of the "spec", you can't choose a callback function (i.e. you're tied to a single callback or a type overloaded callback). Other than that, the code is quite close to what I've been looking for. Check the solution back in a couple of days (still working on it!)
Original published here.
GNU/Linux: Emergency Restart
Post by Nico Brailovsky @ 2009-07-14 | Permalink | Leave a comment
It happens: we're happily hacking on some code and out of nowhere X server freezes. It may be the latest Compiz whose at fault, or perhaps a stray program that decided it should start consuming all available CPU. Anyway, it's easier to reboot than trying to fix whatever got broken but Ctrl - Alt - Backspace is unresponsive and we can't drop to a console. It's not ussual but it happens. What can we do about it?
There's a cool shortcut to help us when shit happens, it'll reboot the computer and it's a little bit nicer than yanking out the power cord. You just need to remember REISUB and have some keyboard dexitry - holding down Ctrl - Alt - SysRQ/PrintScreen is required while typing REISUB (don't do it now, it'll reboot your computer!).
So, what's REISUB all about? It's a little bit better than a forced hard reboot because it'll:
- R: Restore console
- E: Send SIGTERM to all processes
- I: Send SIGKILL to all processes
- S: Emergency sync of all filesystems (commit any changes to the phisical media)
- U: Read only remount of all filesystems
- B: Reboot now
So, off course, you'll have to wait a little bit between every keystroke. Press Ctrl + Alt + PrntScreen + H on a console to get some help on every command.
Why does it work?
There's a lot of magic involved to make this secret incantation work. It involves kernels, vectors and other mythical beasts. There's a crazy thing called interruption vector; it's the place where every (hardware) event gets dispatched to a handling function. There lives a function call to handle keyboard input, amongst other things. This function call will be executed always, though the SO may just decide to queue the keyboard input if it's too busy handling something else.
Well, this key combination can't be delayed 'till later, it must be handled NOW, therefore, even if there's a stray process or a driver gone mad, it'll always be caught and the computer will be rebooted.
What's the catch? You won't be saving that precious code you we're hacking away when it all started, but at least you'll save some fscking time on the next start up.
In reply to this post, Anonymous commented @ 2010-02-14T21:23:58.000+01:00:
Ctrl is not needed!
Original published here.
In reply to this post, nico commented @ 2010-02-14T22:45:17.000+01:00:
Really? All those years practicing yoga so I could reach that weird key combo, for nothing? Man, wish I knew that one sooner :)
Original published here.
In reply to this post, Yuvi commented @ 2010-05-18T15:28:45.000+02:00:
I knew only about the B, thanks for the others.
And no, you don't have to press Ctrl :)
Original published here.
In reply to this post, dsfadsfgafgf commented @ 2010-05-18T15:39:29.000+02:00:
I have to keep a finger on my laptops Fn key to do this. It's most annoying. Almost snaps my fingers.
Original published here.
In reply to this post, thisisabore commented @ 2010-05-18T15:43:02.000+02:00:
RESIUB, also remembered with the nice-to-know “Raising Skinny Elephants Is Utterly Boring”.
RESIUO will Power-Off instead of rebooting, which might sometimes also be useful.
You'd be surprised how many kiosks running Linux don't have this sequence disabled…
Original published here.
In reply to this post, Anonymous commented @ 2010-05-18T15:50:55.000+02:00:
that will not work with kernels where the magic sysrq feature is not enabled
Original published here.
In reply to this post, nico commented @ 2010-05-18T15:54:45.000+02:00:
dsfadsfgafgf
Indeed. Luckly most laptops tend to have a smaller keyboard :)
thisisabore
I'll have to try it next time I see one. There are not much of those over here, though. Most of the kiosks are Windows (it's fun to see the BSODs too)
Original published here.
In reply to this post, David commented @ 2010-05-18T17:40:57.000+02:00:
Seems to me you don't need the first 3. "R" seems nice, though. "SUB" should be enough. I didn't know about "U". I've been doing "SSSSSSB" for years...
Original published here.
In reply to this post, nico commented @ 2010-05-18T17:51:10.000+02:00:
David
May be so may be not, but the look of awe when people look at you pressing a magical 18 key combo to reboot is priceless.
Original published here.
In reply to this post, Dan commented @ 2010-05-18T18:21:33.000+02:00:
Why do people keep spelling fist f--king: "fscking"?
Original published here.
In reply to this post, yono commented @ 2010-05-24T18:53:29.000+02:00:
that's because they actually mean "fscking" - as in the verb "to run the fsck command"
http://linux.die.net/man/8/fsck
the point of this article is to cleanly shutdown your computer when you cannot use conventional commands or menus to do so. this prevents data corruption or an inconsistent disk state, which can sometimes be fixed by running the fsck command on the disk.
Original published here.
In reply to this post, Dan commented @ 2011-06-27T04:09:35.000+02:00:
"It's not usual but it happens."
Yeah, like ever f'ing day.
Original published here.
Vim Tip: Select everything
Post by Nico Brailovsky @ 2009-07-11 | Permalink | Leave a comment
There's an easy way to select the whole contents of the document: "ggVG". May seem like a lot at first glance but let's review it part by part:
- gg: Go to the beggining of the document
- V: Enter visual mode
- G: Go to the end of the document
Easy, isn't it? Now you can press any other command to copy, delete or whatever you like.
LaTeX: Including Source Code
Post by Nico Brailovsky @ 2009-07-09 | Permalink | Leave a comment
I'm sure every programmer has had to include source code in a document at least once, an architecture documentation for example, or perhaps as part of a presentation (beamer FTW, coming soon!). With most document formats including source code usually means writing the code and use some kind of manual syntax higlighting. Of course, any time you change the source you'll need to manualy update the document.
In LaTeX there's an easy way to handle source code. Though it was difficult to figure out, once in place this requires no maintenance at all - no need to manually syntax highlight your code nor update any files other than the source code itself. Even more, you can even change the source code language and it won't even matter in the presentation.
Preliminars
You'll need "pygmentyze" which is provided by the package python-pygments:
apt-cache search pygment
python-pygments - syntax highlighting package written in Python
sudo apt-get install python-pygments
Using it is really easy, check the manpage. Anyway, I'm too lazy to generate the syntax highlighted document by hand, let's add it to the makefile.
Automating the source code generation
Let's beging by adding a "code" folder inside our project. We'll store there all the source code files, and having them in a separated folder will enable us to add a target in the makefile to automatically update the source code in the LaTeX file.
Remember the makefile from a couple of posts back? Let's add a target:
# Hack to make it work when foo.code => foo.code.tex
code/%.tex: code/%. code/%.aux code/%.tex
@rm code/.aux
$(MAKE) -C .
code/%.tex: code/%.
pygmentize -f latex -O style='border=#000000,colorful,linenos=1' $< > $@
# Search each code file to format and include
CODE_FILES:=$(shell ls code/|egrep -v '.tex$ |.aux$ ' )
CODE_FILES_DEP:=$(addprefix code/, $(CODE_FILES))
CODE_FILES_TGT:=$(addsuffix .tex, $(basename $(CODE_FILES_DEP)))
main.pdf: $(shell ls .tex) $(CODE_FILES_TGT)
pdflatex main.tex > /dev/null
OK, maybe a couple more than one. It may seem like a lot of makefile code but all it does is define a code directory and a target to run pygmentize on each source file found there. We'll have to add a dependency in the document's target so it'll be automatically generated with each build:
main.pdf: code_frames *.tex
pdflatex main.tex && pdflatex main.tex
and then, we'll need to clean up the new temp files:
code_clean:
@rm -f $(CODE_FILES_TGT) code/*.aux
Don't worry, there's a link to the full makefile.
At last!
It shouldn't have been too much work and we're done anyway. To include a source code file in your document now use the include command (like include{code/foobar.cpp) and re build. I'm attaching a complete example in a zip file, with my latest implementation of a bogosort algorithm (now 50% faster).
In reply to this post, Anonymous commented @ 2010-02-14T21:25:57.000+01:00:
You can use the package listings too.
Original published here.
LaTeX: including documents
Post by Nico Brailovsky @ 2009-07-07 | Permalink | Leave a comment
This is a post from my LaTeX series - check the others too!
So far we've seen some of LaTeX advantages, and a few basic commands to get you started. Let's see a trick to be more proficient with it:
Including other tex files
You should be able to write some simple documents now, some in LyX, some in LaTeX, but you'll soon start to notice that using a single text file to create a large document becomes cumbersome. Even more so if you need to split the work between several people in a team.
There's an easy way to keep a main file and then several, smaller, files in which you can work more comfortably:
include{file.tex}
Easy, right?
Beware, you can't use an include inside an include. Why? No idea, but there's a way around this:
input{file.tex}
Quick preview
Using includes has another advantage: you can have a quick preview while working with a chapter at a time. I usually keep the following structure within my projects:
% Header declarations
% Include packages
% Document preamble
% ...
%input{chapter1.tex}
%input{chapter2.tex}
input{chapter3.tex}
% input{chapter4.tex}
Just uncomment the chapter you're working with. In big documents this has a very noticeable effect, as compiling a large LaTeX file into an enormous pdf document (several MBs) may be quite slow.
Of course, I use "input" in my main file so I can use include in the chapters themselves. I won't usually need to include other documents inside the chapters, it'd get quite messy, but it's necessary to work with embedded documents, as we'll see in another post.
Vim Trivia
Post by Nico Brailovsky @ 2009-07-04 | Permalink | Leave a comment
Did you knew that Vim can "Make your woman smile for a week"? Me neither: http://www.vim.org/trivia.php.
LaTeX: Makefile
Post by Nico Brailovsky @ 2009-07-02 | Permalink | Leave a comment
Remember I said that being a programmer would make you a lot more comfortable around LaTeX? The reason is quite simple, tex is just source code for a document. As with any source code in Linux (Windows too, but that is besides the point) you can use a Makefile to compile it and make your life easier.
I have already posted this Makefile in another entry but it's time to explain how it works.
all: main.pdf
main.pdf: code_frames .tex
pdflatex main.tex && pdflatex main.tex
.PHONY: run clean edit
edit:
gvim -S vim.sess
run: main.pdf
evince main.pdf &
clean:
@# for each .tex file, remove the extension
@# and delete its generated files
@for PART in $(shell ls .tex| sed 's:.tex::g'); do
echo ".out .nav .aux .toc .log .snm .pdf .vrb" | \
sed "s:*:$$PART:g" | xargs rm -f;
done
It's rather easy, let's check it target by target:
- all: create the main document - used as default target
- main.pdf: document's target - no need to call it main.pdf, I just do it because the entry point in C programs is called "main" so I'm used to it. Also, it looks better than foo.pdf. As a side note, it runs twice "pdflatex" because it first creates the document and the second time updates the document's index.
- edit: I usually have a single document split in many files, so keeping a line to quickly open up your editor with all this files is handy. I just keep it as a Vim session, no need for more
- run: target to open up the document in a pdf viewer. I call it run so I can use it with my default mapping in Vim
- clean: clean up the files created while compiling a document. Some times it's needed if there's an error you can track - it may be a corrupted .aux file
Short entry this time - next: using source code from within LaTeX.