about summary refs log tree commit diff stats
path: root/console.mu
blob: cc81c2323dbb557f1323ea7f363d78e453160a04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# example program: reading events from keyboard or mouse
#
# Keeps printing 'a' until you press a key or click on the mouse.

def main [
  local-scope
  open-console
  {
    e:event, found?:bool <- check-for-interaction
    break-if found?
    print-character-to-display 97, 7/white
    loop
  }
  close-console
  $print e, 10/newline
]
Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Test program to make sure that dirent compiles cleanly with C++
 *
 * Copyright (C) 1998-2019 Toni Ronkko
 * This file is part of dirent.  Dirent may be freely distributed
 * under the MIT license.  For all details and documentation, see
 * https://github.com/tronkko/dirent
 */
#include <iostream>
#include <string.h>
#include <dirent.h>
#include <assert.h>
using namespace std;

/* Filter and sort functions */
static int only_readme(const struct dirent *entry);
static void test_retrieval(void);
static void test_scan(void);

int
main(int argc, char *argv[])
{
	(void) argc;
	(void) argv;

	test_retrieval();
	test_scan();

	cout << "OK" << endl;
	return EXIT_SUCCESS;
}

/* Test basic directory retrieval */
static void
test_retrieval(void)
{
	/* Open directory */
	DIR *dir = opendir("tests/1");
	if (dir == NULL) {
		cerr << "Directory tests/1 not found" << endl;
		abort();
	}

	/* Read directory entries */
	struct dirent *ent;
	int found = 0;
	while ((ent = readdir(dir)) != NULL) {
		/* Check each file */
		if (strcmp(ent->d_name, ".") == 0) {
			/* Directory itself */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 1);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 1);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 1);
#endif
			found += 1;
		} else if (strcmp(ent->d_name, "..") == 0) {
			/* Parent directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 2);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 2);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 2);
#endif
			found += 2;
		} else if (strcmp(ent->d_name, "file") == 0) {
			/* Regular file */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_REG);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 4);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 4);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 4);
#endif
			found += 4;
		} else if (strcmp(ent->d_name, "dir") == 0) {
			/* Just a directory */
#ifdef _DIRENT_HAVE_D_TYPE
			assert(ent->d_type == DT_DIR);
#endif
#ifdef _DIRENT_HAVE_D_NAMLEN
			assert(ent->d_namlen == 3);
#endif
#ifdef _D_EXACT_NAMLEN
			assert(_D_EXACT_NAMLEN(ent) == 3);
#endif
#ifdef _D_ALLOC_NAMLEN
			assert(_D_ALLOC_NAMLEN(ent) > 3);
#endif
			found += 8;
		} else {
			/* Other file */
			cerr << "Unexpected file " << ent->d_name << endl;
			abort();
		}
	}

	/* Make sure that all files were found */
	assert(found == 0xf);

	closedir(dir);
}

/* Text basic scan with simple filter function */
static void
test_scan(void)
{
	struct dirent **files;

	/* Read directory entries */
	int n = scandir("tests/3", &files, only_readme, alphasort);
	assert(n == 1);

	/* Make sure that the filter works */
	assert(strcmp(files[0]->d_name, "README.txt") == 0);

	/* Release file names */
	for (int i = 0; i < n; i++) {
		free(files[i]);
	}
	free(files);
}

/* Only pass README.txt file */
static int
only_readme(const struct dirent *entry)
{
	int pass;

	if (strcmp (entry->d_name, "README.txt") == 0) {
		pass = 1;
	} else {
		pass = 0;
	}

	return pass;
}