Bearsampp 2026.5.5
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 $output = [];
120 $exitCode = 0;
121 exec($cmd, $output, $exitCode);
122
123 @unlink($tmpReg);
124
125 if ($exitCode !== 0) {
126 return false;
127 }
128
129 return true;
130 }
131
158
170 private static function getTabCmdSection()
171 {
172 global $bearsamppRoot, $bearsamppTools;
173
174 return self::getTab(
175 $bearsamppTools->getPowerShell()->getTabTitleDefault(),
176 null, // self::ICON_APP - Icon not supported in native PowerShell console
177 $bearsamppTools->getPowerShell()->getShell(),
178 $bearsamppRoot->getRootPath()
179 ) . PHP_EOL;
180 }
181
193 private static function getTabPowerShellSection()
194 {
195 global $bearsamppRoot, $bearsamppTools;
196
197 $powerShellPath = Util::getPowerShellPath();
198 if ($powerShellPath !== false) {
199 return self::getTab(
200 $bearsamppTools->getPowerShell()->getTabTitlePowershell(),
201 null, // self::ICON_POWERSHELL - Icon not supported in native PowerShell console
202 $powerShellPath,
203 $bearsamppRoot->getRootPath()
204 ) . PHP_EOL;
205 }
206
207 return "";
208 }
209
221 private static function getTabPearSection()
222 {
223 global $bearsamppBins, $bearsamppTools;
224
225 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getPhp()->getPearExe() . '&quot;');
226 if (!file_exists($bearsamppBins->getPhp()->getPearExe())) {
227 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPhp()->getPearExe() . ' not found');
228 }
229
230 return self::getTab(
231 $bearsamppTools->getPowerShell()->getTabTitlePear(),
232 null, // self::ICON_PEAR - Icon not supported in native PowerShell console
233 $shell,
234 $bearsamppBins->getPhp()->getSymlinkPath() . '/pear'
235 ) . PHP_EOL;
236 }
237
249 private static function getTabMysqlSection()
250 {
251 global $bearsamppBins, $bearsamppTools;
252
253 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getMysql()->getCliExe() . '&quot; -u' .
254 $bearsamppBins->getMysql()->getRootUser() .
255 ($bearsamppBins->getMysql()->getRootPwd() ? ' -p' : ''));
256 if (!file_exists($bearsamppBins->getMysql()->getCliExe())) {
257 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMysql()->getCliExe() . ' not found');
258 }
259
260 return self::getTab(
261 $bearsamppTools->getPowerShell()->getTabTitleMysql(),
262 null, // self::ICON_DB - Icon not supported in native PowerShell console
263 $shell,
264 $bearsamppBins->getMysql()->getSymlinkPath()
265 ) . PHP_EOL;
266 }
267
279 private static function getTabMariadbSection()
280 {
281 global $bearsamppBins, $bearsamppTools;
282
283 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getMariadb()->getCliExe() . '&quot; -u' .
284 $bearsamppBins->getMariadb()->getRootUser() .
285 ($bearsamppBins->getMariadb()->getRootPwd() ? ' -p' : ''));
286 if (!file_exists($bearsamppBins->getMariadb()->getCliExe())) {
287 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMariadb()->getCliExe() . ' not found');
288 }
289
290 return self::getTab(
291 $bearsamppTools->getPowerShell()->getTabTitleMariadb(),
292 null, // self::ICON_DB - Icon not supported in native PowerShell console
293 $shell,
294 $bearsamppBins->getMariadb()->getSymlinkPath()
295 ) . PHP_EOL;
296 }
297
309 private static function getTabPostgresqlSection()
310 {
311 global $bearsamppBins, $bearsamppTools;
312
313 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getPostgresql()->getCliExe() . '&quot;' .
314 ' -h 127.0.0.1' .
315 ' -p ' . $bearsamppBins->getPostgresql()->getPort() .
316 ' -U ' . $bearsamppBins->getPostgresql()->getRootUser() .
317 ' -d postgres');
318 if (!file_exists($bearsamppBins->getPostgresql()->getCliExe())) {
319 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPostgresql()->getCliExe() . ' not found');
320 }
321
322 return self::getTab(
323 $bearsamppTools->getPowerShell()->getTabTitlePostgresql(),
324 null, // self::ICON_DB - Icon not supported in native PowerShell console
325 $shell,
326 $bearsamppBins->getPostgresql()->getSymlinkPath()
327 ) . PHP_EOL;
328 }
329
341 private static function getTabGitSection()
342 {
343 global $bearsamppRoot, $bearsamppTools;
344
345 $shell = $bearsamppTools->getPowerShell()->getShell();
346 if (!file_exists($bearsamppTools->getGit()->getExe())) {
347 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGit()->getExe() . ' not found');
348 }
349
350 return self::getTab(
351 $bearsamppTools->getPowerShell()->getTabTitleGit(),
352 null, // self::ICON_GIT - Icon not supported in native PowerShell console
353 $shell,
354 $bearsamppRoot->getWwwPath()
355 ) . PHP_EOL;
356 }
357
370 private static function getTabNodejsSection()
371 {
372 global $bearsamppRoot, $bearsamppBins, $bearsamppTools;
373
374 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getNodejs()->getLaunch() . '&quot;');
375 if (!file_exists($bearsamppBins->getNodejs()->getLaunch())) {
376 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getNodejs()->getLaunch() . ' not found');
377 }
378
379 return self::getTab(
380 $bearsamppTools->getPowerShell()->getTabTitleNodejs(),
381 null, // self::ICON_NODEJS - Icon not supported in native PowerShell console
382 $shell,
383 $bearsamppRoot->getWwwPath()
384 ) . PHP_EOL;
385 }
386
398 private static function getTabComposerSection()
399 {
400 global $bearsamppRoot, $bearsamppTools;
401
402 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getComposer()->getExe() . '&quot;');
403 if (!file_exists($bearsamppTools->getComposer()->getExe())) {
404 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getComposer()->getExe() . ' not found');
405 }
406
407 return self::getTab(
408 $bearsamppTools->getPowerShell()->getTabTitleComposer(),
409 null, // self::ICON_COMPOSER - Icon not supported in native PowerShell console
410 $shell,
411 $bearsamppRoot->getWwwPath()
412 ) . PHP_EOL;
413 }
414
426 private static function getTabPythonSection()
427 {
428 global $bearsamppRoot, $bearsamppTools;
429
430 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPython()->getExe() . '&quot;');
431 if (!file_exists($bearsamppTools->getPython()->getExe())) {
432 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPython()->getExe() . ' not found');
433 }
434
435 return self::getTab(
436 $bearsamppTools->getPowerShell()->getTabTitlePython(),
437 null, // self::ICON_PYTHON - Icon not supported in native PowerShell console
438 $shell,
439 $bearsamppRoot->getWwwPath()
440 ) . PHP_EOL;
441 }
442
454 private static function getTabRubySection()
455 {
456 global $bearsamppRoot, $bearsamppTools;
457
458 // Check if Ruby exists first
459 if (!file_exists($bearsamppTools->getRuby()->getExe())) {
460 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getRuby()->getExe() . ' not found');
461 } else {
462 // Use irb (Interactive Ruby) for an interactive shell
463 $rubyDir = dirname($bearsamppTools->getRuby()->getExe());
464 $irbExe = $rubyDir . '/irb.bat';
465
466 // Check if irb.bat exists, otherwise try irb.cmd or just use ruby with -i flag
467 if (file_exists($irbExe)) {
468 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $irbExe . '&quot;');
469 } elseif (file_exists($rubyDir . '/irb.cmd')) {
470 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb.cmd' . '&quot;');
471 } elseif (file_exists($rubyDir . '/irb')) {
472 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb' . '&quot;');
473 } else {
474 // Fallback to just opening a shell in the Ruby directory
475 $shell = $bearsamppTools->getPowerShell()->getShell();
476 }
477 }
478
479 return self::getTab(
480 $bearsamppTools->getPowerShell()->getTabTitleRuby(),
481 null, // self::ICON_RUBY - Icon not supported in native PowerShell console
482 $shell,
483 $bearsamppRoot->getWwwPath()
484 ) . PHP_EOL;
485 }
486
498 private static function getTabPerlSection()
499 {
500 global $bearsamppRoot, $bearsamppTools;
501
502 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPerl()->getExe() . '&quot;');
503 if (!file_exists($bearsamppTools->getPerl()->getExe())) {
504 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPerl()->getExe() . ' not found');
505 }
506
507 return self::getTab(
508 $bearsamppTools->getPowerShell()->getTabTitlePerl(),
509 null, // self::ICON_PERL - Icon not supported in native PowerShell console
510 $shell,
511 $bearsamppRoot->getWwwPath()
512 ) . PHP_EOL;
513 }
514
528 private static function getTabGhostscriptSection()
529 {
530 global $bearsamppRoot, $bearsamppTools;
531
532 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getGhostscript()->getExeConsole() . '&quot;');
533 if (!file_exists($bearsamppTools->getGhostscript()->getExeConsole())) {
534 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGhostscript()->getExeConsole() . ' not found');
535 }
536
537 return self::getTab(
538 $bearsamppTools->getPowerShell()->getTabTitleGhostscript(),
539 null, // self::ICON_GHOSTSCRIPT - Icon not supported in native PowerShell console
540 $shell,
541 $bearsamppRoot->getWwwPath()
542 ) . PHP_EOL;
543 }
544
558 private static function getTabNgrokSection()
559 {
560 global $bearsamppRoot, $bearsamppTools;
561
562 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getNgrok()->getExe() . '&quot;');
563 if (!file_exists($bearsamppTools->getNgrok()->getExe())) {
564 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getNgrok()->getExe() . ' not found');
565 }
566
567 return self::getTab(
568 $bearsamppTools->getPowerShell()->getTabTitleNgrok(),
569 null, // self::ICON_NGROK - Icon not supported in native PowerShell console
570 $shell,
571 $bearsamppRoot->getWwwPath()
572 ) . PHP_EOL;
573 }
574
592 private static function getTab($title, $icon, $shell, $initDir)
593 {
594 global $bearsamppCore;
595 // Icon parameter is ignored as native PowerShell console does not support custom icons
596 // Return tab information as a formatted string
597 return "Title: $title | Shell: $shell | InitDir: $initDir";
598 }
599}
global $bearsamppBins
global $bearsamppRoot
global $bearsamppCore
static getTab($title, $icon, $shell, $initDir)
static getTabGhostscriptSection()
static getPowerShellPath()