diff --git a/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs b/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs index 8eaa37a34811..1704841986ec 100644 --- a/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs +++ b/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs @@ -298,5 +298,40 @@ public async Task Save_Hidden_Hosts() var hidden = fileSystem.FileInfo.New(service.HostsFilePath).Attributes.HasFlag(FileAttributes.Hidden); Assert.IsTrue(hidden); } + + [TestMethod] + public async Task Toggle_No_WhiteSpace() + { + var content = +@" 10.1.1.1 host host.local # comment + 10.1.1.2 host2 host2.local # another comment +# 10.1.1.1 host host.local # comment +# 10.1.1.2 host2 host2.local # another comment +"; + + var contentResult = +@"10.1.1.1 host host.local # comment +10.1.1.2 host2 host2.local # another comment +#10.1.1.1 host host.local # comment +#10.1.1.2 host2 host2.local # another comment +"; + + var fileSystem = new CustomMockFileSystem(); + var userSettings = new Mock(); + userSettings.Setup(m => m.NoWhiteSpace).Returns(true); + var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object); + fileSystem.AddFile(service.HostsFilePath, new MockFileData(content)); + + var dataNoWhiteSpaceOn = await service.ReadAsync(); + await service.WriteAsync(dataNoWhiteSpaceOn.AdditionalLines, dataNoWhiteSpaceOn.Entries); + var resultOn = fileSystem.GetFile(service.HostsFilePath); + Assert.AreEqual(contentResult, resultOn.TextContents); + + userSettings.Setup(m => m.NoWhiteSpace).Returns(false); + var dataNoWhiteSpaceOff = await service.ReadAsync(); + await service.WriteAsync(dataNoWhiteSpaceOff.AdditionalLines, dataNoWhiteSpaceOff.Entries); + var resultsOff = fileSystem.GetFile(service.HostsFilePath); + Assert.AreEqual(content, resultsOff.TextContents); + } } } diff --git a/src/modules/Hosts/Hosts/Settings/UserSettings.cs b/src/modules/Hosts/Hosts/Settings/UserSettings.cs index 3530a3f74b7b..82630fe60b7f 100644 --- a/src/modules/Hosts/Hosts/Settings/UserSettings.cs +++ b/src/modules/Hosts/Hosts/Settings/UserSettings.cs @@ -24,6 +24,8 @@ public class UserSettings : IUserSettings public bool ShowStartupWarning { get; private set; } + public bool NoWhiteSpace { get; private set; } + private bool _loopbackDuplicates; public bool LoopbackDuplicates @@ -51,6 +53,7 @@ public UserSettings() { _settingsUtils = new SettingsUtils(); ShowStartupWarning = true; + NoWhiteSpace = true; LoopbackDuplicates = false; AdditionalLinesPosition = HostsAdditionalLinesPosition.Top; Encoding = HostsEncoding.Utf8; diff --git a/src/modules/Hosts/HostsUILib/Helpers/HostsService.cs b/src/modules/Hosts/HostsUILib/Helpers/HostsService.cs index b07eb8f93ccc..04f5be80e987 100644 --- a/src/modules/Hosts/HostsUILib/Helpers/HostsService.cs +++ b/src/modules/Hosts/HostsUILib/Helpers/HostsService.cs @@ -153,23 +153,33 @@ public async Task WriteAsync(string additionalLines, IEnumerable entries) } else { - if (!e.Active) + if (_userSettings.NoWhiteSpace) { - lineBuilder.Append('#').Append(' '); + if (!e.Active) + { + lineBuilder.Append('#'); + } } - else if (anyDisabled) + else { - lineBuilder.Append(' ').Append(' '); + if (!e.Active) + { + lineBuilder.Append('#').Append(' '); + } + else if (anyDisabled) + { + lineBuilder.Append(' ').Append(' '); + } } lineBuilder.Append(e.Address.PadRight(addressPadding)); lineBuilder.Append(string.Join(' ', e.Hosts).PadRight(hostsPadding)); if (e.Comment != string.Empty) - { - lineBuilder.Append('#').Append(' '); - lineBuilder.Append(e.Comment); - } + { + lineBuilder.Append('#').Append(' '); + lineBuilder.Append(e.Comment); + } lines.Add(lineBuilder.ToString().TrimEnd()); } diff --git a/src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs b/src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs index 21a8e6fa36d3..20178319f52b 100644 --- a/src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs +++ b/src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs @@ -10,6 +10,8 @@ public interface IUserSettings { public bool ShowStartupWarning { get; } + public bool NoWhiteSpace { get; } + public bool LoopbackDuplicates { get; } public HostsAdditionalLinesPosition AdditionalLinesPosition { get; } diff --git a/src/settings-ui/Settings.UI.Library/HostsProperties.cs b/src/settings-ui/Settings.UI.Library/HostsProperties.cs index 90a576601dd6..af7b38294892 100644 --- a/src/settings-ui/Settings.UI.Library/HostsProperties.cs +++ b/src/settings-ui/Settings.UI.Library/HostsProperties.cs @@ -20,6 +20,9 @@ public class HostsProperties [JsonConverter(typeof(BoolPropertyJsonConverter))] public bool LoopbackDuplicates { get; set; } + [JsonConverter(typeof(BoolPropertyJsonConverter))] + public bool NoWhiteSpace { get; set; } + public HostsAdditionalLinesPosition AdditionalLinesPosition { get; set; } public HostsEncoding Encoding { get; set; } @@ -31,6 +34,7 @@ public HostsProperties() LoopbackDuplicates = false; AdditionalLinesPosition = HostsAdditionalLinesPosition.Top; Encoding = HostsEncoding.Utf8; + NoWhiteSpace = false; } } } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml index 2d0a6d0c2d0c..4f3d9b24e68b 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml @@ -44,6 +44,9 @@ + + + diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index a95cdb86cd18..fb16cd8fe971 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -3608,6 +3608,9 @@ Activate by holding the key for the character you want to add an accent to, then Show a warning at startup + + + Entry starts without white spaces Activation @@ -4460,7 +4463,7 @@ Activate by holding the key for the character you want to add an accent to, then Commonly used variables New+ commonly used variables header in the flyout info card - + Year, represented by a full four or five digits, depending on the calendar used. New+ description of the year $YYYY variable - casing of $YYYY is important diff --git a/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs index d2bfb989e77e..c9803b6f3fd2 100644 --- a/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs @@ -105,6 +105,19 @@ public bool LaunchAdministrator } } + public bool NoWhiteSpace + { + get => Settings.Properties.NoWhiteSpace; + set + { + if (value != Settings.Properties.NoWhiteSpace) + { + Settings.Properties.NoWhiteSpace = value; + NotifyPropertyChanged(); + } + } + } + public int AdditionalLinesPosition { get => (int)Settings.Properties.AdditionalLinesPosition;