tags:

views:

56

answers:

1

Hello people!

I am an amateur try to hack together a little project. It is a simple note storage and retrieval console app on Windows Vista (and XP - i'm hoping to run the whole thing off a USB Stick).

I use Sqlite as the store and Tcl/SQL scripts to add notes (and tags!) and also retrieve them by tag. 3 tables and a "Toxi" schema.

So anyway... I want to use it from either a "dos prompt" or more frequently tclsh (NOT wish!) I don't want the windowing shell or to use TK at all. But to help visually distinguish some things, stdin from stdout, notes from timestamps, etc, I want to change the font color on-the-fly with some kind of crude markup.

I found a c++ project that will do exactly this! Jaded Hobo put it up on: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=9130. Jaded Hobo says the header file "Console.H" is sufficient to include in a c++ project, but he doesn't know TCL.

I found SWIG, the interface compiler, and I'm going to give it a try. But I'm clueless on a few things:

  1. Can just a header file be enough?

  2. The SWIG Win32 examples aren't as edifying as the 'nix example and they use MS VC++ (VStudio)- I want to use Quincy/MinGW.

    (Oh, btw this is my first ever attempt at using C of any kind. So can you show how to use SWIG with Quincy?)

  3. How can I glean from the header source just what the heck to type in my Tcl script to use it?

Thank you for reading this, let alone answering. I started to put it on comp.lang.tcl but I don't like my email addr broadcast like that.

+2  A: 

A header isn't enough by itself. On the other hand, you really don't need to go to all that much work since this page indicates that the API is actually really simple. Here's the C code that you need:

#include <tcl.h>
#include <windows.h>

static int MySetConsoleColorCmd(
    ClientData clientData, Tcl_Interp *interp,
    int objc, Tcl_Obj *const objv[])
{
    HANDLE hConsole;
    int code;

    /* Parse arguments, first for argument count, then for number format */
    if (objc != 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "colorCode");
        return TCL_ERROR;
    } else if (Tcl_GetIntFromObj(interp, objv[1], &code) != TCL_OK) {
        return TCL_ERROR;
    }

    /* Get console handle, checking for the error case */
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole == INVALID_HANDLE_VALUE) {
        Tcl_SetResult(interp, "not a console application", TCL_STATIC);
        return TCL_ERROR;
    }

    /* Set the color! */
    SetConsoleTextAttribute(hConsole, code);
    return TCL_OK;
}

/* Standard entry point for loadable library */
int Consolecolor_Init(Tcl_Interp *interp) {
    Tcl_CreateObjCommand(interp, "consolecolor", MySetConsoleColorCmd,
            NULL, NULL);
    return TCL_OK;
}

Compile this up into a DLL (it's got no fancy dependencies at all, beyond Tcl itself) called consolecolor.dll (the name should match the entry point function somewhat) and then you'll be able to use the load command to import the new consolecolor command into your code, like this:

load /path/to/consolecolor.dll

# Duplicate example from the page mentioned at the top of this answer
for {set k 1} {$k < 255} {incr k} {
    consolecolor $k
    puts "$k => I want to be nice today!"
}

For a guide to how to pick colors, see this MSDN page.

Donal Fellows
Thanks Mr. Fellows - I recognize your name and pic - you must be very generous with you time. I tried quickly with Quincy. I checked the DLL option in the Project but it gave the file a .o extension!So I copied a tcl.h and the consolecolor.c to MinGW\bin and tried a command-line compile with result you see below. I'm ready to hire someone to do this!C:\quincy\MinGW\bin>gcc -shared consolecolor.c -o consolecolor.dllconsolecolor.c:1:17: error: tcl.h: No such file or directoryconsolecolor.c:5: error: expected ')' before 'clientData'consolecolor.c:32: error: expected ')' before '*' token
klausnrooster
@klaus: You need to pass the name of the directory containing tcl.h to GCC using the `-I` option. Thus, if that's the current directory, use `-I.`
Donal Fellows
Anybody who's interested, I put the dll compilation up as a project on www.vworker.com (formerly rentacoder). My handle there is TclHobbyist. I tried and tried. Either I got error messages or a success that resulted in a consolecolor.o file whatever that is. [Uuuuughhhhh!]
klausnrooster
My vworker in Serbia produced the dll in no time flat and it works great. Thanks to him and Donal. Exactly what I was looking for.
klausnrooster