Skip to content

Instantly share code, notes, and snippets.

@skriticos
Created March 26, 2010 10:42
Show Gist options
  • Save skriticos/344756 to your computer and use it in GitHub Desktop.
Save skriticos/344756 to your computer and use it in GitHub Desktop.
Slow print functions.
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
/* maximal size of printing (the rest is truncated) */
#define MAXSIZE 1024*128
extern int nanosleep();
static void slowprn(char *str, unsigned mwait, bool linewise);
int main(void)
{
int c, i = 0;
char buf[MAXSIZE];
while (((c = getchar()) != EOF) && (i < (MAXSIZE-1)))
buf[i++] = (char) c;
buf[i] = '\0';
slowprn(buf, 200, false);
return 0;
}
/* writes a string with delays to the standard output */
static void slowprn(char *str, unsigned mwait, bool linewise)
{
int i;
static struct timespec ts;
if (mwait > 1000) {
ts.tv_sec = mwait / 1000;
ts.tv_nsec = (mwait % 1000) * 100000;
} else {
ts.tv_sec = 0;
ts.tv_nsec = mwait * 100000;
}
for (i = 0; (size_t) i < strlen(str); i++) {
(void) putchar(str[i]);
if ((linewise && (str[i] == '\n')) || (!linewise)) {
(void) nanosleep(&ts, NULL);
(void) fflush(stdout);
}
}
(void) fflush(stdout);
}
#! /bin/bash
# ============================================================================ #
# Name: decho.sh
# Purpose: Prints the input arguments with a delay between the characters
# to the terminal. Behaves like /echo/ with no options.
# Input: - $* String, that will be printed.
# - $MM_DECHO_STEPPING
# If this environment variable is set, it is used as the
# stepping for delay. It is verified before. If
# verification fails, the default value is used.
# - $DEBUG
# If this environment variable is set to "true", extra
# debug information is printed.
# Last modified: 2008-11-06
# Usage: mm_decho [STUFF YOU WANT TO BE PRINTED] or
# MM_DECHO_STEPPING="0.2"; mm_decho [STUFF, AGAIN]
# Data: Low, only breaks streams (should not be used anyway here)
# ============================================================================ #
mm_decho ()
{
local charpos stepping all_args charpos_num_array stp stp_int stp_flt
stepping="0.1"
if [ -n "$MM_DECHO_STEPPING" ]; then
stp=$MM_DECHO_STEPPING
stp_int=$(echo $stp | grep -oE '[[:digit:]]+')
stp_flt=$(echo $stp | grep -oE '[[:digit:]]+\.[[:digit:]]+')
if [[ $stp = $stp_int || $stp = $stp_flt ]]; then
stepping=$stp
elif [[ $DEBUG="true" ]]; then
echo ".! Stepping for delay found, but it seems" 1>&2
echo ".! to be invalid (not /float/): \"$stp\"" 1>&2
echo ".! Using default stepping.." 1>&2; fi; fi
all_args="$*"
charpos_num_array="$(seq 0 $((${#all_args}-1)))"
for charpos in $charpos_num_array; do
echo -n "${all_args:$charpos:1}"
sleep $stepping; done
echo
}
for a in $@
do
mm_decho "$(cat $a)"
done
#! /usr/bin/env python
# ------------------------------------------------------------------------------
# Simple program, walks through a directory tree and slow print every py file,
# another one..
# ##### FILE: <filename> #####
# <content>
# ------------------------------------------------------------------------------
import os
import sys
import time
def outfname(fname):
print 5*'#', fname, 5*'#'
def outfile(fname):
"""Read files and slow print content
"""
with open(fname, 'r') as fp:
data = fp.read()
for i in range(len(data)):
sys.stdout.write(data[i])
sys.stdout.flush()
time.sleep(0.01)
def searchfiles():
"""Walk a directory tree and make a list of files
"""
filelist = list()
for root, dirs, files in os.walk(os.getcwd()):
for f in files:
if f.count(".py") and not f.count(".pyc") and not f.count(".swp"):
filelist.append(os.path.join(root, f))
return filelist
if __name__ == "__main__":
os.system("clear")
files = searchfiles()
for f in files:
outfname(f)
outfile(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment