aboutsummaryrefslogtreecommitdiffstats
path: root/every.js
blob: 05c57629b0f58d712519a3a0f2c94441836f3a8e (plain)
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
// ==VimperatorPlugin==
// @name           every.js
// @description    to run a specified command every time at specified interval.
// @description-ja 指定のコマンドを指定の間隔で実行する。
// @license        Creative Commons 2.1 (Attribution + Share Alike)
// @version        1.1
// @author         anekos (anekos@snca.net)
// ==/VimperatorPlugin==
//
// Usage:
//    :[INTERVAL]every <COMMAND>
//      run <COMMAND> every time at [INTERVAL] sec.
//
//    :[INTERVAL]delay <COMMAND>
//      run <COMMAND> after [INTERVAL] sec.
//
//    :every! <PROCESS-ID>
//      kill specified process.
//
//    The default value of [INTERVAL] is 1 sec.
//    While Vimperator's focus is at command-line,
//    these commands does not run.
//
// Usage-ja:
//    :[INTERVAL]every <COMMAND>
//      [INTERVAL] 間隔で <COMMAND> を走らせる。
//
//    :[INTERVAL]delay <COMMAND>
//      [INTERVAL] 秒後に <COMMAND> を走らせる。
//
//    :every! <PROCESS-ID>
//      指定のプロセスを殺す。
//
//    [INTERVAL] のデフォルトは 1秒。
//    コマンドラインにいるときには、実行されないようになっている。
//
// Links:
//    http://d.hatena.ne.jp/nokturnalmortum/20081102#1225552718


(function () {

  let every = liberator.plugins.every;
  if (every) {
    kill('*');
  } else {
    liberator.plugins.every = every = {ps: []};
  }

  function run (command, interval) {
    let fun = function () {
      if (liberator.mode != liberator.modules.modes.COMMAND_LINE)
        liberator.execute(command);
    };
    every.ps.push({
      handle: setInterval(fun, interval),
      command: command
    });
  }

  function kill (index) {
    if (index == '*') {
      every.ps.forEach(function (process) clearInterval(process.handle));
      liberator.echo(every.ps.length + ' processes were killed!');
      every.ps = [];
    } else {
      let process = every.ps[index];
      if (process) {
        clearInterval(process.handle);
        every.ps.splice(index, index);
        liberator.echo('process "' + process.command + '" was killed!');
      } else {
        liberator.echoerr('unknown process');
      }
    }
  }

  function msec (count) {
    return (count > 0) ? count * 1000 : 1000;
  }

  liberator.modules.commands.addUserCommand(
    ['every', 'ev'],
    'every',
    function (arg, bang, count) {
      if (bang) {
        kill(arg.arguments[0]);
      } else {
        run(arg.string, msec(count));
      }
    },
    {
      count: true,
      bang: true,
      argCount: '+',
      completer: function (context, arg, bang) {
        if (bang) {
          context.title = ['PID', 'every process'];
          context.items = [['*', 'kill em all']].concat(every.ps.map(function (p, i) ([i.toString(), p.command])));
        } else {
          liberator.modules.completion.ex(context);
        }
      }
    },
    true
  );


  liberator.modules.commands.addUserCommand(
    ['delay'],
    'delay',
    function (arg, bang, count) {
      let cmd = arg.string;
      let f = function () {
        if (liberator.mode == liberator.modules.modes.COMMAND_LINE) {
          setTimeout(f, 500);
        } else {
          liberator.execute(cmd);
        }
      };
      setTimeout(f, msec(count));
    },
    {
      count: true,
      argCount: '+',
      completer: function (context) liberator.modules.completion.ex(context)
    }
  );

})();