Simulate.
Here's a simple function I wrote to simulate typing. The
echodelay()
function takes parameters that describe the delay before printing
characters, spaces and newlines. The last parameter is the string to
print. The function loops through the string and pauses before printing
each character, then flushes the output buffer. The effect is that each
character seems to appear one at a time, as though someone were typing
at a keyboard:
void echodelay (int chdelay, int spdelay, int nldelay, char *string) { int pos = 0; /* add a delay between printing each character in the string, depending on the character */ do { switch (string[pos]) { case '\0': /* new line */ msleep (nldelay); break; case ' ': /* space */ msleep (spdelay); break; default: /* character */ msleep (chdelay); break; } putchar (string[pos]); fflush (stdout); } while (string[pos++] != '\0'); }
With that function, it's a simple process to write a parent
main()
function to parse the command line, set the different delays, and call the
echodelay() function to generate the output with the
appropriate delays:
/* echodelay.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> void echodelay (int chdelay, int spdelay, int nldelay, ↪char *string); int msleep (int millisec); int atoipos (char *string); int main (int argc, char **argv) { int opt; int chdelay = 0, spdelay = 0, nldelay = 0; /* parse command line */ while ((opt = getopt (argc, argv, "c:s:n:")) != -1) { switch (opt) { case 'c': /* -c nnn */ chdelay = atoipos (optarg); break; case 's': /* -s nnn */ spdelay = atoipos (optarg); break; case 'n': /* -n nnn */ nldelay = atoipos (optarg); break; default: /* unrecognized option */ fprintf (stderr, "Usage: echodelay [-c millisec] ↪[-s millisec] [-n millisec] [text..]\n"); exit (1); break; } } /* pass all remaining options as text to echodelay() */ for (opt = optind; opt < argc; opt++) { echodelay (chdelay, spdelay, nldelay, argv[opt]); putchar (' '); } putchar ('\n'); exit (0); } void echodelay (int chdelay, int spdelay, int nldelay, char *string) { ... } int msleep (int millisec) { ... } int atoipos (char *string) { int val; /* wrapper to atoi() but always a positive return value */ val = atoi (string); if (val < 0) { val = 0; } return (val); }
And compile it like this:
gcc -Wall -o echodelay echodelay.c
In a shell script, I had commands to print a "prompt", then simulate typing a command before executing the command—for example, this command to generate a file of random size in your /tmp directory:
echo -n 'prompt$' echodelay -c 500 -s 1000 -n 2000 'ls -lh /tmp' ls -lh /tmp
This is a fairly straightforward C program to simulate typing. I wrote it quickly to do a single job, but it works as a solution to simulate typing while I narrated my how-to video. In this way, I didn't need to think about what I was typing while I was trying to describe it. If you need to simulate typing for a similar task, I hope you find this program useful.