poll() {
os.cpuUsage(v => {
plugin.currentValue = (Math.floor(v * 100))
plugin.initialized = true
})
}...
selectedProcess = selectedProcess.slice(0, processWidth).trim()
childProcess.exec(`killall "${selectedProcess}"`, () => {})
}
if (key.name === 'c' && charts[2].plugin.sort !== 'cpu') {
charts[2].plugin.sort = 'cpu'
charts[2].plugin.poll()
setTimeout(() => {
processListSelection.select(0)
}, 200)
}
if (key.name === 'm' && charts[2].plugin.sort !== 'mem') {
charts[2].plugin.sort = 'mem'
charts[2].plugin.poll()
...poll() {
const computeUsage = (used, total) => Math.round(100 * (used / total))
if (plugin.isLinux) {
child.exec('free -m', (err, stdout, stderr) => {
if (err) {
console.error(err)
}
const data = stdout.split('\n')[1].replace(/[\s\n\r]+/g, ' ').split(' ')
const used = parseInt(data[2], 10)
const total = parseInt(data[1], 10)
plugin.currentValue = computeUsage(used, total)
})
} else {
plugin.currentValue = Math.round((1 - os.freememPercentage()) * 100)
}
plugin.initialized = true
}...
selectedProcess = selectedProcess.slice(0, processWidth).trim()
childProcess.exec(`killall "${selectedProcess}"`, () => {})
}
if (key.name === 'c' && charts[2].plugin.sort !== 'cpu') {
charts[2].plugin.sort = 'cpu'
charts[2].plugin.poll()
setTimeout(() => {
processListSelection.select(0)
}, 200)
}
if (key.name === 'm' && charts[2].plugin.sort !== 'mem') {
charts[2].plugin.sort = 'mem'
charts[2].plugin.poll()
...poll() {
const stats = {}
// @todo If you can think of a better way of getting process stats,
// then please feel free to send me a pull request. This is version 0.1
// and needs some love.
childProcess.exec('ps -ewwwo %cpu,%mem,comm', (error, stdout, stderr) => {
if (error) {
console.error(error)
}
const lines = stdout.split('\n')
// Ditch the first line
lines[0] = ''
for (const line in lines) {
const currentLine = lines[line].trim().replace(' ', ' ')
const words = currentLine.split(' ')
if (typeof words[0] !== 'undefined' && typeof words[1] !== 'undefined') {
const cpu = words[0].replace(',', '.')
const mem = words[1].replace(',', '.')
const offset = cpu.length + mem.length + 2
let comm = currentLine.slice(offset)
// If we're on Mac then remove the path
if (/^darwin/.test(process.platform)) {
comm = comm.split('/')
comm = comm[comm.length - 1]
} else {
// Otherwise assume linux and remove the unnecessary /1 info like
// you get on kworker
comm = comm.split('/')
comm = comm[0]
}
// If already exists, then add them together
if (typeof stats[comm] !== 'undefined') {
stats[comm] = {
cpu: parseFloat(stats[comm].cpu, 10) + parseFloat(cpu),
mem: parseFloat(stats[comm].mem, 10) + parseFloat(mem),
comm,
count: parseInt(stats[comm].count, 10) + 1
}
} else {
stats[comm] = {
cpu,
mem,
comm,
count: 1
}
}
}
}
const statsArray = []
for (const stat in stats) {
// Divide by number of CPU cores
const cpuRounded = parseFloat(stats[stat].cpu / os.cpus().length).toFixed(1)
const memRounded = parseFloat(stats[stat].mem).toFixed(1)
statsArray.push({
'Command': stats[stat].comm,
'Count': stats[stat].count,
'CPU %': cpuRounded,
'Memory %': memRounded,
'cpu': stats[stat].cpu,
'mem': stats[stat].mem // exact cpu for comparison
})
}
statsArray.sort((a, b) => parseFloat(b[plugin.sort]) - parseFloat(a[plugin.sort]))
plugin.currentValue = statsArray
plugin.initialized = true
})
}...
selectedProcess = selectedProcess.slice(0, processWidth).trim()
childProcess.exec(`killall "${selectedProcess}"`, () => {})
}
if (key.name === 'c' && charts[2].plugin.sort !== 'cpu') {
charts[2].plugin.sort = 'cpu'
charts[2].plugin.poll()
setTimeout(() => {
processListSelection.select(0)
}, 200)
}
if (key.name === 'm' && charts[2].plugin.sort !== 'mem') {
charts[2].plugin.sort = 'mem'
charts[2].plugin.poll()
...check = function (callback) {
try {
var current = require('./package.json').version
var childProcess = require('child_process')
childProcess.exec('npm --color=false info vtop', function (error, stdout, stderr) {
if (error) {
console.error(error)
}
var output = safeEval('(' + stdout + ')')
if (output['dist-tags']['latest'] !== current) {
callback(output['dist-tags']['latest'])
} else {
callback(false)
}
})
} catch (e) {
callback(false)
}
}...
// Create a screen object.
screen = blessed.screen()
// Configure 'q', esc, Ctrl+C for quit
let upgrading = false
const doCheck = () => {
upgrade.check(v => {
upgradeNotice = v
drawHeader()
})
}
doCheck()
// Check for updates every 5 minutes
...install = function (packageName, vars) {
var sudo = require('sudo')
console.log('')
console.log('Installing vtop update...')
console.log('')
console.log(' ** You will need to enter your password to upgrade ** ')
console.log('')
var args = ['npm', 'install', '-g', 'vtop']
console.log(args.join(' '))
var options = {
cachePassword: false,
prompt: 'Password:',
spawnOptions: { stdio: 'inherit' }
}
var child = sudo(args, options)
var path = false
child.stdout.on('data', function (data) {
console.log(data.toString())
if (data.toString().indexOf('vtop.js') !== -1) {
path = data.toString().trim().split(' ')[2]
}
})
child.stderr.on('data', function (data) {
console.log(data.toString())
})
child.on('close', function () {
for (var file in require.cache) {
delete require.cache[file]
}
console.log('Finished updating. Clearing cache and relaunching...')
setTimeout(function () {
for (var v in vars) {
process[v] = vars[v]
}
require(path)
}, 1000)
})
}...
program = blessed.program()
program.clear()
program.disableMouse()
program.showCursor()
program.normalBuffer()
// @todo: show changelog AND smush existing data into it :D
upgrade.install('vtop', [
{
'theme': theme
}
])
}
if ((key.name === 'left' || key.name === 'h') && graphScale < 8) {
...