11'use strict' ;
2-
32// Import parts of electron to use
4- const { app, BrowserWindow, Menu } = require ( 'electron' ) ;
3+ const { app, BrowserWindow, ipcMain } = require ( 'electron' ) ;
54const Store = require ( 'electron-store' ) ;
6- const storage = new Store ( ) ;
5+ const os = require ( 'os' ) ;
76const path = require ( 'path' ) ;
87const url = require ( 'url' ) ;
8+ const pty = require ( 'node-pty' ) ;
9+
10+ // Determines the type of shell needed for the terminal based on the user's platform
11+ const shell = os . platform === 'win32' ? 'powershell.exe' : 'zsh' ;
912
1013// Keep a global reference of the window object, if you don't, the window will
1114// be closed automatically when the JavaScript object is garbage collected.
@@ -18,9 +21,12 @@ if (process.env.NODE_ENV !== undefined && process.env.NODE_ENV === 'development'
1821 dev = true ;
1922}
2023
24+ const storage = new Store ( ) ;
25+
2126function createWindow ( ) {
2227 const getWinSettings = ( ) => {
23- const defaultBounds = [ 1280 , 768 ] ;
28+ //Gets and stores the previous window's size upon close and restores them
29+ const defaultBounds = [ 1280 , 1024 ] ;
2430 const size = storage . get ( 'win-size' ) ;
2531
2632 if ( size ) return size ;
@@ -68,15 +74,30 @@ function createWindow() {
6874 slashes : true ,
6975 } ) ;
7076 }
71- // if (is.development) {
72- // mainWindow.webContents.openDevTools({ mode: 'detach' });
73- // mainWindow.loadURL('http://localhost:3000');
74- // } else {
75- // mainWindow.loadURL(`file://${path.join(__dirname, 'dist', 'index.html')}`);
76- // }
7777
7878 mainWindow . loadURL ( indexPath ) ;
7979
80+ // For the Terminal
81+ const ptyProcess = pty . spawn ( shell , [ ] , {
82+ name : 'xterm-color' ,
83+ cols : 60 ,
84+ rows : 80 ,
85+ cwd : process . env . HOME ,
86+ env : process . env ,
87+ } ) ;
88+
89+ // We send incoming data to the Terminal
90+ ptyProcess . on ( 'data' , ( data ) => {
91+ mainWindow . webContents . send ( 'terminal.sentData' , data ) ;
92+ console . log ( 'data sent from main' , data ) ;
93+ } ) ;
94+ // in the main process, when data is received in the terminal,
95+ // main process will write and add to ptyProcess
96+ ipcMain . on ( 'terminal.toTerm' , ( event , data ) => {
97+ ptyProcess . write ( data ) ;
98+ console . log ( data , `being written in ptyMain: ${ data } ` ) ;
99+ } ) ;
100+
80101 var splash = new BrowserWindow ( {
81102 width : 500 ,
82103 height : 300 ,
@@ -91,10 +112,10 @@ function createWindow() {
91112 mainWindow . once ( 'ready-to-show' , ( ) => {
92113 splash . close ( ) ;
93114 // uncomment out to maximise app on load
94- mainWindow . maximize ( ) ;
115+ // mainWindow.maximize();
95116 mainWindow . show ( ) ;
96117 mainWindow . focus ( ) ;
97- mainWindow . center ( ) ;
118+ // mainWindow.center();
98119
99120 // Open the DevTools automatically if developing
100121 if ( dev ) {
@@ -105,11 +126,16 @@ function createWindow() {
105126 }
106127 } ) ;
107128
129+ mainWindow . on ( 'resize' , ( ) => saveBounds ( mainWindow . getSize ( ) ) ) ;
108130 // Emitted when the window is closed.
109131 mainWindow . on ( 'closed' , function ( ) {
110132 // Dereference the window object, usually you would store windows
111133 // in an array if your app supports multi windows, this is the time
112134 // when you should delete the corresponding element.
135+ // mainWindow.webContents.send('terminal.close', () => {
136+ // console.log('SENT CLOSE');
137+ // });
138+ ptyProcess . kill ( ) ;
113139 mainWindow = null ;
114140 } ) ;
115141}
0 commit comments