-
Notifications
You must be signed in to change notification settings - Fork 1
/
pika.js
113 lines (91 loc) · 2.72 KB
/
pika.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
// INFO {{{
var INFO = xml`
<plugin name='Pika-!'
version='0.5'
href='https://github.com/Jagua/vimperator-plugins-sample/blob/master/pika.js'
summary='calculate the distance from you to a lightning.'
lang='en-US'
xmlns='http://vimperator.org/namespaces/liberator'>
<author homepage='https://github.com/Jagua'>Jagua</author>
<license>New BSD License</license>
<project name='Vimperator' minVersion='3.8'/>
<item>
<tags>:pika</tags>
<spec>:pika</spec>
<description>
<p>
calculate the distance from you to a lightning.
</p>
</description>
</item>
</plugin>`;
// }}}
(function () {
const api = {
_shineTime: 0,
_soundTime: 0,
set soundTime (arg) this._soundTime = arg,
set shineTime (arg) this._shineTime = arg,
get soundTime () this._soundTime,
get shineTime () this._shineTime,
get calc () {
let _dt = (api.soundTime - api.shineTime) / 1000; // (sec)
let distance = Math.round(0.34 * _dt * 100) / 100; // (km)
let dt = Math.round(_dt * 100) / 100; // (sec)
return [dt, distance];
},
start: function () {
setTimeout(function() {
commandline.input('Seeing the flash of lightning, press <Enter>!',
function(arg) {
liberator.execute(':thutime lightning');
commandline.close();
}
);
}, 0);
},
seeLightning: function () {
setTimeout(function() {
api.shineTime = (new Date()).getTime();
commandline.input('Hearing the sound of thunder, press <Enter>!',
function(arg) {
liberator.execute(':thutime thunder');
commandline.close();
}
);
}, 0);
},
hearThunder: function () {
api.soundTime = (new Date()).getTime();
api.echoResult();
},
echoResult: function () {
let [t, d] = api.calc;
liberator.echo(`Distance: ${d} km (= 0.34 (km/s) x ${t} (s))`);
},
};
liberator._thutime = __context__.api = api;
let subCommands = [
{name: 'start', actionName: 'start'},
{name: 'lightning', actionName: 'seeLightning'},
{name: 'thunder', actionName: 'hearThunder'},
];
commands.addUserCommand(['thutime'], 'display the distance to the lightning.',
function (args) {
api.start();
},
{
subCommands:
subCommands.map(cmd =>
new Command(
[cmd.name],
cmd.description || cmd.name,
cmd.actionName ? function (args) api[cmd.actionName](args) : cmd.action,
{completer: cmd.completer, literal: 0, bang: false}
)
)
},
true
);
})();
// vim: set et fdm=marker ft=javascript si sts=2 sw=2 ts=2 :