-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
202 lines (170 loc) · 5.77 KB
/
extension.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const St = imports.gi.St;
const Main = imports.ui.main;
const Search = imports.ui.search;
const SearchDisplay = imports.ui.searchDisplay;
const IconGrid = imports.ui.iconGrid;
const GLib = imports.gi.GLib;
const MAX_SEARCH_RESULTS_ROWS = 1;
const ICON_SIZE = 81;
let calcProvider = "";
let octal = /(^|\s|[^0-9a-fA-Fxb\.]+)0([0-7]+)/g;
let binary = /(^|\s|[^0-9a-fA-Fxb]+)0b([0-1]+)/g;
let hex = /(^|\s|[^0-9a-fA-Fxb]+)0x([0-9a-fA-F]+)/g;
let radians = /r(sin|cos|tan)\(/g;
let radians2 = /ra(sin|cos|tan)\(/g;
let changeBase = /in (hex|octal|binary)$/i;
let bases = {
"hex" : 16,
"octal" : 8,
"binary" : 2
};
let prefixes = {
"16" : "0x",
"10" : "",
"8" : "0",
"2" : "0b"
};
function CalcResult(result) {
this._init(result);
}
CalcResult.prototype = {
_init: function(resultMeta) {
this.actor = new St.Bin({ style_class: 'contact',
reactive: true,
track_hover: true });
let content = new St.BoxLayout( { style_class: 'contact-content',
vertical: false });
this.actor.set_child(content);
let icon = new St.Icon({ icon_size: ICON_SIZE,
icon_name: 'accessories-calculator',
style_class: 'contact-icon' });
content.add(icon, { x_fill: true,
y_fill: false,
x_align: St.Align.START,
y_align: St.Align.MIDDLE });
let result = new St.BoxLayout({ style_class: 'contact-details',
vertical: true });
content.add(result, { x_fill: true, x_align: St.Align.START });
let exprLabel = new St.Label({ text: resultMeta.expr,
style_class: 'result-expression' });
let resultLabel = new St.Label({ text: resultMeta.result,
style_class: 'result-result' });
result.add(exprLabel, { x_fill: false, x_align: St.Align.START });
result.add(resultLabel, { x_fill: false, x_align: St.Align.START });
result.set_width(400);
}
};
function CalcProvider() {
this._init.apply(this, arguments);
}
CalcProvider.prototype = {
__proto__: Search.SearchProvider.prototype,
_init: function(title) {
Search.SearchProvider.prototype._init.call(this, title);
},
_convertTable : ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"],
_toBase: function(number, base) {
number = Math.floor(number);
var string = "";
var term = 1;
while(term <= number) {
term *= base;
}
term /= base;
while(term > 1) {
string += this._convertTable[Math.floor(number / term)];
number = number % term;
term /= base;
}
string += this._convertTable[Math.floor(number)];
if(string == "") {
string = "0";
}
return string;
},
_validExpression: function(expression) {
return /([0-9+\-*\/^!]|'pi')+/i.test(expression);
},
getInitialResultSet: function(terms) {
// Join everything together, then replace commas with periods to support
// Using a comma as a decimal point
let expr = terms.join(" ").replace(/,/g, ".");
let finalBase = 10;
if (this._validExpression(expr)) {
expr = expr.replace(/'pi'/gi, "\u03C0");
expr = expr.replace(octal, "$1$2\u2088");
expr = expr.replace(hex, "$1$2\u2081\u2086");
expr = expr.replace(binary, "$1$2\u2082");
expr = expr.replace(radians, "$1((180/\u03C0) *");
expr = expr.replace(radians2, "(\u03C0/180) * a$1(");
if(changeBase.test(expr)) {
finalBase = bases[changeBase.exec(expr)[1]];
expr = expr.replace(changeBase, "");
}
try {
let [success, out, err, error] = GLib.spawn_sync(null, ["gcalctool", "-s", expr], null, 4, null)
if(error == 0) {
let result = out.toString();
if(finalBase != 10) {
let neg = false;
// \u2212 is a minus sign. Since it's unicode javascript doesn't recognize
// the result as a negative number.
if(result[0] == "\u2212") {
result = result.substring(1);
neg = true;
}
result = this._toBase(result, finalBase);
result = prefixes[finalBase] + result;
if(neg) {
result = "\u2212" + result;
}
}
this._lastResult = result;
this.searchSystem.pushResults(this,
[{'expr': expr, 'result': result}]);
return [{'expr': expr, 'result': result}];
}
} catch(exp) {
}
}
this.searchSystem.pushResults(this, []);
return [];
},
getSubsearchResultSet: function(prevResults, terms) {
return this.getInitialResultSet(terms);
},
getResultMetas: function(result, callback) {
let metas = [];
for(let i = 0; i < result.length; i++) {
metas.push({'id' : i, 'result' : result[i].result, 'expr' : result[i].expr});
}
callback(metas);
return metas;
},
createResultActor: function(resultMeta, terms) {
let result = new CalcResult(resultMeta);
return result.actor;
},
createResultContainerActor: function() {
let grid = new IconGrid.IconGrid({ rowLimit: MAX_SEARCH_RESULTS_ROWS,
xAlign: St.Align.START });
grid.actor.style_class = 'contact-grid';
let actor = new SearchDisplay.GridSearchResults(this, grid);
return actor;
},
activateResult: function(resultId) {
if(this._lastResult) {
St.Clipboard.get_default().set_text(this._lastResult.replace("\n",""));
}
return true;
}
}
function init() {
calcProvider = new CalcProvider('CALCULATOR');
}
function enable() {
Main.overview.addSearchProvider(calcProvider);
}
function disable() {
Main.overview.removeSearchProvider(calcProvider);
}