Bearsampp 2026.7.11
Loading...
Searching...
No Matches
class.tpl.powershell.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
17{
18 // Icon constants - Currently unused as native PowerShell consoles do not support custom icons
19 // These icons were used with ConsoleZ but are not supported by standard PowerShell console host
20 // To use icons, consider using a terminal emulator like ConEmu, Cmder, or Windows Terminal
21 // Icon files are located at: core/resources/homepage/img/icons/
22 /*
23 const ICON_APP = 'app.ico';
24 const ICON_POWERSHELL = 'powershell.ico';
25 const ICON_PEAR = 'pear.ico';
26 const ICON_DB = 'db.ico';
27 const ICON_GHOSTSCRIPT = 'ghostscript.ico';
28 const ICON_GIT = 'git.ico';
29 const ICON_NODEJS = 'nodejs.ico';
30 const ICON_COMPOSER = 'composer.ico';
31 const ICON_PYTHON = 'python.ico';
32 const ICON_RUBY = 'ruby.ico';
33 const ICON_PERL = 'perl.ico';
34 const ICON_NGROK = 'ngrok.ico';
35 */
36
40 private function __construct()
41 {
42 }
43
50 public static function process()
51 {
52 global $bearsamppTools;
53
54 $fontName = 'CaskaydiaMono NF'; // Default Nerd Font
55
56 // Collect all console window titles that need font configuration
57 $titles = [];
58 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleDefault();
59 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePowershell();
60 $titles[] = 'Console';
61 $titles[] = 'Bearsampp Powershell Console'; // Fallback casing
62
63 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePear(); } catch (Exception $e) {}
64 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleMysql(); } catch (Exception $e) {}
65 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleMariadb(); } catch (Exception $e) {}
66 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePostgresql(); } catch (Exception $e) {}
67 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleGit(); } catch (Exception $e) {}
68 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleNodejs(); } catch (Exception $e) {}
69 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleComposer(); } catch (Exception $e) {}
70 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePython(); } catch (Exception $e) {}
71 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleRuby(); } catch (Exception $e) {}
72 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePerl(); } catch (Exception $e) {}
73 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleGhostscript(); } catch (Exception $e) {}
74 try { $titles[] = $bearsamppTools->getPowerShell()->getTabTitleNgrok(); } catch (Exception $e) {}
75
76 // Also include generic/short titles
77 $shortTitles = ["Composer", "Ghostscript", "ngrok", "PEAR", "Perl", "Ruby", "Git", "Python", "MariaDB", "MySQL", "PostgreSQL", "Node.js"];
78 $allTitles = array_unique(array_merge($titles, $shortTitles));
79
80 // Build a single .reg file with all HKCU Console font settings.
81 // This replaces ~140 individual exec("reg add ...") calls, each of which
82 // would spawn a separate process and potentially flash a console window.
83 $regContent = "Windows Registry Editor Version 5.00\r\n";
84
85 // Global HKCU\Console defaults
86 $regContent .= "\r\n[HKEY_CURRENT_USER\\Console]\r\n";
87 $regContent .= '"FaceName"="' . $fontName . '"' . "\r\n";
88 $regContent .= '"FontFamily"=dword:00000036' . "\r\n";
89 $regContent .= '"CodePage"=dword:0000fde9' . "\r\n";
90
91 // Per-title settings
92 foreach ($allTitles as $title) {
93 if (empty($title)) continue;
94 $regContent .= "\r\n[HKEY_CURRENT_USER\\Console\\" . $title . "]\r\n";
95 $regContent .= '"FaceName"="' . $fontName . '"' . "\r\n";
96 $regContent .= '"FontFamily"=dword:00000036' . "\r\n";
97 $regContent .= '"FontSize"=dword:00100000' . "\r\n";
98 $regContent .= '"FontWeight"=dword:00000190' . "\r\n";
99 $regContent .= '"CodePage"=dword:0000fde9' . "\r\n";
100 $regContent .= '"ScreenBufferSize"=dword:0bb8006e' . "\r\n";
101 $regContent .= '"WindowSize"=dword:001e006e' . "\r\n";
102 }
103
104 // Register as a valid TrueType console font (HKCU only — no elevation needed)
105 $regContent .= "\r\n[HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Console\\TrueTypeFont]\r\n";
106 $regContent .= '"0"="' . $fontName . '"' . "\r\n";
107 $regContent .= '"00"="' . $fontName . '"' . "\r\n";
108 $regContent .= '"000"="' . $fontName . '"' . "\r\n";
109
110 // Write and import the .reg file in a single process — zero flashing windows
111 $tmpReg = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bearsampp_console_font.reg';
112
113 $bytes = file_put_contents($tmpReg, $regContent);
114 if ($bytes === false || $bytes === 0) {
115 return false;
116 }
117
118 $cmd = 'reg import "' . $tmpReg . '"';
119 $exitCode = 1;
120 try {
121 $wsh = new COM('WScript.Shell');
122 $exitCode = $wsh->Run($cmd, 0, true);
123 } catch (Throwable $e) {
124 $output = [];
125 exec($cmd, $output, $exitCode);
126 }
127
128 @unlink($tmpReg);
129
130 if ($exitCode !== 0) {
131 return false;
132 }
133
134 return true;
135 }
136
163
175 private static function getTabCmdSection()
176 {
177 global $bearsamppRoot, $bearsamppTools;
178
179 return self::getTab(
180 $bearsamppTools->getPowerShell()->getTabTitleDefault(),
181 null, // self::ICON_APP - Icon not supported in native PowerShell console
182 $bearsamppTools->getPowerShell()->getShell(),
184 ) . PHP_EOL;
185 }
186
198 private static function getTabPowerShellSection()
199 {
200 global $bearsamppRoot, $bearsamppTools;
201
202 $powerShellPath = Path::getPowerShellPath();
203 if ($powerShellPath !== false) {
204 return self::getTab(
205 $bearsamppTools->getPowerShell()->getTabTitlePowershell(),
206 null, // self::ICON_POWERSHELL - Icon not supported in native PowerShell console
207 $powerShellPath,
209 ) . PHP_EOL;
210 }
211
212 return "";
213 }
214
226 private static function getTabPearSection()
227 {
228 global $bearsamppBins, $bearsamppTools;
229
230 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getPhp()->getPearExe() . '&quot;');
231 if (!file_exists($bearsamppBins->getPhp()->getPearExe())) {
232 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPhp()->getPearExe() . ' not found');
233 }
234
235 return self::getTab(
236 $bearsamppTools->getPowerShell()->getTabTitlePear(),
237 null, // self::ICON_PEAR - Icon not supported in native PowerShell console
238 $shell,
239 Path::getModuleSymlinkPath($bearsamppBins->getPhp()) . '/pear'
240 ) . PHP_EOL;
241 }
242
254 private static function getTabMysqlSection()
255 {
256 global $bearsamppBins, $bearsamppTools;
257
258 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getMysql()->getCliExe() . '&quot; -u' .
259 $bearsamppBins->getMysql()->getRootUser() .
260 ($bearsamppBins->getMysql()->getRootPwd() ? ' -p' : ''));
261 if (!file_exists($bearsamppBins->getMysql()->getCliExe())) {
262 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMysql()->getCliExe() . ' not found');
263 }
264
265 return self::getTab(
266 $bearsamppTools->getPowerShell()->getTabTitleMysql(),
267 null, // self::ICON_DB - Icon not supported in native PowerShell console
268 $shell,
270 ) . PHP_EOL;
271 }
272
284 private static function getTabMariadbSection()
285 {
286 global $bearsamppBins, $bearsamppTools;
287
288 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getMariadb()->getCliExe() . '&quot; -u' .
289 $bearsamppBins->getMariadb()->getRootUser() .
290 ($bearsamppBins->getMariadb()->getRootPwd() ? ' -p' : ''));
291 if (!file_exists($bearsamppBins->getMariadb()->getCliExe())) {
292 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMariadb()->getCliExe() . ' not found');
293 }
294
295 return self::getTab(
296 $bearsamppTools->getPowerShell()->getTabTitleMariadb(),
297 null, // self::ICON_DB - Icon not supported in native PowerShell console
298 $shell,
300 ) . PHP_EOL;
301 }
302
314 private static function getTabPostgresqlSection()
315 {
316 global $bearsamppBins, $bearsamppTools;
317
318 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getPostgresql()->getCliExe() . '&quot;' .
319 ' -h 127.0.0.1' .
320 ' -p ' . $bearsamppBins->getPostgresql()->getPort() .
321 ' -U ' . $bearsamppBins->getPostgresql()->getRootUser() .
322 ' -d postgres');
323 if (!file_exists($bearsamppBins->getPostgresql()->getCliExe())) {
324 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPostgresql()->getCliExe() . ' not found');
325 }
326
327 return self::getTab(
328 $bearsamppTools->getPowerShell()->getTabTitlePostgresql(),
329 null, // self::ICON_DB - Icon not supported in native PowerShell console
330 $shell,
332 ) . PHP_EOL;
333 }
334
346 private static function getTabGitSection()
347 {
348 global $bearsamppRoot, $bearsamppTools;
349
350 $shell = $bearsamppTools->getPowerShell()->getShell();
351 if (!file_exists($bearsamppTools->getGit()->getExe())) {
352 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGit()->getExe() . ' not found');
353 }
354
355 return self::getTab(
356 $bearsamppTools->getPowerShell()->getTabTitleGit(),
357 null, // self::ICON_GIT - Icon not supported in native PowerShell console
358 $shell,
360 ) . PHP_EOL;
361 }
362
375 private static function getTabNodejsSection()
376 {
377 global $bearsamppRoot, $bearsamppBins, $bearsamppTools;
378
379 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getNodejs()->getLaunch() . '&quot;');
380 if (!file_exists($bearsamppBins->getNodejs()->getLaunch())) {
381 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getNodejs()->getLaunch() . ' not found');
382 }
383
384 return self::getTab(
385 $bearsamppTools->getPowerShell()->getTabTitleNodejs(),
386 null, // self::ICON_NODEJS - Icon not supported in native PowerShell console
387 $shell,
389 ) . PHP_EOL;
390 }
391
403 private static function getTabComposerSection()
404 {
405 global $bearsamppRoot, $bearsamppTools;
406
407 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getComposer()->getExe() . '&quot;');
408 if (!file_exists($bearsamppTools->getComposer()->getExe())) {
409 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getComposer()->getExe() . ' not found');
410 }
411
412 return self::getTab(
413 $bearsamppTools->getPowerShell()->getTabTitleComposer(),
414 null, // self::ICON_COMPOSER - Icon not supported in native PowerShell console
415 $shell,
417 ) . PHP_EOL;
418 }
419
431 private static function getTabPythonSection()
432 {
433 global $bearsamppRoot, $bearsamppTools;
434
435 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPython()->getExe() . '&quot;');
436 if (!file_exists($bearsamppTools->getPython()->getExe())) {
437 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPython()->getExe() . ' not found');
438 }
439
440 return self::getTab(
441 $bearsamppTools->getPowerShell()->getTabTitlePython(),
442 null, // self::ICON_PYTHON - Icon not supported in native PowerShell console
443 $shell,
445 ) . PHP_EOL;
446 }
447
459 private static function getTabRubySection()
460 {
461 global $bearsamppRoot, $bearsamppTools;
462
463 // Check if Ruby exists first
464 if (!file_exists($bearsamppTools->getRuby()->getExe())) {
465 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getRuby()->getExe() . ' not found');
466 } else {
467 // Use irb (Interactive Ruby) for an interactive shell
468 $rubyDir = dirname($bearsamppTools->getRuby()->getExe());
469 $irbExe = $rubyDir . '/irb.bat';
470
471 // Check if irb.bat exists, otherwise try irb.cmd or just use ruby with -i flag
472 if (file_exists($irbExe)) {
473 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $irbExe . '&quot;');
474 } elseif (file_exists($rubyDir . '/irb.cmd')) {
475 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb.cmd' . '&quot;');
476 } elseif (file_exists($rubyDir . '/irb')) {
477 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb' . '&quot;');
478 } else {
479 // Fallback to just opening a shell in the Ruby directory
480 $shell = $bearsamppTools->getPowerShell()->getShell();
481 }
482 }
483
484 return self::getTab(
485 $bearsamppTools->getPowerShell()->getTabTitleRuby(),
486 null, // self::ICON_RUBY - Icon not supported in native PowerShell console
487 $shell,
489 ) . PHP_EOL;
490 }
491
503 private static function getTabPerlSection()
504 {
505 global $bearsamppRoot, $bearsamppTools;
506
507 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPerl()->getExe() . '&quot;');
508 if (!file_exists($bearsamppTools->getPerl()->getExe())) {
509 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPerl()->getExe() . ' not found');
510 }
511
512 return self::getTab(
513 $bearsamppTools->getPowerShell()->getTabTitlePerl(),
514 null, // self::ICON_PERL - Icon not supported in native PowerShell console
515 $shell,
517 ) . PHP_EOL;
518 }
519
533 private static function getTabGhostscriptSection()
534 {
535 global $bearsamppRoot, $bearsamppTools;
536
537 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getGhostscript()->getExeConsole() . '&quot;');
538 if (!file_exists($bearsamppTools->getGhostscript()->getExeConsole())) {
539 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGhostscript()->getExeConsole() . ' not found');
540 }
541
542 return self::getTab(
543 $bearsamppTools->getPowerShell()->getTabTitleGhostscript(),
544 null, // self::ICON_GHOSTSCRIPT - Icon not supported in native PowerShell console
545 $shell,
547 ) . PHP_EOL;
548 }
549
563 private static function getTabNgrokSection()
564 {
565 global $bearsamppRoot, $bearsamppTools;
566
567 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getNgrok()->getExe() . '&quot;');
568 if (!file_exists($bearsamppTools->getNgrok()->getExe())) {
569 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getNgrok()->getExe() . ' not found');
570 }
571
572 return self::getTab(
573 $bearsamppTools->getPowerShell()->getTabTitleNgrok(),
574 null, // self::ICON_NGROK - Icon not supported in native PowerShell console
575 $shell,
577 ) . PHP_EOL;
578 }
579
597 private static function getTab($title, $icon, $shell, $initDir)
598 {
599 global $bearsamppCore;
600 // Icon parameter is ignored as native PowerShell console does not support custom icons
601 // Return tab information as a formatted string
602 return "Title: $title | Shell: $shell | InitDir: $initDir";
603 }
604}
global $bearsamppBins
global $bearsamppRoot
global $bearsamppCore
static getRootPath($aetrayPath=false)
static getWwwPath($aetrayPath=false)
static getModuleSymlinkPath($module)
static getPowerShellPath()
static getTab($title, $icon, $shell, $initDir)
static getTabGhostscriptSection()