-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmux-layout.js
executable file
·72 lines (63 loc) · 1.92 KB
/
tmux-layout.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
#!/usr/bin/env node
var tmux = require("./Data/Tmux").Data_Tmux;
var fs = require("fs");
var spawn = require("child_process").spawn
var OptionParser = require("optparse").OptionParser
var switches = [
["-h", "--help", "You are looking at it"],
["-c", "--compile", "Print the compiled tmux command to STDOUT"],
["-i", "--init", "Create an example tmux.json file in the current directory"],
];
var options = {
help : false,
compile : false,
init : false
};
var parser = new OptionParser(switches);
parser.on("help", function() { options.help = true; });
parser.on("compile", function() { options.compile = true; });
parser.on("init", function() { options.init = true; });
parser.banner = "Usage: [Options] layout-file"
parser.parse(process.argv);
if (options.help) {
return console.log(parser.toString());
}
if (options.init) {
if (fs.existsSync("tmux.json")) {
return console.log("tmux.json already exists - refusing to overwrite");
}
var example = {
"title": "tmux-layout (Purescript)",
"windows": [
{
"title": "Main",
"layout": {
"left": {
"top": "echo hi",
"bottom": "git status"
},
"right": "ls -al"
}
}
]
}
return fs.writeFileSync("tmux.json", JSON.stringify(example, null, 2));
}
try {
var lastArg = process.argv[process.argv.length - 1];
var path = lastArg.match(/\.json$/) ? lastArg : "tmux.json";
var json = JSON.parse(fs.readFileSync(path));
var config = tmux.parseConfig(json);
var command = tmux.toCommand(process.cwd())(config);
if (options.compile) {
return console.log(command);
}
spawn("sh", ["-c", command], { stdio: "inherit" });
} catch (err) {
if (err.code === "ENOENT" || err.code === "EACCES") {
process.stderr.write("tmux layout not readable: " + err.path + "\n");
process.exit(1);
}
console.log("Error while parsing config:");
throw err;
}