Skip to content

Commit

Permalink
FYA 1.1
Browse files Browse the repository at this point in the history
 - `Card` command has been renamed to` Switch`
 - `Card` command now searches for the specified video card (or displays the entire list)
 - `Help` command has been improved
  • Loading branch information
Kir-Antipov committed Aug 6, 2019
1 parent 06e7a1f commit 5ea850a
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 87 deletions.
10 changes: 3 additions & 7 deletions FYA/Options/CardOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ namespace FYA.Options
public class CardOptions
{
#region Var
[Option('d', "disable", HelpText = "Indicates turn on or off the specified video card")]
public bool Disable { get; }

[Option('i', "id", Required = true, SetName = "cardID", HelpText = "Video card's instance id")]
[Option('i', "id", SetName = "cardID", HelpText = "Video card's instance id")]
public string ID { get; }

[Option('n', "name", Required = true, SetName = "cardName", HelpText = "Video card's name")]
[Option('n', "name", SetName = "cardName", HelpText = "Video card's name")]
public string Name { get; }
#endregion

#region Init
public CardOptions(bool Disable, string ID, string Name)
public CardOptions(string ID, string Name)
{
this.Disable = Disable;
this.ID = ID ?? string.Empty;
this.Name = Name ?? string.Empty;
}
Expand Down
8 changes: 4 additions & 4 deletions FYA/Options/CrutchRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace FYA.Options
{
public class CrutchRunOptions
{
[Option('p', "path", Required = true, HelpText = "Application path")]
public string Path { get; }

[Option('i', "id", Required = true, SetName = "cardID", HelpText = "Video card's instance id")]
public string ID { get; }

Expand All @@ -15,13 +18,10 @@ public class CrutchRunOptions
[Option('w', "wait", HelpText = "The time the video card is disabled")]
public int Wait { get; }

[Option('p', "path", Required = true, HelpText = "Application path")]
public string Path { get; }

[Option('d', "directory", HelpText = "Application working directory")]
public string WorkingDirectory { get; }

public CrutchRunOptions(string ID, string Name, int Wait, string Path, string WorkingDirectory)
public CrutchRunOptions(string Path, string ID, string Name, int Wait, string WorkingDirectory)
{
if (!string.IsNullOrEmpty(Path))
{
Expand Down
8 changes: 4 additions & 4 deletions FYA/Options/ShortcutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace FYA.Options
public class ShortcutOptions
{
#region Var
[Option('p', "path", Required = true, HelpText = "Application path")]
public string Path { get; }

[Option('i', "id", Required = true, SetName = "cardID", HelpText = "Video card's instance id")]
public string ID { get; }

Expand All @@ -16,9 +19,6 @@ public class ShortcutOptions
[Option('w', "wait", HelpText = "The time the video card is disabled")]
public int Wait { get; }

[Option('p', "path", Required = true, HelpText = "Application path")]
public string Path { get; }

[Option('d', "directory", HelpText = "Application working directory")]
public string WorkingDirectory { get; }

Expand All @@ -33,7 +33,7 @@ public class ShortcutOptions
#endregion

#region Init
public ShortcutOptions(string ID, string Name, int Wait, string Path, string WorkingDirectory, string Save, string Icon, bool ShowCommandLine)
public ShortcutOptions(string Path, string ID, string Name, int Wait, string WorkingDirectory, string Save, string Icon, bool ShowCommandLine)
{
if (!string.IsNullOrEmpty(Path))
{
Expand Down
27 changes: 27 additions & 0 deletions FYA/Options/SwitchOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CommandLine;

namespace FYA.Options
{
public class SwitchOptions
{
#region Var
[Option('i', "id", Required = true, SetName = "cardID", HelpText = "Video card's instance id")]
public string ID { get; }

[Option('n', "name", Required = true, SetName = "cardName", HelpText = "Video card's name")]
public string Name { get; }

[Option('d', "disable", HelpText = "Indicates turn on or off the specified video card")]
public bool Disable { get; }
#endregion

#region Init
public SwitchOptions(string ID, string Name, bool Disable)
{
this.ID = ID ?? string.Empty;
this.Name = Name ?? string.Empty;
this.Disable = Disable;
}
#endregion
}
}
2 changes: 2 additions & 0 deletions FYA/Programs/BaseProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public BaseProgram()
#region Functions
public abstract void Run(IEnumerable<string> Arguments);

public abstract string GetHelpText();

public virtual void PrintException(Exception Exception) => PrintException(Exception.Message);
public virtual void PrintException(string Exception)
{
Expand Down
24 changes: 15 additions & 9 deletions FYA/Programs/BaseProgram`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@ namespace FYA.Programs
public abstract class BaseProgram<TOptions> : BaseProgram
{
#region Functions
public override void Run(IEnumerable<string> Arguments)
private static Parser CreateParser() => new Parser(settings =>
{
Parser parser = new Parser(settings =>
{
settings.AutoVersion = false;
settings.AutoHelp = false;
settings.CaseSensitive = false;
settings.HelpWriter = null;
});
settings.AutoVersion = false;
settings.AutoHelp = false;
settings.CaseSensitive = false;
settings.HelpWriter = null;
});

ParserResult<TOptions> result = parser.ParseArguments<TOptions>(Arguments);
public override void Run(IEnumerable<string> Arguments)
{
ParserResult<TOptions> result = CreateParser().ParseArguments<TOptions>(Arguments);
result
.WithNotParsed(x => { Console.WriteLine(GetHelpText(result)); })
.WithParsed(Run);
}

public abstract void Run(TOptions Options);

public override string GetHelpText()
{
ParserResult<TOptions> result = CreateParser().ParseArguments<TOptions>(new string[0]);
return GetHelpText(result);
}

public virtual string GetHelpText(ParserResult<TOptions> Result)
{
HelpText help = new HelpText
Expand Down
48 changes: 18 additions & 30 deletions FYA/Programs/CardProgram.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
using System;
using FYA.Models;
using FYA.Options;
using FYA.PInvoke;
using System.Linq;

namespace FYA.Programs
{
public class CardProgram : BaseProgram<CardOptions>
{
public override string Usage => "fya card -d --name Intel";
public override string Description => "Enables or disables the specified video card";
public override string Usage => "fya card --name Intel";
public override string Description => "Searches for a specified video card or displays all";

public override void Run(CardOptions Options)
{
string id = Options.ID.ToLower();
string name = Options.Name.ToLower();

Func<VideoCard, bool> predicate = string.IsNullOrEmpty(id)
? new Func<VideoCard, bool>(x => x.Name.ToLower().Contains(name))
: x => x.ID.ToLower().Contains(id) || id.Contains(x.ID.ToLower());
Func<VideoCard, bool> predicate = !string.IsNullOrEmpty(name)
? x => x.Name.ToLower().Contains(name)
: !string.IsNullOrEmpty(id)
? x => x.ID.ToLower().Contains(id) || id.Contains(x.ID.ToLower())
: new Func<VideoCard, bool>(x => true);

VideoCard card = VideoCard.EnumerateVideoCards().FirstOrDefault(predicate);
if (card is null)
Console.WriteLine();
foreach (VideoCard card in VideoCard.EnumerateVideoCards().Where(predicate))
{
if (!string.IsNullOrEmpty(Options.ID))
try
{
SetupAPI.ChangeDeviceState(VideoCard.VideoAdaptersClass, Options.ID, !Options.Disable);
return;
} catch { }
PrintException("The specified video card was not found");
}
else
{
try
{
if (Options.Disable)
card.Disable();
else
card.Enable();
}
catch (Exception e)
{
PrintException(e);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("{0}:", card.Name);
Console.ResetColor();
Console.WriteLine("ID: {0}", card.ID);
Console.WriteLine("State: {0}", card.StateFlags);
Console.WriteLine("Devices: {0}", string.Join(", ", card.Devices.Select(x => $"\"{x}\"")));
Console.WriteLine("Keys: {0}", string.Join(", ", card.RegistryKeys.Select(x => $"\"{x}\"")));
Console.WriteLine();
}
}
}
}
}
19 changes: 14 additions & 5 deletions FYA/Programs/HelpProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ public class HelpProgram : BaseProgram
#region Functions
public override void Run(IEnumerable<string> Arguments)
{
int maxNameLength = Program.Programs.Max(x => x.Name.Length);
string format = $"{{0, -{maxNameLength + 10}}}{{1}}";
Console.WriteLine();
foreach (IProgram program in Program.Programs.OrderBy(x => x.Name))
Console.WriteLine(format, program.Name, program.Description);
string programName = Arguments.FirstOrDefault();
IProgram programToSeeHelp = string.IsNullOrEmpty(programName) ? null : Program.Programs.FirstOrDefault(x => x.Name.Equals(programName, StringComparison.InvariantCultureIgnoreCase));
if (programToSeeHelp is null)
{
int maxNameLength = Program.Programs.Max(x => x.Name.Length);
string format = $"{{0, -{maxNameLength + 10}}}{{1}}";
Console.WriteLine();
foreach (IProgram program in Program.Programs.OrderBy(x => x.Name))
Console.WriteLine(format, program.Name, program.Description);
}
else
Console.WriteLine(programToSeeHelp.GetHelpText());
}

public override string GetHelpText() => string.Empty;
#endregion
}
}
2 changes: 2 additions & 0 deletions FYA/Programs/IProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IProgram

void Run(IEnumerable<string> Arguments);

string GetHelpText();

void PrintException(string Error);
void PrintException(Exception Error);
}
Expand Down
2 changes: 1 addition & 1 deletion FYA/Programs/ShortcutProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Run(ShortcutOptions Options)
shortcut.TargetPath = Path.Combine(location, $"{(Options.ShowCommandLine ? "FYA" : "HFYA")}.exe");
shortcut.WorkingDirectory = location;
shortcut.IconLocation = Options.Icon;
shortcut.Arguments = $"crutchrun {Parser.Default.FormatCommandLine(new CrutchRunOptions(Options.ID, Options.Name, Options.Wait, Options.Path, Options.WorkingDirectory))}";
shortcut.Arguments = $"crutchrun {Parser.Default.FormatCommandLine(new CrutchRunOptions(Options.Path, Options.ID, Options.Name, Options.Wait, Options.WorkingDirectory))}";
shortcut.Save();
}
}
Expand Down
50 changes: 50 additions & 0 deletions FYA/Programs/SwitchProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using FYA.Models;
using FYA.Options;
using FYA.PInvoke;
using System.Linq;

namespace FYA.Programs
{
public class SwitchProgram : BaseProgram<SwitchOptions>
{
public override string Usage => "fya switch --name Intel -d";
public override string Description => "Enables or disables the specified video card";

public override void Run(SwitchOptions Options)
{
string id = Options.ID.ToLower();
string name = Options.Name.ToLower();

Func<VideoCard, bool> predicate = string.IsNullOrEmpty(id)
? new Func<VideoCard, bool>(x => x.Name.ToLower().Contains(name))
: x => x.ID.ToLower().Contains(id) || id.Contains(x.ID.ToLower());

VideoCard card = VideoCard.EnumerateVideoCards().FirstOrDefault(predicate);
if (card is null)
{
if (!string.IsNullOrEmpty(Options.ID))
try
{
SetupAPI.ChangeDeviceState(VideoCard.VideoAdaptersClass, Options.ID, !Options.Disable);
return;
} catch { }
PrintException("The specified video card was not found");
}
else
{
try
{
if (Options.Disable)
card.Disable();
else
card.Enable();
}
catch (Exception e)
{
PrintException(e);
}
}
}
}
}
30 changes: 5 additions & 25 deletions FYA/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FYA")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FYA")]
[assembly: AssemblyCompany("Kir_Antipov")]
[assembly: AssemblyTrademark("Kir_Antipov")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyDescription("F*CK YOU ADOBE (a.k.a. FYA)")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("91743895-7b0a-423f-a302-f029210412e3")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
3 changes: 1 addition & 2 deletions FYA/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"FYA": {
"commandName": "Project",
"commandLineArgs": "shortcut"
"commandName": "Project"
}
}
}

0 comments on commit 5ea850a

Please sign in to comment.