Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/WindowResizer.Base/ConfigUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,35 @@ public static bool Load(string? configPath, Action<string>? onError)
return false;
}
}

public static bool LoadOrCreate(string? configPath, string? profileName, Action<string>? onError)
{
configPath ??= Path.Combine(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(WindowResizer)),
DefaultConfigFile);

profileName ??= string.Empty;

try
{
ProfilesFactory.Load(configPath);
}
catch (Exception ex)
{
if (ex is System.IO.FileNotFoundException)
{
var cnf = ProfileConfig.NewConfig(profileName);
}
else
{
onError?.Invoke("Unexpected error while trying to load config file at \"" + configPath + "\": " + ex.Message);
return false;
}
}

ProfilesFactory.ConfigPath = configPath;
return true;
}

}
}
52 changes: 52 additions & 0 deletions src/WindowResizer.Base/WindowCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ public static bool Resize(string? configPath, string? profileName, string? proce
return true;
}

public static bool SaveAll(string? configPath, string? profileName,
Action<string>? onError = null,
Action<List<TargetWindow>>? onDebug = null)
{
var profile = LoadOrCreateConfig(configPath, profileName, onError);

if (profile == null)
{
return false;
}

profile.WindowSizes.Clear();

var windows = Resizer.GetOpenWindows();
var targets = new List<TargetWindow>();

foreach (var handle in windows)
{
if (!IsProcessAvailable(handle, out string processName, null))
{
continue;
}

var t = Resizer.GetWindowTitle(handle);

targets.Add(new TargetWindow(handle, processName, t));
}

foreach (var tp in targets)
{
UpdateOrSaveWindowSize(tp.Handle, profile, (p, e) =>
{
tp.Result = "Elevated privileges may be required.";
onError?.Invoke($"Unable to save position for process <{p}>, elevated privileges may be required.");
});
}

onDebug?.Invoke(targets);

return true;
}

public class TargetWindow
{
public TargetWindow(IntPtr handle, string processName, string? title)
Expand Down Expand Up @@ -114,4 +156,14 @@ public TargetWindow(IntPtr handle, string processName, string? title)

return p;
}

private static ProfileConfig? LoadOrCreateConfig(string? configPath, string? profileName, Action<string>? onError)
{
if (!ConfigUtils.LoadOrCreate(configPath, profileName, onError))
{
return null;
}

return ProfilesFactory.Current;
}
}
65 changes: 65 additions & 0 deletions src/WindowResizer.CLI/Commands/SaveAllCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
using Spectre.Console;
using WindowResizer.Base;
using WindowResizer.CLI.Utils;

namespace WindowResizer.CLI.Commands
{
internal class SaveAllCommand : Command
{
public SaveAllCommand() : base("saveAll", "Saves the current position of all open windows.")
{
var configOption = new ConfigOption();
AddOption(configOption);
var profileOption = new ProfileOption();
AddOption(profileOption);
var verboseOption = new VerboseOption();
AddOption(verboseOption);

this.SetHandler((config, profile, verbose) =>
{
void VerboseInfo(List<WindowCmd.TargetWindow> lists)
{
if (verbose)
{
Verbose(lists);
}
}

var success = WindowCmd.SaveAll(config?.FullName, profile, Output.Error, VerboseInfo);

return Task.FromResult(success ? 0 : 1);
}, configOption, profileOption, verboseOption);
}

private static void Verbose(List<WindowCmd.TargetWindow> lists)
{
if (!lists.Any())
{
Output.Echo("No windows to be saved.");
return;
}

Output.Echo("The following windows were saved:");

var table = new Table();
table.AddColumn(new TableColumn("Handle"));
table.AddColumn(new TableColumn("Process"));
table.AddColumn(new TableColumn("Title"));
table.AddColumn(new TableColumn("Success").Centered());
table.AddColumn(new TableColumn("Error"));
foreach (var item in lists)
{
var result = string.IsNullOrEmpty(item.Result) ? "[green]Y[/]" : "[red]N[/]";
table.AddRow(item.Handle.ToString(), $"[green]{item.ProcessName}[/]", item.Title ?? string.Empty, result, $"[red]{item.Result}[/]");
}

table.Border(TableBorder.Square);
table.Alignment(Justify.Left);
AnsiConsole.Write(table);
}
}
}
1 change: 1 addition & 0 deletions src/WindowResizer.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static Task<int> Main(string[] args)

var rootCommand = new RootCommand($"{nameof(WindowResizer)} CLI.");
rootCommand.AddCommand(new ResizeCommand());
rootCommand.AddCommand(new SaveAllCommand());

var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
Expand Down