Skip to content

Commit 2825762

Browse files
author
Lionel Penaud
committed
chore(init)
0 parents  commit 2825762

16 files changed

+442
-0
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": ["transform-class-properties", "transform-function-bind", "transform-object-rest-spread"]
4+
}

.eslintrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "airbnb-base",
3+
"parser": "babel-eslint",
4+
"rules": {
5+
"comma-dangle": [2, "never"],
6+
"import/no-extraneous-dependencies": 0,
7+
"no-unused-vars": 1,
8+
"object-curly-spacing": [2, "never"],
9+
"max-len": [1, 128]
10+
}
11+
}

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
lib/*
4+
yarn.lock
5+
*.log

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# node-xrandr
2+
3+
## Allow parsing of xrandr output
4+
5+

package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "xrandr",
3+
"version": "0.1.0",
4+
"description": "Xrandr output parser",
5+
"main": "lib/index.js",
6+
"repository": "[email protected]:carlipa/node-xrandr.git",
7+
"author": "Lionel Penaud <[email protected]>",
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "mocha",
11+
"test:watch": "npm run test -- --watch",
12+
"lint": "eslint src/ test/",
13+
"compile": "rimraf lib/*; babel src/ -d lib/ -s",
14+
"prepublish": "npm run compile"
15+
},
16+
"devDependencies": {
17+
"babel-cli": "^6.22.2",
18+
"babel-eslint": "^7.1.1",
19+
"babel-plugin-transform-class-properties": "^6.22.0",
20+
"babel-plugin-transform-function-bind": "^6.22.0",
21+
"babel-plugin-transform-object-rest-spread": "^6.22.0",
22+
"babel-preset-es2015": "^6.22.0",
23+
"babel-register": "^6.22.0",
24+
"eslint": "^3.14.1",
25+
"eslint-config-airbnb-base": "^11.0.1",
26+
"eslint-plugin-import": "^2.2.0",
27+
"eslint-plugin-jsx-a11y": "^3.0.2",
28+
"expect": "^1.20.2",
29+
"mocha": "^3.2.0",
30+
"rimraf": "^2.5.4"
31+
}
32+
}

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import xrandr from './xrandr';
2+
3+
export default xrandr;

src/xrandr.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const CONNECTED_REGEX = /^(\S+) connected (?:(\d+)x(\d+))?/;
2+
const DISCONNECTED_REGEX = /^(\S+) disconnected/;
3+
const MODE_REGEX = /^\s+(\d+)x([0-9i]+)\s+((?:\d+\.)?\d+)([* ]?)([+ ]?)/;
4+
5+
export default function xrandrParser(input) {
6+
let strInput = input;
7+
if (Buffer.isBuffer(input)) {
8+
strInput = input.toString();
9+
}
10+
const lines = strInput.split('\n');
11+
const result = {};
12+
let mode = {};
13+
let lastInterface;
14+
15+
lines.forEach((line) => {
16+
let parts;
17+
if (CONNECTED_REGEX.test(line)) {
18+
parts = CONNECTED_REGEX.exec(line);
19+
result[parts[1]] = {
20+
connected: true,
21+
modes: []
22+
};
23+
if (parts[2] && parts[3]) {
24+
result[parts[1]].width = parseInt(parts[2], 10);
25+
result[parts[1]].height = parseInt(parts[3], 10);
26+
}
27+
lastInterface = parts[1];
28+
} else if (DISCONNECTED_REGEX.test(line)) {
29+
parts = DISCONNECTED_REGEX.exec(line);
30+
result[parts[1]] = {
31+
connected: false,
32+
modes: []
33+
};
34+
lastInterface = parts[1];
35+
} else if (lastInterface && MODE_REGEX.test(line)) {
36+
parts = MODE_REGEX.exec(line);
37+
mode = {
38+
width: parseInt(parts[1], 10),
39+
height: parseInt(parts[2], 10),
40+
rate: parseFloat(parts[3], 10)
41+
};
42+
if (parts[4] === '+' || parts[5] === '+') mode.native = true;
43+
if (parts[4] === '*' || parts[5] === '*') mode.current = true;
44+
result[lastInterface].modes.push(mode);
45+
} else {
46+
lastInterface = null;
47+
}
48+
});
49+
return result;
50+
}

test/.eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./../.eslintrc",
3+
"env": {
4+
mocha: true
5+
}
6+
}

test/fixtures/input1.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Screen 0: minimum 8 x 8, current 1920 x 1080, maximum 32767 x 32767
2+
DP1 disconnected (normal left inverted right x axis y axis)
3+
HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 531mm x 299mm
4+
1920x1080 60.00*+ 50.00 59.94
5+
1920x1080i 60.00 50.00 59.94
6+
1680x1050 59.88
7+
1440x900 59.90
8+
1280x960 60.00
9+
1366x768 59.79 60.00
10+
1360x768 60.02
11+
1280x800 59.91
12+
1280x768 59.87
13+
1280x720 60.00 50.00 59.94
14+
1024x768 60.00
15+
800x600 60.32 56.25
16+
720x576 50.00
17+
720x576i 50.00
18+
720x480 60.00 59.94
19+
640x480 60.00 59.94
20+
HDMI2 disconnected (normal left inverted right x axis y axis)
21+
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

test/fixtures/input2.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Screen 0: minimum 8 x 8, current 1920 x 1080, maximum 32767 x 32767
2+
DP1 disconnected (normal left inverted right x axis y axis)
3+
HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 531mm x 299mm
4+
1920x1080 60.00+* 50.00 59.94
5+
1920x1080i 60.00 50.00 59.94
6+
1680x1050 59.88
7+
1440x900 59.90
8+
1280x960 60.00
9+
1366x768 59.79 60.00
10+
1360x768 60.02
11+
1280x800 59.91
12+
1280x768 59.87
13+
1280x720 60.00 50.00 59.94
14+
1024x768 60.00
15+
800x600 60.32 56.25
16+
720x576 50.00
17+
720x576i 50.00
18+
720x480 60.00 59.94
19+
640x480 60.00 59.94
20+
HDMI2 disconnected (normal left inverted right x axis y axis)
21+
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

test/fixtures/input3.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Screen 0: minimum 8 x 8, current 1280 x 800, maximum 32767 x 32767
2+
DP1 disconnected (normal left inverted right x axis y axis)
3+
HDMI1 connected primary 1280x800+0+0 (normal left inverted right x axis y axis) 531mm x 299mm
4+
1920x1080 60.00 + 50.00 59.94
5+
1920x1080i 60.00 50.00 59.94
6+
1680x1050 59.88
7+
1440x900 59.90
8+
1280x960 60.00
9+
1366x768 59.79 60.00
10+
1360x768 60.02
11+
1280x800 59.91*
12+
1280x768 59.87
13+
1280x720 60.00 50.00 59.94
14+
1024x768 60.00
15+
800x600 60.32 56.25
16+
720x576 50.00
17+
720x576i 50.00
18+
720x480 60.00 59.94
19+
640x480 60.00 59.94
20+
HDMI2 disconnected (normal left inverted right x axis y axis)
21+
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

test/fixtures/output1.json

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"DP1": {
3+
"connected": false,
4+
"modes": []
5+
},
6+
"HDMI1": {
7+
"connected": true,
8+
"modes": [
9+
{
10+
"current": true,
11+
"native": true,
12+
"height": 1080,
13+
"rate": 60,
14+
"width": 1920
15+
},
16+
{
17+
"height": 1080,
18+
"rate": 60,
19+
"width": 1920
20+
},
21+
{
22+
"height": 1050,
23+
"rate": 59.88,
24+
"width": 1680
25+
},
26+
{
27+
"height": 900,
28+
"rate": 59.9,
29+
"width": 1440
30+
},
31+
{
32+
"height": 960,
33+
"rate": 60,
34+
"width": 1280
35+
},
36+
{
37+
"height": 768,
38+
"rate": 59.79,
39+
"width": 1366
40+
},
41+
{
42+
"height": 768,
43+
"rate": 60.02,
44+
"width": 1360
45+
},
46+
{
47+
"height": 800,
48+
"rate": 59.91,
49+
"width": 1280
50+
},
51+
{
52+
"height": 768,
53+
"rate": 59.87,
54+
"width": 1280
55+
},
56+
{
57+
"height": 720,
58+
"rate": 60,
59+
"width": 1280
60+
},
61+
{
62+
"height": 768,
63+
"rate": 60,
64+
"width": 1024
65+
},
66+
{
67+
"height": 600,
68+
"rate": 60.32,
69+
"width": 800
70+
},
71+
{
72+
"height": 576,
73+
"rate": 50,
74+
"width": 720
75+
},
76+
{
77+
"height": 576,
78+
"rate": 50,
79+
"width": 720
80+
},
81+
{
82+
"height": 480,
83+
"rate": 60,
84+
"width": 720
85+
},
86+
{
87+
"height": 480,
88+
"rate": 60,
89+
"width": 640
90+
}
91+
]
92+
},
93+
"HDMI2": {
94+
"connected": false,
95+
"modes": []
96+
},
97+
"VIRTUAL1": {
98+
"connected": false,
99+
"modes": []
100+
}
101+
}

test/fixtures/output3.json

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"DP1": {
3+
"connected": false,
4+
"modes": []
5+
},
6+
"HDMI1": {
7+
"connected": true,
8+
"modes": [
9+
{
10+
"native": true,
11+
"height": 1080,
12+
"rate": 60,
13+
"width": 1920
14+
},
15+
{
16+
"height": 1080,
17+
"rate": 60,
18+
"width": 1920
19+
},
20+
{
21+
"height": 1050,
22+
"rate": 59.88,
23+
"width": 1680
24+
},
25+
{
26+
"height": 900,
27+
"rate": 59.9,
28+
"width": 1440
29+
},
30+
{
31+
"height": 960,
32+
"rate": 60,
33+
"width": 1280
34+
},
35+
{
36+
"height": 768,
37+
"rate": 59.79,
38+
"width": 1366
39+
},
40+
{
41+
"height": 768,
42+
"rate": 60.02,
43+
"width": 1360
44+
},
45+
{
46+
"current": true,
47+
"height": 800,
48+
"rate": 59.91,
49+
"width": 1280
50+
},
51+
{
52+
"height": 768,
53+
"rate": 59.87,
54+
"width": 1280
55+
},
56+
{
57+
"height": 720,
58+
"rate": 60,
59+
"width": 1280
60+
},
61+
{
62+
"height": 768,
63+
"rate": 60,
64+
"width": 1024
65+
},
66+
{
67+
"height": 600,
68+
"rate": 60.32,
69+
"width": 800
70+
},
71+
{
72+
"height": 576,
73+
"rate": 50,
74+
"width": 720
75+
},
76+
{
77+
"height": 576,
78+
"rate": 50,
79+
"width": 720
80+
},
81+
{
82+
"height": 480,
83+
"rate": 60,
84+
"width": 720
85+
},
86+
{
87+
"height": 480,
88+
"rate": 60,
89+
"width": 640
90+
}
91+
]
92+
},
93+
"HDMI2": {
94+
"connected": false,
95+
"modes": []
96+
},
97+
"VIRTUAL1": {
98+
"connected": false,
99+
"modes": []
100+
}
101+
}

0 commit comments

Comments
 (0)