Skip to content
Merged
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
29 changes: 29 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,34 @@ public async Task Save_Hidden_Hosts()
var hidden = fileSystem.FileInfo.New(service.HostsFilePath).Attributes.HasFlag(FileAttributes.Hidden);
Assert.IsTrue(hidden);
}

[TestMethod]
public async Task NoLeadingSpaces_Disabled_RemovesIndent()
{
var content =
@"10.1.1.1 host host.local # comment
10.1.1.2 host2 host2.local # another comment
";

var expected =
@"10.1.1.1 host host.local # comment
10.1.1.2 host2 host2.local # another comment
# 10.1.1.30 host30 host30.local # new entry
";

var fs = new CustomMockFileSystem();
var settings = new Mock<IUserSettings>();
settings.Setup(s => s.NoLeadingSpaces).Returns(true);
var svc = new HostsService(fs, settings.Object, _elevationHelper.Object);
fs.AddFile(svc.HostsFilePath, new MockFileData(content));

var data = await svc.ReadAsync();
var entries = data.Entries.ToList();
entries.Add(new Entry(0, "10.1.1.30", "host30 host30.local", "new entry", false));
await svc.WriteAsync(data.AdditionalLines, entries);

var result = fs.GetFile(svc.HostsFilePath);
Assert.AreEqual(expected, result.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 @@ -26,6 +26,8 @@ public class UserSettings : IUserSettings

private bool _loopbackDuplicates;

public bool NoLeadingSpaces { get; private set; }

public bool LoopbackDuplicates
{
get => _loopbackDuplicates;
Expand Down Expand Up @@ -88,6 +90,7 @@ private void LoadSettingsFromJson()
AdditionalLinesPosition = (HostsAdditionalLinesPosition)settings.Properties.AdditionalLinesPosition;
Encoding = (HostsEncoding)settings.Properties.Encoding;
LoopbackDuplicates = settings.Properties.LoopbackDuplicates;
NoLeadingSpaces = settings.Properties.NoLeadingSpaces;
}

retry = false;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Hosts/HostsUILib/Helpers/HostsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task WriteAsync(string additionalLines, IEnumerable<Entry> entries)
{
lineBuilder.Append('#').Append(' ');
}
else if (anyDisabled)
else if (anyDisabled && !_userSettings.NoLeadingSpaces)
{
lineBuilder.Append(' ').Append(' ');
}
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 @@ -19,5 +19,7 @@ public interface IUserSettings
event EventHandler LoopbackDuplicatesChanged;

public delegate void OpenSettingsFunction();

public bool NoLeadingSpaces { get; }
}
}
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 @@ -24,13 +24,17 @@ public class HostsProperties

public HostsEncoding Encoding { get; set; }

[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool NoLeadingSpaces { get; set; }

public HostsProperties()
{
ShowStartupWarning = true;
LaunchAdministrator = true;
LoopbackDuplicates = false;
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
Encoding = HostsEncoding.Utf8;
NoLeadingSpaces = 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 @@ -56,6 +56,9 @@
<tkcontrols:SettingsCard x:Uid="Hosts_Toggle_LoopbackDuplicates" HeaderIcon="{ui:FontIcon Glyph=&#xEC27;}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.LoopbackDuplicates, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="Hosts_NoLeadingSpaces" HeaderIcon="{ui:FontIcon Glyph=&#xE8A5;}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.NoLeadingSpaces, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="Hosts_Encoding">
<ComboBox MinWidth="{StaticResource SettingActionControlMinWidth}" SelectedIndex="{x:Bind Path=ViewModel.Encoding, Mode=TwoWay}">
<ComboBoxItem x:Uid="Hosts_Encoding_Utf8" />
Expand Down
6 changes: 6 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -5127,4 +5127,10 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
<data name="KeyBack" xml:space="preserve">
<value>Back key</value>
</data>
<data name="Hosts_NoLeadingSpaces.Header" xml:space="preserve">
<value>No leading spaces</value>
</data>
<data name="Hosts_NoLeadingSpaces.Description" xml:space="preserve">
<value>Do not prepend spaces to active lines when saving the hosts file</value>
</data>
</root>
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 NoLeadingSpaces
{
get => Settings.Properties.NoLeadingSpaces;
set
{
if (value != Settings.Properties.NoLeadingSpaces)
{
Settings.Properties.NoLeadingSpaces = value;
NotifyPropertyChanged();
}
}
}

public int AdditionalLinesPosition
{
get => (int)Settings.Properties.AdditionalLinesPosition;
Expand Down