Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 225d7c1

Browse files
committed
Fix #24 + optimisations
1 parent a1c3e60 commit 225d7c1

File tree

8 files changed

+47
-19
lines changed

8 files changed

+47
-19
lines changed

core/AutocompleteSocket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function openSocket(){
4747
}
4848
}
4949
else {
50-
echo "ERROR: Unable to bind socket to '" . $this->socketpath . "'!" . PHP_EOL;
50+
CLIOutput::error( CLIOutput::ERROR_FATAL, "ERROR: Unable to bind socket to '" . $this->socketpath . "'!");
5151
}
5252

5353
}
@@ -183,7 +183,7 @@ public static function createSocketThread(){
183183
}
184184
}
185185
else {
186-
echo "INFO: Automatic Socket starting disabled!" . PHP_EOL;
186+
CLIOutput::error( CLIOutput::ERROR_INFO, "Automatic Socket starting disabled!");
187187
}
188188
}
189189

@@ -192,7 +192,7 @@ public static function getWinSocketFile() : string {
192192
return getenv('USERPROFILE') . '/AppData/Local/Temp/TaskTimeTerminateAutocomplete.sock';
193193
}
194194
else{
195-
echo "ERROR: Used windows socket path function (AutocompleteSocket::getWinSocketFile) on non-windows OS!" . PHP_EOL;
195+
CLIOutput::error( CLIOutput::ERROR_FATAL, "Used windows socket path function (AutocompleteSocket::getWinSocketFile) on non-windows OS!");
196196
return "";
197197
}
198198
}

core/CLIOutput.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,30 @@ class CLIOutput {
1515
const WHITE = "\e[0;37m";
1616
const RESET = "\e[0;0m";
1717

18-
public static function colorString($s, $color) : string {
18+
const ERROR_WARN = 1;
19+
const ERROR_INFO = 2;
20+
const ERROR_FATAL = 3;
21+
22+
public static function colorString(string $s, string $color) : string {
1923
return $color . $s . self::RESET;
2024
}
2125

26+
public static function error(int $type, string $msg) : void {
27+
$p = "";
28+
switch ($type){
29+
case self::ERROR_WARN:
30+
$p = self::colorString("WARN", self::YELLOW);
31+
break;
32+
case self::ERROR_INFO:
33+
$p = self::colorString("INFO", self::BLUE);
34+
break;
35+
case self::ERROR_FATAL:
36+
$p = self::colorString("ERROR", self::RED);
37+
break;
38+
}
39+
echo $p . ': "' . $msg . '"' . PHP_EOL;
40+
}
41+
2242
public function __construct() {
2343
$this->hello();
2444
}

core/Recorder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(bool $inTerminal = false) {
1919
AutocompleteSocket::createSocketThread();
2020
break;
2121
case Utilities::OS_LINUX:
22-
MacDialog::checkOSPackages();
22+
LinuxDialog::checkOSPackages();
2323
$this->dialog = new LinuxDialog();
2424
break;
2525
case Utilities::OS_WIN:
@@ -96,7 +96,7 @@ private function saveTaskTime(JSONReader $r) : void {
9696
));
9797
}
9898
else{
99-
echo "WARN: Last task is already stored -- therefore TTT ignores it." . PHP_EOL;
99+
CLIOutput::error(CLIOutput::ERROR_WARN, "Last task is already stored -- therefore TTT ignores it.");
100100
}
101101
// also save to sync
102102
StatsLoader::saveDayTasks( $data->getArray() );

core/Time.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ class Time {
33

44
private const DEFAULT_FORMAT = 'dhm|s';
55
private const ALLOWED_PARTS = array(
6-
'd' => 24*60*60,
7-
'h' => 60*60,
8-
'm' => 60,
9-
's' => 1
6+
'y' => 365*24*60*60, // year
7+
't' => 30*24*60*60, // month (one of twelve)
8+
'w' => 7*24*60*60, // week
9+
'd' => 24*60*60, // day
10+
'a' => 6*60*60, // work-day (8 hours)
11+
'h' => 60*60, // hour
12+
'm' => 60, // minute
13+
's' => 1 // second
1014
);
1115
private const DELIMITER = '|';
1216
private const PAD_VALUE = 4;
@@ -35,8 +39,8 @@ public function __construct() {
3539
str_replace('||', '', $this->timeformat) === $this->timeformat;
3640

3741
if(!$ok){
38-
$this->timeformat = self::DEFAULT_FORMAT;
39-
echo "ERROR: Invalid timeformat in config.json" . PHP_EOL;
42+
$this->timeformat = self::DEFAULT_FORMAT;
43+
CLIOutput::error( CLIOutput::ERROR_WARN, "Invalid timeformat in config.json" );
4044
}
4145

4246
// calculate pad

core/Utilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
class Utilities {
66

7-
const VERSION = 'v1.0.0 rc';
7+
const VERSION = 'v1.0.0 rc2';
88

99
/**
1010
* OS Consts

core/platform/Dialog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function createCommandLineArgs(string $dialogPath) : string {
8888
'-cats',
8989
'"'. implode(',', $this->categories) .'"',
9090
'-lastcat',
91-
'"'.$this->categories[$this->chCategory ?? 0] .'"'
91+
'"'.$this->categories[$this->chCategory ?? array_key_first($this->categories)] .'"'
9292
);
9393
if( !empty($this->chName)){
9494
array_push($cmd,

core/platform/WindowsDialog.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public static function checkOSPackages() : void {
3131
$zip->extractTo(__DIR__ . '/php-gtk/');
3232
$zip->close();
3333

34-
// activate Sockets in PHP-GD
35-
file_put_contents( __DIR__ . '/php-gtk/PHP55-GTK2/php.ini', PHP_EOL . 'extension = "php_sockets.dll"' . PHP_EOL, FILE_APPEND);
36-
3734
unlink(__DIR__ . '/d.zip');
3835

3936
exec(self::PHP_GTK_TEST, $output);
@@ -50,6 +47,13 @@ public static function checkOSPackages() : void {
5047
die( PHP_EOL . 'Error downloading PHP-GTK!!' . PHP_EOL . PHP_EOL);
5148
}
5249
}
50+
51+
// check if Sockets in PHP-GD active
52+
$iniActivate = PHP_EOL . 'extension = "php_sockets.dll"' . PHP_EOL;
53+
$ini = file_get_contents( __DIR__ . '/php-gtk/PHP55-GTK2/php.ini' );
54+
if( strpos($ini, $iniActivate) === false ){
55+
file_put_contents( __DIR__ . '/php-gtk/PHP55-GTK2/php.ini', $iniActivate, FILE_APPEND);
56+
}
5357
}
5458
}
5559

socket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
$socket = new AutocompleteSocket(AutocompleteSocket::getWinSocketFile());
1212
break;
1313
default:
14-
echo "ERROR: Unable to create Socket on this OS!" . PHP_EOL;
14+
CLIOutput::error( CLIOutput::ERROR_FATAL, "Unable to create Socket on this OS!" );
1515
break;
1616
}
1717
}
1818
else{
19-
echo "ERROR: Socket Library not loaded in this installation!" . PHP_EOL;
19+
CLIOutput::error( CLIOutput::ERROR_WARN, "Socket Library not loaded in this php installation!" );
2020
}
2121
?>

0 commit comments

Comments
 (0)