forked from aerospike-community/aerospike-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_util.php
77 lines (69 loc) · 1.77 KB
/
examples_util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
global $has_pygmentize;
exec("which pygmentize 2>&1", $output, $exit_code);
if ($exit_code > 0) {
$has_pygmentize = false;
}
else {
$has_pygmentize = true;
}
function colorize($str, $color, $bold = false) {
$start = "\033[";
if ($bold) $start .= "1;";
else $start .= "0;";
switch ($color) {
case 'black':
$start .= "30";
break;
case 'blue':
$start .= "34";
break;
case 'green':
$start .= "32";
break;
case 'red':
$start .= "31";
break;
case 'gray':
$start .= "37";
break;
case 'cyan':
$start .= "30";
break;
case 'purple':
$start .= "35";
break;
default:
$start .= "30";
break;
}
return $start. 'm'. $str. "\033[0m";
}
function success() {
return colorize(" [✓] ", 'green', true)."\n";
}
function fail($msg) {
return colorize(" [✗] \n".$msg, 'red', true)."\n";
}
function standard_fail($db) {
return fail("Error [{$db->errorno()}] {$db->error()}");
}
function display_code($path, $after, $till) {
global $has_pygmentize;
if ($has_pygmentize) {
// check if the file has already been pygmentized
$cache_path = '/tmp/.'.basename($path);
if (file_exists($path) && file_exists($cache_path) &&
(filemtime($path) <= filemtime($cache_path))) {
$path = $cache_path;
} else {
exec("pygmentize -fterminal256 -lphp -o$cache_path $path 2>&1", $o, $r);
$path = $cache_path;
}
}
$end = $till - 1;
$len = $end - $after;
passthru("cat -n $path 2>&1|head -n $end |tail -n $len");
echo "\n";
}
?>