Skip to content

Commit

Permalink
Add battery info
Browse files Browse the repository at this point in the history
  • Loading branch information
HO-COOH committed Feb 22, 2021
1 parent 58a3933 commit b04b9b7
Show file tree
Hide file tree
Showing 17 changed files with 561 additions and 57 deletions.
Binary file modified 2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Battery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Windows.Forms;
using System.Linq;
using System.Text;


namespace ResourceMonitor
{
static class Battery
{
private static readonly PowerStatus status = SystemInformation.PowerStatus;

//Returns (hours, minutes, seconds)
public static (int, int, int) BatteryRemains
{
get
{
int remainsInSeconds = status.BatteryLifeRemaining;
int remainsInHour = remainsInSeconds / 60 / 60;
int remainsMinute = (remainsInSeconds - remainsInHour * 3600) / 60;
int remainsSecond = remainsInSeconds - remainsInHour * 3600 - remainsMinute * 60;
return (remainsInHour, remainsMinute, remainsInSeconds);
}
}

public static float BatteryPercent => status.BatteryLifePercent;
}
}
16 changes: 16 additions & 0 deletions CPU.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResourceMonitor
{
static class CPU
{
private static readonly PerformanceCounter counter = new PerformanceCounter("Processor", "% Processor Time", "_Total", Environment.MachineName);

public static int Usage => (int)counter.NextValue();
}
}
163 changes: 117 additions & 46 deletions Command1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using System.Diagnostics;
using Microsoft.VisualBasic.Devices;
using Microsoft.VisualStudio.Debugger.Interop;
using EnvDTE;
using System.IO;

Expand All @@ -20,17 +17,38 @@ namespace ResourceMonitor
/// </summary>
internal sealed class Command1: AsyncPackage
{
static PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", Environment.MachineName);
static PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
static int memtotal = 0;
public static int GetCpuUsage()
{
return (int)cpuCounter.NextValue();
}
public static int GetRamUsage()
private static Disk disk;

/*Options*/
private static int refreshInterval = 1;

private bool showCPU;

private bool showRam;
private static SizeUnit ramUsageUnit;
private static SizeUnit ramTotalUnit;
private bool showVSRam;

private bool showDisk;

private bool showBatteryPercent;
private bool showBatteryTime;

private void UpdateSettings()
{
//System.Threading.Thread.Sleep(500);
return (memtotal - (int)ramCounter.NextValue());
var options = (OptionPage)GetDialogPage(typeof(OptionPage));

showCPU = options.ShowCPU;

showRam = options.ShowRAM;
ramUsageUnit = options.RamUsageUnit;
ramTotalUnit = options.TotalRamUnit;
showVSRam = options.ShowVSRAM;

showDisk = options.ShowDisk;

showBatteryPercent = options.ShowBatteryPercent;
showBatteryTime = options.ShowBatteryTime;
}

/// <summary>
Expand Down Expand Up @@ -99,56 +117,109 @@ public static async Task InitializeAsync(AsyncPackage package)
Instance = new Command1(package, commandService);
}

private long GetSolutionSize(string path)

private static string SizeUnitToStr(SizeUnit unit)
{
if (Directory.Exists(path))
switch (unit)
{
var info = new DirectoryInfo(path);
long size = 0;
foreach (var fileInfo in info.EnumerateFiles("*", SearchOption.AllDirectories))
{
Console.WriteLine($"{fileInfo.FullName} -> {fileInfo.Length}");
size += fileInfo.Length;
}
return size;
case SizeUnit.KB:
return "KB";
case SizeUnit.MB:
return "MB";
case SizeUnit.GB:
return "GB";
}
else
return 0;

return "";
}

