-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathextension.js
173 lines (154 loc) · 5.69 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
var vscode = require('vscode');
var renderTableInOutputChannel = require('./view');
var fzCalculator = require('filesize-calculator');
var window = vscode.window;
var workspace = vscode.workspace;
var cachedLocation = 'left';
var statusBarItem, oc, info, config, isShowingDetailedInfo;
function updateConfig() {
var configuration = workspace.getConfiguration('filesize');
config = {
useDecimal: configuration.get('useDecimal'),
use24HourFormat: configuration.get('use24HourFormat'),
showGzip: configuration.get('showGzip'),
showBrotli: configuration.get('showBrotli'),
showGzipInStatusBar: configuration.get('showGzipInStatusBar'),
showBrotliInStatusBar: configuration.get('showBrotliInStatusBar'),
showRawInBytes: configuration.get('showRawInBytes'),
displayInfoOnTheRightSideOfStatusBar: configuration.get('displayInfoOnTheRightSideOfStatusBar')
};
updateStatusBarItem();
return config;
}
function showStatusBarItem(newInfo) {
info = fzCalculator.addPrettySize(newInfo, config);
if (info && info.prettySize) {
if (config.showGzipInStatusBar || config.showBrotliInStatusBar) {
statusBarItem.text = `Raw: ${info.prettySize}`;
} else {
statusBarItem.text = info.prettySize;
}
if (config.showGzipInStatusBar) {
info = fzCalculator.addGzipSize(info, config);
statusBarItem.text += ` | Gz: ${info.gzipSize}`;
}
if (config.showBrotliInStatusBar) {
info = fzCalculator.addBrotliSize(info, config);
statusBarItem.text += ` | Br: ${info.brotliSize}`;
}
statusBarItem.show();
}
}
function hideStatusBarItem() {
hideDetailedInfo();
statusBarItem.text = '';
statusBarItem.hide();
}
// Update simple info in the status bar
function updateStatusBarItem() {
// Check where to display the status bar
var location = config.displayInfoOnTheRightSideOfStatusBar ? 'right' : 'left';
// Set up statusBarItem
if (cachedLocation !== location) {
cachedLocation = location;
if (location === 'right') {
statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Right, 1);
} else {
statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Left, 1);
}
statusBarItem.command = 'extension.toggleFilesizeInfo';
statusBarItem.tooltip = 'Current file size - Click to toggle more info';
}
try {
var currentEditor = window.activeTextEditor.document;
if (currentEditor && currentEditor.uri.scheme === 'file') {
hideDetailedInfo();
showStatusBarItem(fzCalculator.loadFileInfoSync(currentEditor.fileName));
} else {
if (currentEditor.uri.scheme !== 'output') hideStatusBarItem();
}
} catch (e) {
hideStatusBarItem();
}
}
// Show detailed filesize info in the OC
function showDetailedInfo() {
if (info && info.prettySize) {
info = config.showGzip ? fzCalculator.addGzipSize(info, config) : info;
info = config.showBrotli ? fzCalculator.addBrotliSize(info, config) : info;
info = fzCalculator.addMimeTypeInfo(info);
info = fzCalculator.addPrettyDateInfo(info, config);
const table = [];
if (info.prettySize) table.push({ header: 'Size', content: info.prettySize });
if (info.size && config.showRawInBytes) table.push({ header: 'Raw size', content: info.size + ' bytes' });
if (info.gzipSize) table.push({ header: 'Gzipped', content: info.gzipSize });
if (info.brotliSize) table.push({ header: 'Brotli', content: info.brotliSize });
if (info.mimeType) table.push({ header: 'Mime type', content: info.mimeType });
if (info.prettyDateCreated) table.push({ header: 'Created', content: info.prettyDateCreated });
if (info.prettyDateChanged) table.push({ header: 'Changed', content: info.prettyDateChanged });
renderTableInOutputChannel(oc, info.absolutePath, table);
} else {
oc.clear();
oc.appendLine('No file information available for this context!');
}
oc.show(true);
isShowingDetailedInfo = true;
}
function hideDetailedInfo() {
oc.hide();
isShowingDetailedInfo = false;
}
function toggleDetailedInfo() {
if (isShowingDetailedInfo) {
hideDetailedInfo();
} else {
showDetailedInfo();
}
}
// Called when VS Code activates the extension
function activate(context) {
console.log('filesize is active');
// Set up statusBarItem
statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Left, 1);
statusBarItem.command = 'extension.toggleFilesizeInfo';
statusBarItem.tooltip = 'Current file size - Click to toggle more info';
// Global OutputChannel to be used for the extension detailed info
oc = window.createOutputChannel('filesize');
// Update handlers
var onSave = workspace.onDidSaveTextDocument(updateStatusBarItem);
var onActiveEditorChanged = window.onDidChangeActiveTextEditor(updateStatusBarItem);
var onChangeConfig = workspace.onDidChangeConfiguration(updateConfig);
// Show detailed info through custom command
var command = vscode.commands.registerCommand('extension.toggleFilesizeInfo', toggleDetailedInfo);
// Register disposables that get disposed when deactivating
context.subscriptions.push(onSave);
context.subscriptions.push(onChangeConfig);
context.subscriptions.push(onActiveEditorChanged);
context.subscriptions.push(command);
// Set default config
updateConfig();
}
// Called when VS Code deactivates the extension
function deactivate() {
try {
if (oc) {
oc.clear();
oc.dispose();
}
if (statusBarItem) {
statusBarItem.hide();
statusBarItem.dispose();
}
oc = null;
statusBarItem = null;
config = null;
info = null;
} catch(e) {
console.log('Error disposing filesize disposable when deactivating');
}
return undefined;
}
module.exports = {
activate: activate,
deactivate: deactivate
};