'use strict' ; import { window , ExtensionContext , StatusBarAlignment , StatusBarItem , workspace , WorkspaceConfiguration } from 'vscode' ; import { Units , DiskSpaceFormat , DiskSpaceFormatMappings , FreqMappings , MemMappings } from './constants' ; var si = require ( 'systeminformation' ) ; export function activate ( context ) { var resourceMonitor = new ResMon ( ) ; resourceMonitor . StartUpdating ( ) ; context . subscriptions . push ( resourceMonitor ) ; } abstract class Resource { protected _config : WorkspaceConfiguration ; protected _isShownByDefault : boolean ; protected _configKey : string ; protected _maxWidth : number ; constructor ( config , isShownByDefault , configKey ) { this . _config = config ; this . _isShownByDefault = isShownByDefault ; this . _configKey = configKey ; this . _maxWidth = 0 ; } public async getResourceDisplay ( ) { if ( await this . isShown ( ) ) { let display = await this . getDisplay ( ) ; this . _maxWidth = Math . max ( this . _maxWidth , display . length ) ; return display . padEnd ( this . _maxWidth , ' ' ) ; } return null ; } protected async abstract getDisplay ( ) ; protected async isShown ( ) { return Promise . resolve ( this . _config . get ( `show.${ this . _configKey }` , false ) ) ; } public getPrecision ( ) { return this . _config . get ( "show.precision" , 2 ) ; } protected convertBytesToLargestUnit ( bytes , precision = 2 ) { let unit = Units . None ; while ( bytes / unit >= 1024 && unit < Units . G ) { unit *= 1024 ; } return `${ ( bytes / unit ) . toFixed ( this . getPrecision ( ) ) } ${ Units [ unit ] }` ; } } class CpuUsage extends Resource { constructor ( config ) { super ( config , true , "cpuusage" ) ; } async getDisplay ( ) { let currentLoad = await si . currentLoad ( ) ; return `$(pulse) ${ ( 100 - currentLoad . currentload_idle ) . toFixed ( this . getPrecision ( ) ) }%` ; } } class CpuTemp extends Resource { constructor ( config ) { super ( config , true , "cputemp" ) ; } protected async isShown ( ) { var cpuTemp = ( await si . cpuTemperature ( ) ) . main ; let hasCpuTemp = cpuTemp !== - 1 ; return Promise . resolve ( hasCpuTemp && this . _config . get ( "show.cputemp" , true ) ) ; } async getDisplay ( ) { let currentTemps = await si . cpuTemperature ( ) ; return `$(flame) ${ ( currentTemps . main ) . toFixed ( this . getPrecision ( ) ) } C` ; } } class CpuFreq extends Resource { constructor ( config ) { super ( config , true , "cpufreq" ) ; } async getDisplay ( ) { let cpuCurrentSpeed = await si . cpuCurrentspeed ( ) ; let speedHz = parseFloat ( cpuCurrentSpeed . avg ) * Units . G ; let formattedWithUnits = this . getFormattedWithUnits ( speedHz ) ; return `$(dashboard) ${ ( formattedWithUnits ) }` ; } getFormattedWithUnits ( speedHz ) { var unit = this . _config . get ( 'freq.unit' , "GHz" ) ; var freqDivisor = FreqMappings [ unit ] ; return `${ ( speedHz / freqDivisor ) . toFixed ( this . getPrecision ( ) ) } ${ unit }` ; } } class Battery extends Resource { constructor ( config ) { super ( config , false , "battery" ) ; } protected async isShown ( ) { let hasBattery = ( await si . battery ( ) ) . hasbattery ; return Promise . resolve ( hasBattery && this . _config . get ( "show.battery" , false ) ) ; } async getDisplay ( ) { let rawBattery = await si . battery ( ) ; var percentRemaining = Math . min ( Math . max ( rawBattery . percent , 0 ) , 100 ) ; return `$(plug) ${ percentRemaining }%` ; } } class Memory extends Resource { constructor ( config ) { super ( config , true , "mem" ) ; } async getDisplay ( ) { let unit = this . _config . get ( 'memunit' , "GB" ) ; var memDivisor = MemMappings [ unit ] ; let memoryData = await si . mem ( ) ; let memoryUsedWithUnits = memoryData . active / memDivisor ; let memoryTotalWithUnits = memoryData . total / memDivisor ; return `$(ellipsis) ${ ( memoryUsedWithUnits ) . toFixed ( this . getPrecision ( ) ) }/${ ( memoryTotalWithUnits ) . toFixed ( this . getPrecision ( ) ) } ${ unit }` ; } } class Network extends Resource { constructor ( config ) { super ( config , true , "net" ) ; this . getInterfaceStats ( ) ; } async getInterfaceStats ( ) { let networkInterfaces = await si . networkInterfaces ( ) ; for ( let networkInterface in networkInterfaces ) { console . log ( networkInterface ) ; let networkStats = await si . networkStats ( networkInterface ) ; console . log ( networkStats ) ; } } async getDisplay ( ) { return "" ; } } class DiskSpace extends Resource { constructor ( config ) { super ( config , false , "disk" ) ; } getFormat ( ) { let format = this . _config . get < string > ( "disk.format" ) ; if ( format ) { return DiskSpaceFormatMappings [ format ] ; } else { return DiskSpaceFormat . PercentRemaining ; } } getDrives ( ) { let drives = this . _config . get < string [ ] > ( "disk.drives" ) ; if ( drives ) { return drives ; } else { return [ ] ; } } getFormattedDiskSpace ( fsSize ) { switch ( this . getFormat ( ) ) { case DiskSpaceFormat . PercentRemaining : return `${ fsSize . fs } ${ ( 100 - fsSize . use ) . toFixed ( this . getPrecision ( ) ) }% remaining` ; case DiskSpaceFormat . PercentUsed : return `${ fsSize . fs } ${ fsSize . use . toFixed ( this . getPrecision ( ) ) }% used` ; case DiskSpaceFormat . Remaining : return `${ fsSize . fs } ${ this . convertBytesToLargestUnit ( fsSize . size - fsSize . used ) } remaining` ; case DiskSpaceFormat . UsedOutOfTotal : return `${ fsSize . fs } ${ this . convertBytesToLargestUnit ( fsSize . used ) }/${ this . convertBytesToLargestUnit ( fsSize . size ) } used` ; } } async getDisplay ( ) { let fsSizes = await si . fsSize ( ) ; let drives = this . getDrives ( ) ; var formatted = "$(database) " ; let formattedDrives = [ ] ; for ( let fsSize of fsSizes ) { if ( drives . length === 0 || drives . indexOf ( fsSize . fs ) !== - 1 ) { formattedDrives . push ( this . getFormattedDiskSpace ( fsSize ) ) ; } } return formatted + formattedDrives . join ( ", " ) ; } } class ResMon { private _statusBarItem : StatusBarItem ; private _config : WorkspaceConfiguration ; private _delimiter : string ; private _updating : boolean ; private _resources : Resource [ ] ; constructor ( ) { this . _config = workspace . getConfiguration ( 'resmon' ) ; this . _delimiter = "    " ; this . _updating = false ; this . _statusBarItem = window . createStatusBarItem ( this . _config . get ( 'alignLeft' ) ? StatusBarAlignment . Left : StatusBarAlignment . Right ) ; this . _statusBarItem . color = this . _getColor ( ) ; this . _statusBarItem . show ( ) ; this . _resources = [ ] ; this . _resources . push ( new CpuUsage ( this . _config ) ) ; this . _resources . push ( new CpuFreq ( this . _config ) ) ; this . _resources . push ( new Battery ( this . _config ) ) ; this . _resources . push ( new Memory ( this . _config ) ) ; this . _resources . push ( new DiskSpace ( this . _config ) ) ; this . _resources . push ( new CpuTemp ( this . _config ) ) ; this . _resources . push ( new Network ( this . _config ) ) ; } public StartUpdating ( ) { this . _updating = true ; this . update ( ) ; } public StopUpdating ( ) { this . _updating = false ; } private _getColor ( ) { const defaultColor = "#FFFFFF" ; let hexColorCodeRegex = /^#[0-9A-F]{6}$/i ; let configColor = this . _config . get ( 'color' , defaultColor ) ; if ( ! hexColorCodeRegex . test ( configColor ) ) { configColor = defaultColor ; } return configColor ; } private async update ( ) { if ( this . _updating ) { this . _config = workspace . getConfiguration ( 'resmon' ) ; let proposedAlignment = this . _config . get ( 'alignLeft' ) ? StatusBarAlignment . Left : StatusBarAlignment . Right ; if ( proposedAlignment !== this . _statusBarItem . alignment ) { this . _statusBarItem . dispose ( ) ; this . _statusBarItem = window . createStatusBarItem ( proposedAlignment ) ; this . _statusBarItem . color = this . _getColor ( ) ; this . _statusBarItem . show ( ) ; } else { this . _statusBarItem . color = this . _getColor ( ) ; } let pendingUpdates = this . _resources . map ( resource => resource . getResourceDisplay ( ) ) ; this . _statusBarItem . text = await Promise . all ( pendingUpdates ) . then ( finishedUpdates => { return finishedUpdates . filter ( update => update !== null ) . join ( this . _delimiter ) ; } ) ; setTimeout ( ( ) => this . update ( ) , this . _config . get ( 'updatefrequencyms' , 2000 ) ) ; } } dispose ( ) { this . StopUpdating ( ) ; this . _statusBarItem . dispose ( ) ; } } export function deactivate ( ) { }