private async void GetSolutionDir()
{
try
{
var env = await ServiceProvider.GetServiceAsync(typeof(SDTE)) as DTE;
var solutionDir = new FileInfo(env.Solution.FullName);
disk = solutionDir.Extension.Length == 0 ?
new Disk(env.Solution.FullName) :
new Disk(solutionDir.Directory.FullName);
}
catch
{
disk = null;
}
}
private async Task DoUpdate()
{
var statusBar = await ServiceProvider.GetServiceAsync(typeof(SVsStatusbar)) as IVsStatusbar;
while (true)
{
//await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);
var statusBar = await ServiceProvider.GetServiceAsync(typeof(SVsStatusbar)) as IVsStatusbar;
int frozen;
UpdateSettings();
string str=string.Empty;
if (showCPU)
str += $"CPU: {CPU.Usage} %";
if (showRam || showVSRam)
{
str += $" RAM: ";
if (showVSRam)
{
if(ramUsageUnit != SizeUnit.GB)
str += $"{RAM.VsUsage(ramUsageUnit):0.}";
else
str += $"{RAM.VsUsage(SizeUnit.GB):0.#}";
str += SizeUnitToStr(ramUsageUnit);
}

if (showVSRam && showRam)
str += " / ";

if (showRam)
{
if(ramTotalUnit!=SizeUnit.GB)
str += $"{RAM.TotalUsage(ramTotalUnit):0.}";
else
str += $"{RAM.TotalUsage(SizeUnit.GB):0.#}";
str += SizeUnitToStr(ramTotalUnit);
}
}

statusBar.IsFrozen(out frozen);
if (frozen != 0)
statusBar.FreezeOutput(0);
var mem = GetRamUsage();
if (showDisk)
{
if(disk!=null)
str += $" Disk: {disk.SolutionSize(SizeUnit.MB):0.#}MB";
else
GetSolutionDir();
}

var env = await ServiceProvider.GetServiceAsync(typeof(SDTE)) as DTE;
var solution = env.Solution;
var solutionFile = solution.FullName;
var solutionDir = new FileInfo(solutionFile).Directory;
var size = GetSolutionSize(solutionDir.FullName) / 1024 / 1024;
if (showBatteryPercent || showBatteryTime)
{
str += " Battery:";
if (showBatteryPercent)
str += $" {Battery.BatteryPercent * 100} %";

if (showBatteryTime)
{
var batteryRemain = Battery.BatteryRemains;
str += $" {batteryRemain.Item1} h {batteryRemain.Item2} min";
}
}

statusBar.GetText(out string existingText);
var index = existingText.IndexOf('|');
if(index >= 0)
existingText = existingText.Substring(0, index - 1);
statusBar.FreezeOutput(0);
statusBar?.SetText(existingText + " | " + str);

statusBar.FreezeOutput(1);
System.Threading.Thread.Sleep(refreshInterval * 1000);

statusBar.SetText($"CPU: {GetCpuUsage()} % RAM: {mem} MB / {(float)memtotal/1024.0:0.#} GB ({(float)mem/memtotal *100 :##} %) Disk: {size} MB");
System.Threading.Thread.Sleep(1000);
}
}
private async void Execute(object sender, EventArgs e)
{
var info = new Microsoft.VisualBasic.Devices.ComputerInfo();
memtotal = (int)(info.TotalPhysicalMemory / 1024 / 1024);



await Task.Run(() => DoUpdate());

private async void Execute(object sender, EventArgs e)
{
await Task.Run(DoUpdate);
}
}
}
48 changes: 48 additions & 0 deletions Disk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ResourceMonitor
{
class Disk
{
private static DirectoryInfo directoryInfo;

public Disk(string path)
{
directoryInfo = new DirectoryInfo(path);
}

private static float ToKB(long value) => value / 1024.0f;
private static float ToMB(long value) => value / 1024.0f / 1024.0f;
private static float ToGB(long value) => value / 1024.0f / 1024.0f / 1024.0f;

private static float ConvertUnit(long value, SizeUnit unit)
{
switch (unit)
{
case SizeUnit.KB:
return ToKB(value);
case SizeUnit.MB:
return ToMB(value);
case SizeUnit.GB:
return ToGB(value);
}
return 0.0f;
}

public float SolutionSize(SizeUnit unit)
{
long size = 0;
foreach (var fileInfo in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories))
{
Console.WriteLine($"{fileInfo.FullName} -> {fileInfo.Length}");
size += fileInfo.Length;
}
return ConvertUnit(size, unit);
}
}
}
Binary file added Key.snk
Binary file not shown.
File renamed without changes.
Loading

0 comments on commit b04b9b7

Please sign in to comment.