mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Can currently watch the file system.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.CommandLine;
|
using System.CommandLine;
|
||||||
using System.CommandLine.Invocation;
|
using System.CommandLine.Invocation;
|
||||||
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Espresso.Shell
|
namespace Espresso.Shell
|
||||||
@@ -25,6 +26,19 @@ namespace Espresso.Shell
|
|||||||
|
|
||||||
Console.WriteLine("Espresso - Computer Caffeination Engine");
|
Console.WriteLine("Espresso - Computer Caffeination Engine");
|
||||||
|
|
||||||
|
var configOption = new Option<string>(
|
||||||
|
aliases: new[] { "--config", "-c" },
|
||||||
|
getDefaultValue: () => string.Empty,
|
||||||
|
description: "Pointer to a PowerToys configuration file that the tool will be watching for changes. All other options are disregarded if config is used.")
|
||||||
|
{
|
||||||
|
Argument = new Argument<string>(() => string.Empty)
|
||||||
|
{
|
||||||
|
Arity = ArgumentArity.ZeroOrOne,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
configOption.Required = false;
|
||||||
|
|
||||||
var displayOption = new Option<bool>(
|
var displayOption = new Option<bool>(
|
||||||
aliases: new[] { "--display-on", "-d" },
|
aliases: new[] { "--display-on", "-d" },
|
||||||
getDefaultValue: () => true,
|
getDefaultValue: () => true,
|
||||||
@@ -53,55 +67,78 @@ namespace Espresso.Shell
|
|||||||
|
|
||||||
var rootCommand = new RootCommand
|
var rootCommand = new RootCommand
|
||||||
{
|
{
|
||||||
|
configOption,
|
||||||
displayOption,
|
displayOption,
|
||||||
timeOption
|
timeOption
|
||||||
};
|
};
|
||||||
|
|
||||||
rootCommand.Description = appName;
|
rootCommand.Description = appName;
|
||||||
|
|
||||||
rootCommand.Handler = CommandHandler.Create<bool, long>(HandleCommandLineArguments);
|
rootCommand.Handler = CommandHandler.Create<string, bool, long>(HandleCommandLineArguments);
|
||||||
|
|
||||||
return rootCommand.InvokeAsync(args).Result;
|
return rootCommand.InvokeAsync(args).Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void HandleCommandLineArguments(bool displayOn, long timeLimit)
|
private static void HandleCommandLineArguments(string config, bool displayOn, long timeLimit)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"The value for --display-on is: {displayOn}");
|
Console.WriteLine($"The value for --display-on is: {displayOn}");
|
||||||
Console.WriteLine($"The value for --time-limit is: {timeLimit}");
|
Console.WriteLine($"The value for --time-limit is: {timeLimit}");
|
||||||
|
|
||||||
if (timeLimit <= 0)
|
if (!string.IsNullOrWhiteSpace(config))
|
||||||
{
|
{
|
||||||
// Indefinite keep awake.
|
// Configuration file is used, therefore we disregard any other command-line parameter
|
||||||
bool success = APIHelper.SetIndefiniteKeepAwake(displayOn);
|
// and instead watch for changes in the file.
|
||||||
if (success)
|
|
||||||
|
FileSystemWatcher watcher = new FileSystemWatcher
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Currently in indefinite keep awake. Display always on: {displayOn}");
|
Path = Path.GetDirectoryName(config),
|
||||||
}
|
EnableRaisingEvents = true,
|
||||||
else
|
NotifyFilter = NotifyFilters.LastWrite,
|
||||||
{
|
Filter = Path.GetFileName(config)
|
||||||
Console.WriteLine("Could not set up the state to be indefinite keep awake.");
|
};
|
||||||
}
|
watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Timed keep-awake.
|
if (timeLimit <= 0)
|
||||||
bool success = APIHelper.SetTimedKeepAwake(timeLimit, displayOn);
|
|
||||||
if (success)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Finished execution of timed keep-awake.");
|
// Indefinite keep awake.
|
||||||
|
bool success = APIHelper.SetIndefiniteKeepAwake(displayOn);
|
||||||
// Because the timed keep-awake execution completed, there is no reason for
|
if (success)
|
||||||
// Espresso to stay alive - I will just shut down the application until it's
|
{
|
||||||
// launched again by the user.
|
Console.WriteLine($"Currently in indefinite keep awake. Display always on: {displayOn}");
|
||||||
Environment.Exit(0);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Could not set up the state to be indefinite keep awake.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine("Could not set up the state to be timed keep awake.");
|
// Timed keep-awake.
|
||||||
|
bool success = APIHelper.SetTimedKeepAwake(timeLimit, displayOn);
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Finished execution of timed keep-awake.");
|
||||||
|
|
||||||
|
// Because the timed keep-awake execution completed, there is no reason for
|
||||||
|
// Espresso to stay alive - I will just shut down the application until it's
|
||||||
|
// launched again by the user.
|
||||||
|
Environment.Exit(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Could not set up the state to be timed keep awake.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new ManualResetEvent(false).WaitOne();
|
new ManualResetEvent(false).WaitOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void HandleEspressoConfigChange(object sender, FileSystemEventArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Reached test!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user