Skip to content

Commit

Permalink
Merge pull request #79 from sgrottel/ToggleDisplay/yaclap
Browse files Browse the repository at this point in the history
Toggle display/yaclap
  • Loading branch information
sgrottel authored Dec 4, 2024
2 parents ffeea1d + 1252300 commit ac92074
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 48 deletions.
70 changes: 33 additions & 37 deletions ToggleDisplay/CmdLineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//
#include "CmdLineArgs.h"

#include "yaclap.hpp"

#include <algorithm>
#include <iostream>

Expand All @@ -23,47 +25,41 @@ bool CmdLineArgs::Parse(int argc, const wchar_t* argv[])
command = Command::Unknown;
id.clear();

if (argc <= 1)
{
return false;
}
yaclap::Parser<wchar_t> parser(L"ToggleDisplay.exe", L"Toggle Display Utility");

std::wstring cmdStr{ argv[1] };
std::transform(cmdStr.begin(), cmdStr.end(), cmdStr.begin(), std::toupper);
yaclap::Argument<wchar_t> idArgument(L"id", L"An identifier for the display to select for the operation");

if (cmdStr == L"LIST")
{
command = Command::List;
}
else if (cmdStr == L"TOGGLE")
{
command = Command::Toggle;
}
else if (cmdStr == L"ENABLE")
{
command = Command::Enable;
}
else if (cmdStr == L"DISABLE")
{
command = Command::Disable;
}
yaclap::Command<wchar_t> listCmd({ L"LIST", yaclap::Alias<wchar_t>::StringCompare::CaseInsensitive }, L"to list all displays");

if (argc > 2)
{
id = argv[2];
}
yaclap::Command<wchar_t> toggleCmd({ L"TOGGLE", yaclap::Alias<wchar_t>::StringCompare::CaseInsensitive }, L"to toggle a display (enable if disabled, disable if enabled)");
toggleCmd.Add(idArgument);

return command != Command::Unknown;
}
yaclap::Command<wchar_t> enableCmd({ L"ENABLE", yaclap::Alias<wchar_t>::StringCompare::CaseInsensitive }, L"to enable a display");
enableCmd.Add(idArgument);

#include "SimpleLog/SimpleLog.hpp"
yaclap::Command<wchar_t> disableCmd({ L"DISABLE", yaclap::Alias<wchar_t>::StringCompare::CaseInsensitive }, L"to disable a display");
disableCmd.Add(idArgument);

void CmdLineArgs::PrintHelp(sgrottel::ISimpleLog& log)
{
using sgrottel::SimpleLog;
SimpleLog::Error(log, "You must specify a command:\n");
SimpleLog::Error(log, " LIST -- to list all displays\n");
SimpleLog::Error(log, " TOGGLE -- to toggle a display (enable if disabled, disable if enabled)\n");
SimpleLog::Error(log, " ENABLE -- to enable a display\n");
SimpleLog::Error(log, " DISABLE -- to disable a display\n");
parser.Add(listCmd)
.Add(toggleCmd)
.Add(enableCmd)
.Add(disableCmd);

yaclap::Parser<wchar_t>::Result res = parser.Parse(argc, argv);

if (res.HasCommand(listCmd)) { command = Command::List; }
else if (res.HasCommand(toggleCmd)) { command = Command::Toggle; }
else if (res.HasCommand(enableCmd)) { command = Command::Enable; }
else if (res.HasCommand(disableCmd)) { command = Command::Disable; }
else {
res.SetError(L"You must specify a command");
}

auto const& idVal = res.GetArgument(idArgument);
if (idVal.HasValue()) {
id = idVal;
}

parser.PrintErrorAndHelpIfNeeded(res);
return res.IsSuccess() && !res.ShouldShowHelp();
}
1 change: 0 additions & 1 deletion ToggleDisplay/CmdLineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ struct CmdLineArgs
std::wstring id;

bool Parse(int argc, const wchar_t* argv[]);
void PrintHelp(sgrottel::ISimpleLog& log);
};
341 changes: 341 additions & 0 deletions ToggleDisplay/LogUtility.cpp

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions ToggleDisplay/LogUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ToggleDisplay
// Copyright 2024, SGrottel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once

#include "DisplayConfig.h"

namespace sgrottel
{
class ISimpleLog;
}

void LogPath(sgrottel::ISimpleLog& log, DisplayConfig::PathInfo const* path);
void LogPaths(sgrottel::ISimpleLog& log, DisplayConfig::PathsVector const& paths);
void LogModes(sgrottel::ISimpleLog& log, DisplayConfig::ModesVector const& modes);
19 changes: 14 additions & 5 deletions ToggleDisplay/ToggleDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "DisplayConfig.h"

#include "SimpleLog/SimpleLog.hpp"
#include "LogUtility.h"

#include <iostream>
#include <cassert>
Expand All @@ -41,20 +42,28 @@ int wmain(int argc, const wchar_t* argv[])

if (!cmd.Parse(argc, argv))
{
cmd.PrintHelp(log);
return 1;
}

