-
Notifications
You must be signed in to change notification settings - Fork 0
/
rofi-grub-reboot
executable file
·116 lines (98 loc) · 2.82 KB
/
rofi-grub-reboot
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
#!/bin/env node
"use strict";
const util = require("util");
const exec = util.promisify(require("child_process").exec);
(async () => {
/*
* Explaination
* Bash script:
* sudo awk -F \- /^menuentry/{print\$1} /boot/grub/grub.cfg
*
* -F is field separator, we will separate the line (into columns)
* when the '-' is found
* ex (when printing 2nd column '{print $2}'):
* ```
* m42nk - gemink
* btw - i use arch
* ketawa - lol
* ```
*
* will be:
* ```
* gemink
* i use arch
* lol
* ```
*
* /^menuentry means that find the line with 'menuentry'
* as the start of the line (regex)
*
* when using regex, put the print command right after the
* regex ending ('/')
*
* last argument is the name of the file
*
* Cut comand:
* cut -d '"' -f2
*
* -d is the delimiter, we use the double-quote sign ('"')
* -f2 means print the second column of that splitted line
* ex:
* ```
* my name is "pakde" just kidding
* ```
*
* will be:
* ```
* my name is
* pakde
* just kidding
* ```
*
*/
// rofi Scripts
const rofi_menu = 'rofi -dmenu -format i:s -p "Reboot to"';
const rofi_confirm = (os_label) =>
`rofi -dmenu -no-fixed-num-lines -p "Reboot to ${os_label}? (Y/n)" &`;
const rofi_cancel = 'rofi -e "Operation cancelled."';
const rofi_invalid = 'rofi -e "Invalid input, available options: Y/n"';
// get all grub menuentry
const { stdout: rawMenuEntry } = await exec(
"sudo awk -F \\- /^menuentry/{print\\ \\$1} /boot/grub/grub.cfg | cut -d '\"' -f2"
);
// // split output by newline
// const menuentry = rawMenuEntry.split("\n");
// // remove trailing empty array element
// menuentry.pop();
// //join menuentry
// const cleaned = menuentry.join("\n");
const cleaned = rawMenuEntry.slice(0, -1);
const choosen_os_raw = await exec(`echo "${cleaned}" | ${rofi_menu}`).catch(
(err) => console.log("Exited: \n" + err)
);
// stop if error
if (!choosen_os_raw) return;
// remove trailing newline
const chosen = choosen_os_raw.stdout.replace("\n", "");
// destructure result from <bootnum>:<label>
const [bootnum, label] = chosen.split(":");
console.log("choosen " + chosen);
const confirm_raw = await exec(rofi_confirm(label));
// stop if error
if (!confirm_raw) return;
const confirm = confirm_raw.stdout;
// available confirm answer
const yes_ans = ["\n", "y\n", "yes\n"];
const no_ans = ["n\n", "no\n"];
if (yes_ans.includes(confirm.toLowerCase())) {
console.log("yes");
await exec(`sudo grub-reboot ${bootnum} && sudo reboot`);
} else if (no_ans.includes(confirm.toLowerCase())) {
console.log("no");
await exec(rofi_cancel);
} else {
console.log("invalid input");
await exec(rofi_invalid);
}
console.log({ confirm });
})();