-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
160 lines (142 loc) · 4.05 KB
/
main.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
const { app, BrowserWindow, Menu, dialog } = require('electron');
// const ipc = require('electron').ipcRenderer;
const fs = require('fs'); // Load the File System to execute our common tasks (CRUD)
let win;
var entryText = "";
var comparisons = 0;
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function sortEntries(){
// TODO: Connect to a button
// TODO: Implement the whole function
// Split entries by newline
let entries = entryText.split('\n')
// Mergesort them in helper function
let sortedEntries = mergeSort(entries)
// TODO: Render popup list of results
console.log(sortedEntries);
console.log(comparisons);
}
function mergeSort(entries){
// Helper method for merge-sorting an array of entries
if(entries.length <= 1){
return entries;
}
// In order to divide the array in half, we need to figure out the middle
const middle = Math.floor(entries.length / 2);
// This is where we will be dividing the array into left and right
const left = entries.slice(0, middle);
const right = entries.slice(middle);
// Using recursion to combine the left and right
return merge(
mergeSort(left), mergeSort(right)
);
}
function merge(left, right){
// Merge two sorted lists of entries
let resultArray = [], leftIndex = 0, rightIndex = 0;
// We will concatenate values into the resultArray in order
while (leftIndex < left.length && rightIndex < right.length) {
// if (left[leftIndex] < right[rightIndex]) {
if (compare(left[leftIndex], right[rightIndex])) {
resultArray.push(left[leftIndex]);
leftIndex++; // move left array cursor
} else {
resultArray.push(right[rightIndex]);
rightIndex++; // move right array cursor
}
}
// We need to concat here because there will be one element remaining
// from either left OR the right
return resultArray
.concat(left.slice(leftIndex))
.concat(right.slice(rightIndex));
}
function compare(entry1, entry2){
// Determine if entry1 is greater than entry2 by asking the user to decide
// TODO: Instead of popup, modify window
comparisons++;
const options = {
type: 'question',
buttons: [entry2, entry1],
message: 'Choice',
detail: 'Which option do you prefer?'
}
const response = dialog.showMessageBoxSync(options);
return (response == 1);
}
function createWindow(){
// Create Menu
var menu = Menu.buildFromTemplate([
{
label: 'Menu',
submenu: [
{
label: 'Exit',
click() {
app.quit()
}
}
]
},
{
label: 'Upload',
submenu: [
{
label: 'From File',
// Select a file, then copy down the data
click() {
dialog.showOpenDialog({properties: ['openFile','multiSelections']}, (filePaths) => {
(fs.readFile(filePaths[0], 'utf-8', (err, data) => {
if(err){
alert("An error ocurred reading the file :" + err.message);
return;
}
// Change how to handle the file content
console.log("The file content is : " + data);
entryText = data;
}))})}
},
{
label: 'Manual Entry (WIP)'
// TODO: Open a window with a textbox for manual entry
}
]
},
{
label: 'Sort',
submenu: [
{
label: 'Begin Sorting',
click() {
sortEntries()
}
}
]
}
])
Menu.setApplicationMenu(menu);
// Create browser window
win = new BrowserWindow({
width: 500,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
// Load index.html
win.loadFile('views/index.html');
// Unload window when closed
win.on('closed', () => {
win = null;
});
}
// Run create window function
app.on('ready', createWindow);
// Quit when all windows are closed
app.on('window-all-closed', () => {
if (process.platform !== 'darwin'){
app.quit();
}
});