DisplayConfig::ReturnCode res;
res = DisplayConfig::Query(DisplayConfig::QueryScope::AllPaths, paths, modes);
if (res != DisplayConfig::ReturnCode::Success)
{
SimpleLog::Error(log, "Failed to query display config: %s", DisplayConfig::to_string(res));
SimpleLog::Error(log, "Failed to query display config: %s", DisplayConfig::to_string(res).c_str());
return 1;
}
log.Write(sgrottel::EchoingSimpleLog::FlagDontEcho, "Query Result Paths:");
LogPaths(log, paths);
log.Write(sgrottel::EchoingSimpleLog::FlagDontEcho, "Query Result Modes:");
LogModes(log, modes);

DisplayConfig::FilterPaths(paths);
log.Write(sgrottel::EchoingSimpleLog::FlagDontEcho, "Filtered Paths:");
LogPaths(log, paths);

DisplayConfig::PathInfo* selected = DisplayConfig::FindPath(paths, cmd.id);
log.Write(sgrottel::EchoingSimpleLog::FlagDontEcho, "Selected Path:");
LogPath(log, selected);

log.Write(sgrottel::EchoingSimpleLog::FlagDontEcho, ("Command = " + std::to_string(static_cast<int>(cmd.command))).c_str());
switch (cmd.command)
Expand All @@ -81,7 +90,7 @@ int wmain(int argc, const wchar_t* argv[])
res = DisplayConfig::Apply(paths);
if (res != DisplayConfig::ReturnCode::Success)
{
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res));
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res).c_str());
return 1;
}

Expand All @@ -102,7 +111,7 @@ int wmain(int argc, const wchar_t* argv[])
res = DisplayConfig::Apply(paths);
if (res != DisplayConfig::ReturnCode::Success)
{
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res));
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res).c_str());
return 1;
}

Expand All @@ -123,7 +132,7 @@ int wmain(int argc, const wchar_t* argv[])
res = DisplayConfig::Apply(paths);
if (res != DisplayConfig::ReturnCode::Success)
{
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res));
SimpleLog::Error(log, "Failed to apply changed display config: %s", DisplayConfig::to_string(res).c_str());
return 1;
}

Expand Down
13 changes: 9 additions & 4 deletions ToggleDisplay/ToggleDisplay.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ItemGroup>
<ClCompile Include="CmdLineArgs.cpp" />
<ClCompile Include="DisplayConfig.cpp" />
<ClCompile Include="LogUtility.cpp" />
<ClCompile Include="ToggleDisplay.cpp" />
</ItemGroup>
<ItemGroup>
Expand All @@ -29,6 +30,8 @@
<ItemGroup>
<ClInclude Include="CmdLineArgs.h" />
<ClInclude Include="DisplayConfig.h" />
<ClInclude Include="LogUtility.h" />
<ClInclude Include="SimpleLog\SimpleLog.hpp" />
<ClInclude Include="VersionInfo.h" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -109,7 +112,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -125,7 +128,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -141,7 +144,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -157,7 +160,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -169,11 +172,13 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="packages\SGrottel.SimpleLog.Cpp.2.2.0\build\native\SGrottel.SimpleLog.Cpp.targets" Condition="Exists('packages\SGrottel.SimpleLog.Cpp.2.2.0\build\native\SGrottel.SimpleLog.Cpp.targets')" />
<Import Project="packages\SGrottel.yaclap.0.3.0\build\native\SGrottel.yaclap.targets" Condition="Exists('packages\SGrottel.yaclap.0.3.0\build\native\SGrottel.yaclap.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\SGrottel.SimpleLog.Cpp.2.2.0\build\native\SGrottel.SimpleLog.Cpp.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SGrottel.SimpleLog.Cpp.2.2.0\build\native\SGrottel.SimpleLog.Cpp.targets'))" />
<Error Condition="!Exists('packages\SGrottel.yaclap.0.3.0\build\native\SGrottel.yaclap.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SGrottel.yaclap.0.3.0\build\native\SGrottel.yaclap.targets'))" />
</Target>
</Project>
6 changes: 6 additions & 0 deletions ToggleDisplay/ToggleDisplay.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<ClCompile Include="CmdLineArgs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LogUtility.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="VersionInfo.rc">
Expand All @@ -40,6 +43,9 @@
<ClInclude Include="VersionInfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LogUtility.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
2 changes: 1 addition & 1 deletion ToggleDisplay/VersionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define TOGGLEDISPLAY_VER_MAJOR 1
#define TOGGLEDISPLAY_VER_MINOR 1
#define TOGGLEDISPLAY_VER_PATCH 1
#define TOGGLEDISPLAY_VER_PATCH 2
#define TOGGLEDISPLAY_VER_BUILD 0

#define TOGGLEDISPLAY_VER_YEARSTR "2023-2024"
1 change: 1 addition & 0 deletions ToggleDisplay/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SGrottel.SimpleLog.Cpp" version="2.2.0" targetFramework="native" />
<package id="SGrottel.yaclap" version="0.3.0" targetFramework="native" />
</packages>

0 comments on commit ac92074

Please sign in to comment.