Repository navigation
Expand file tree
/
Copy pathipAddressStatusBarItem.js
More file actions
79 lines (60 loc) · 2.4 KB
/
Copy pathipAddressStatusBarItem.js
File metadata and controls
79 lines (60 loc) · 2.4 KB
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
'use strict';
const REFRESH_INTERVAL = 5000;
const Commands = require('./commands');
const freshPublicIP = require('./freshPublicIP')({ autoRefresh: false });
const NetworkInterfaceUtil = require('./networkInterfaceUtil');
const os = require('os');
const vscode = require('vscode');
const { window } = vscode;
class IPAddressStatusBarItem {
constructor() {
this._selectedInterfaceName = 0;
this._statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Right);
this._statusBarItem.command = Commands.SHOW_NEXT_IP_ADDRESS;
this._statusBarItem.show();
this.refresh();
this._refreshInterval = setInterval(() => this.refresh(), REFRESH_INTERVAL);
}
dispose() {
this._statusBarItem.dispose();
clearInterval(this._refreshInterval);
}
refresh() {
this.fetchNetworkInterfaces();
this.refreshUI();
}
getNetworkInterfaces() {
const networkInterfaces = os.networkInterfaces();
networkInterfaces['public'] = [{
address: freshPublicIP.address(),
// address: Promise.resolve('123.45.67.89'),
family : 'IPv4',
type : 'fresh'
}];
return NetworkInterfaceUtil.flattenNetworkInterfaces(networkInterfaces);
}
fetchNetworkInterfaces() {
this._networkInterfaces = this.getNetworkInterfaces();
}
refreshUI() {
let networkInterface;
if (this._selectedInterfaceName) {
networkInterface = this._networkInterfaces.get(this._selectedInterfaceName);
}
networkInterface || (networkInterface = this._networkInterfaces.first());
if (networkInterface) {
const addressesOfSameInterface = NetworkInterfaceUtil.getAddresses(this._networkInterfaces, networkInterface.interfaceName).sort(addr => addr.family);
this._statusBarItem.text = `$(home) ${ networkInterface.address }`;
this._statusBarItem.tooltip = `${ networkInterface.interfaceName }\n${ (networkInterface.mac || '').toUpperCase() }\n\n${ addressesOfSameInterface.valueSeq().map(a => `(${ a.family }) ${ a.address }`).toJS().join('\n') }`;
} else {
this._statusBarItem.text = `$(unplug)`;
this._statusBarItem.tooltip = null;
}
}
nextAddress() {
this.fetchNetworkInterfaces();
this._selectedInterfaceName = NetworkInterfaceUtil.getNextInterfaceName(this._networkInterfaces, this._selectedInterfaceName);
this.refreshUI();
}
}
module.exports = IPAddressStatusBarItem;