-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclui.php
More file actions
148 lines (113 loc) · 2.72 KB
/
clui.php
File metadata and controls
148 lines (113 loc) · 2.72 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
if ( ! function_exists('ncurses_init'))
{
die ('Clui requires the ncurses php extension.');
}
require_once 'clui_view.php';
require_once 'clui_list.php';
require_once 'clui_table.php';
class Clui {
protected static $instance;
public $screen;
public $view;
const WHITE = 1,
GREEN = 2,
YELLOW = 3,
RED = 4,
BLUE = 5,
KEY_ESCAPE = 27,
KEY_ENTER = 13;
// Singleton instantiation only
protected function __construct() {}
public static function init($config = array())
{
$instance = self::$instance = new self;
ncurses_init();
extract($config);
// Window size
! isset($width) && $width = 0;
! isset($height) && $height = 0;
// Create the root View
$instance->view = Clui_View::make(0, 0, 0, 0);
ncurses_curs_set(0);
if ( ! empty($color)) ncurses_start_color();
ncurses_init_pair(self::WHITE, NCURSES_COLOR_WHITE, NCURSES_COLOR_BLACK);
ncurses_init_pair(self::GREEN, NCURSES_COLOR_GREEN, NCURSES_COLOR_BLACK);
ncurses_init_pair(self::YELLOW, NCURSES_COLOR_YELLOW, NCURSES_COLOR_BLACK);
ncurses_init_pair(self::RED, NCURSES_COLOR_RED, NCURSES_COLOR_BLACK);
ncurses_init_pair(self::BLUE, NCURSES_COLOR_BLUE, NCURSES_COLOR_BLACK);
ncurses_noecho();
if ( ! empty($border))
{
self::setBorder(true);
}
if ( ! empty($title))
{
self::setTitle($title);
}
set_error_handler(function($code, $error, $file, $line)
{
Clui::end();
die('Error: '.$error."\nin $file (line $line)");
});
ncurses_refresh();
self::draw();
}
public static function instance()
{
return self::$instance;
}
public static function rootView()
{
return self::$instance->view;
}
public function __call($method, $args)
{
return call_user_func_array(array(self::$instance->view, $method), $args);
}
public static function __callStatic($method, $args)
{
return call_user_func_array(array(self::$instance->view, $method), $args);
}
public function __get($key)
{
return self::$instance->$key;
}
public function __set($key, $value)
{
return self::$instance->$key = $value;
}
public static function end()
{
if ($instance = self::instance())
{
ncurses_end();
self::$instance = null;
}
}
public static function debug($var)
{
self::end();
var_dump($var);
die();
}
public static function log($str, $clear = false)
{
static $contents;
$clear && $contents = '';
$contents .= $str."\n";
list($x, $y, $w, $h) = self::getFrame();
$width = $w/2;
$height = count(explode("\n",$contents))+1;
$x = $w/2 - $width/2;
$y = $h - $height - 2;
$alert = Clui_View::make()
->setParent(Clui::rootView())
->setFrame($x, $y, $width, $height)
->setBorder(true)
->draw()
->addString(1,0, $contents)
->draw();
return $alert;
}
}