Skip to content
Closed
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
35 changes: 35 additions & 0 deletions src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IUserSettings>();
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);
}
}
}
3 changes: 3 additions & 0 deletions src/modules/Hosts/Hosts/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,6 +53,7 @@ public UserSettings()
{
_settingsUtils = new SettingsUtils();
ShowStartupWarning = true;
NoWhiteSpace = true;
LoopbackDuplicates = false;
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
Encoding = HostsEncoding.Utf8;
Expand Down
26 changes: 18 additions & 8 deletions src/modules/Hosts/HostsUILib/Helpers/HostsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,33 @@ public async Task WriteAsync(string additionalLines, IEnumerable<Entry> 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());
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IUserSettings
{
public bool ShowStartupWarning { get; }

public bool NoWhiteSpace { get; }

public bool LoopbackDuplicates { get; }

public HostsAdditionalLinesPosition AdditionalLinesPosition { get; }
Expand Down
4 changes: 4 additions & 0 deletions src/settings-ui/Settings.UI.Library/HostsProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -31,6 +34,7 @@ public HostsProperties()
LoopbackDuplicates = false;
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
Encoding = HostsEncoding.Utf8;
NoWhiteSpace = false;
}
}
}
3 changes: 3 additions & 0 deletions src/settings-ui/Settings.UI/SettingsXAML/Views/HostsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<tkcontrols:SettingsCard x:Uid="Hosts_Toggle_ShowStartupWarning" HeaderIcon="{ui:FontIcon Glyph=&#xE7BA;}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.ShowStartupWarning, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="Hosts_Toggle_NoWhiteSpace" HeaderIcon="{ui:FontIcon Glyph=&#xE7BA;}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.NoWhiteSpace, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</controls:SettingsGroup>

<controls:SettingsGroup x:Uid="Hosts_Behavior_GroupSettings" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
Expand Down
5 changes: 4 additions & 1 deletion src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,9 @@ Activate by holding the key for the character you want to add an accent to, then
</data>
<data name="Hosts_Toggle_ShowStartupWarning.Header" xml:space="preserve">
<value>Show a warning at startup</value>
</data>
<data name="Hosts_Toggle_NoWhiteSpace.Header" xml:space="preserve">
<value>Entry starts without white spaces</value>
</data>
<data name="Hosts_Activation_GroupSettings.Header" xml:space="preserve">
<value>Activation</value>
Expand Down Expand Up @@ -4460,7 +4463,7 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="NewPlus_Behaviour_Replace_Variables_Info_Card_Title.Text" xml:space="preserve">
<value>Commonly used variables</value>
<comment>New+ commonly used variables header in the flyout info card</comment>
</data>
</data>
<data name="NewPlus_Year_YYYY_Variable_Description.Text" xml:space="preserve">
<value>Year, represented by a full four or five digits, depending on the calendar used.</value>
<comment>New+ description of the year $YYYY variable - casing of $YYYY is important</comment>
Expand Down
13 changes: 13 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading