-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
211 lines (202 loc) · 7.86 KB
/
App.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, {useState,useEffect} from "react"
import {render,Text,useInput,Box,useStdout,Newline, Spacer} from "ink"
import Gradient from "ink-gradient"
import BigText from "ink-big-text"
import osu from "node-os-utils"
import os from "os"
import psList from "ps-list"
const mem = osu.mem;
const cpu = osu.cpu;
const osuos = osu.os;
import { exec } from 'child_process';
function getDiskUsage(callback) {
let command;
switch(os.platform()) {
case 'win32':
command = 'wmic logicaldisk get size,freespace,caption';
break;
case 'darwin':
case 'linux':
default:
command = 'df -h';
break;
}
exec(command, (error, stdout, stderr) => {
if (error) {
// console.error(`exec error: ${error}`);
return callback(error);
}
return callback(null, stdout);
});
}
// Using the function
function getOSInfo() {
let platform = os.platform();
let release = os.release();
// Map the platform to a human-readable name
let name;
switch(platform) {
case 'win32': name = 'Windows'; break;
case 'darwin': name = 'macOS'; break;
case 'linux': name = 'Linux'; break;
default: name = platform; break;
}
return `${name} ${release}`;
}
async function getPrograms(){
const programs = await psList();
const result = {};
let count = 0;
let page = "0";
for(let i=0;i<programs.length;i++){
const program = programs[i]
if(count==22){
count = 0;
page = (parseInt(page)+1).toString();
}
if(Object.keys(result).includes(page)){
result[page].push(program)
}else{
result[page] = [program]
}
count+=1
}
return result
}
const App = ()=>{
const {stdout}=useStdout()
const [memInfo,setMemInfo] = useState(null)
const [uptime,setUptime] = useState(null)
const [pList,setPlist] = useState([])
const [pCount, setPCount] = useState(0)
const [cpuUsage,setCpuUsage] = useState(null)
const [diskInfo,setDiskInfo] = useState(null)
const [procInfo,setProcInfo] = useState(null)
const [userInfo,setUserInfo] = useState(null)
const [pageNumber,setPageNumber] = useState("0")
function handlePageDown(){
if(Object.keys(pList).includes((parseInt(pageNumber)+1).toString())){
setPageNumber((parseInt(pageNumber)+1).toString())
}
}
function handlePageUp(){
if(Object.keys(pList).includes((parseInt(pageNumber)-1).toString())){
setPageNumber((parseInt(pageNumber)-1).toString())
}
}
useInput((input,key)=>{
if(input==='q'){
process.exit()
}
if(key.upArrow || input=="k"){
handlePageUp()
}
if(key.downArrow || input=="j"){
handlePageDown()
}
})
useEffect(()=>{
const timeLoop = setInterval(async ()=>{
setPCount((await psList()).length)
setPlist(await getPrograms())
setUptime(osuos.uptime())
setCpuUsage(await cpu.usage())
getDiskUsage((error, data) => {
if (error) {
// console.error(`Error getting disk usage: ${error}`);
} else {
// console.log(`Disk usage:\n${data}`);
setDiskInfo(data.replace(/\r/g, "").split("\n").slice(1,-2))
}
});
setMemInfo(await mem.info())
},100);
return ()=>{
clearInterval(timeLoop);
}
},[])
return (
(memInfo&&diskInfo&&pList)?
<>
<Box height={stdout.rows-4} borderStyle="single" flexDirection="column">
<Box flexDirection="row">
<Box>
<Gradient name="teen">
<BigText font="tiny" text="TERASK"/>
</Gradient>
</Box>
<Box width="75%" marginLeft={5} flexDirection="column" borderStyle="single">
<Text bold>Username: <Text>{os.userInfo().username}</Text></Text>
<Text bold>OS: <Text>{getOSInfo()}</Text></Text>
<Text bold>Uptime: <Text>{uptime}</Text></Text>
</Box>
</Box>
<Box width={stdout.cols}>
<Box width="50%" height={7} borderStyle="single" flexDirection="column">
<Text underline bold>Memory</Text>
{/* <Spacer/> */}
<Text>
Memory Usage:<Text bold color={memInfo["usedMemPercentage"]>90?"red":memInfo["usedMemPercentage"]>50?"yellow":"red"}> {JSON.stringify(memInfo["usedMemPercentage"])}%</Text>
</Text>
{/* <Spacer/> */}
<Text>
Total Memory(Mb):<Text> {JSON.stringify(memInfo["totalMemMb"])}</Text>
</Text>
{/* <Spacer/> */}
{/* <Spacer/> */}
<Text>
Free Memory(Mb):<Text> {JSON.stringify(memInfo["freeMemMb"])}</Text>
</Text>
{/* <Spacer/> */}
<Text>
Used Memory(Mb):<Text> {JSON.stringify(memInfo["usedMemMb"])}</Text>
</Text>
</Box>
<Box width="50%" height={7} borderStyle="single" flexDirection="column">
<Text underline bold>CPU</Text>
{/* <Spacer/> */}
<Text>
CPU Used:<Text bold color={cpuUsage>90?"red":cpuUsage>50?"yellow":"green"}> {cpuUsage}%</Text>
</Text>
{/* <Spacer/> */}
<Text>Model: {cpu.model()}</Text>
{/* <Spacer/> */}
<Text>Count: {cpu.count()}</Text>
{/* <Spacer/> */}
</Box>
<Box width="50%" height={7} borderStyle="single" flexDirection="column">
<Text underline bold>Disk</Text>
{diskInfo.map((el)=>{
const data = el.split(" ").filter(x=>x.length>0)
const usePerc = (parseInt(data[1])/parseInt(data[2]))*100
return (<>
{/* <Spacer/> */}
<Text>
Disk {data[0]} <Text bold color={usePerc>90?"red":usePerc>50?"yellow":"green"}>{usePerc}%</Text>
</Text>
</>)
})}
{/* <Spacer/> */}
</Box>
</Box>
<Box width={stdout.cols} height="100%" flexDirection="column">
<Text bold>Processes ({pCount})</Text>
<Box><Text bold color="cyan">PID - Process</Text></Box>
{pList[pageNumber].map(el=><Box><Text>{el.pid} - {el.name}</Text></Box>)}
<Text color="cyan">Page: {pageNumber}</Text>
</Box>
</Box>
<Box borderStyle="single">
<Text color="white" backgroundColor="red"> QUIT: q </Text>
<Text color="white" backgroundColor="#A020F0" marginLeft={1}> SCROLL: j/k </Text>
</Box>
</>
:null
)
}
const leaveAltScreenCommand = "\x1b[?1049l";
process.stdout.write('\x1b[?1049h\x1b[H\x1b[2J');
process.on("exit", () => {
process.stdout.write(leaveAltScreenCommand);
});
render(<App/>)