Skip to content

Commit b97762e

Browse files
authored
Merge pull request #531 from LogExperts/menutoolbarcontroller
MenuToolbarController added and Extracted functionality from LogTabWindow
2 parents 38864a6 + f94c5fe commit b97762e

27 files changed

Lines changed: 1122 additions & 205 deletions

src/LogExpert.Configuration/ConfigManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ public void AddToFileHistory (string fileName)
281281
Save(SettingsFlags.FileHistory);
282282
}
283283

284+
[SupportedOSPlatform("windows")]
285+
public void RemoveFromFileHistory (string fileName)
286+
{
287+
bool findName (string s) => s.ToUpperInvariant().Equals(fileName.ToUpperInvariant(), StringComparison.Ordinal);
288+
289+
var index = Instance.Settings.FileHistoryList.FindIndex(findName);
290+
291+
if (index != -1)
292+
{
293+
Instance.Settings.FileHistoryList.RemoveAt(index);
294+
}
295+
296+
Save(SettingsFlags.FileHistory);
297+
}
298+
299+
284300
public void ClearLastOpenFilesList ()
285301
{
286302
lock (_loadSaveLock)

src/LogExpert.Core/Classes/Log/PositionAwareStreamReaderBase.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected PositionAwareStreamReaderBase (Stream stream, EncodingOptions encoding
4747

4848
MaximumLineLength = maximumLineLength;
4949

50-
_preambleLength = DetectPreambleLengthAndEncoding(out var detectedEncoding);
50+
(_preambleLength, Encoding? detectedEncoding) = DetectPreambleLength(_stream);
5151

5252
var usedEncoding = DetermineEncoding(encodingOptions, detectedEncoding);
5353
_posIncPrecomputed = GetPosIncPrecomputed(usedEncoding);
@@ -165,11 +165,19 @@ protected void MovePosition (int offset)
165165

166166
#region Private Methods
167167

168+
public static Encoding DetermineEncoding (EncodingOptions options, Encoding detectedEncoding)
169+
{
170+
return options?.Encoding != null
171+
? options.Encoding
172+
: detectedEncoding ?? options?.DefaultEncoding ?? Encoding.Default;
173+
}
174+
168175
/// <summary>
169-
/// Determines the actual number of preamble bytes in the file.
176+
/// Determines the actual number of preamble bytes in the file and the Encoding.
170177
/// </summary>
171-
/// <returns>Number of preamble bytes in the file</returns>
172-
private int DetectPreambleLengthAndEncoding (out Encoding detectedEncoding)
178+
/// <param name="stream"></param>
179+
/// <returns>Number of preamble bytes in the file and the Encoding if there is one</returns>
180+
public static (int length, Encoding? detectedEncoding) DetectPreambleLength (Stream stream)
173181
{
174182
/*
175183
UTF-8: EF BB BF
@@ -179,31 +187,15 @@ private int DetectPreambleLengthAndEncoding (out Encoding detectedEncoding)
179187
UTF-32-Little-Endian-Byteorder: FF FE 00 00
180188
*/
181189

182-
var (length, encoding) = DetectPreambleLength(_stream);
183-
// not found or less than 2 byte read
184-
detectedEncoding = encoding;
185-
186-
return length;
187-
}
188-
189-
public static Encoding DetermineEncoding (EncodingOptions options, Encoding detectedEncoding)
190-
{
191-
return options?.Encoding != null
192-
? options.Encoding
193-
: detectedEncoding ?? options?.DefaultEncoding ?? Encoding.Default;
194-
}
195-
196-
public static (int length, Encoding? detectedEncoding) DetectPreambleLength (Stream stream)
197-
{
198190
if (!stream.CanSeek)
199191
{
200192
return (0, null);
201193
}
202194

203195
var originalPos = stream.Position;
204-
var buffer = new byte[4];
196+
Span<byte> buffer = stackalloc byte[4];
205197
_ = stream.Seek(0, SeekOrigin.Begin);
206-
var readBytes = stream.Read(buffer, 0, buffer.Length);
198+
var readBytes = stream.Read(buffer);
207199
_ = stream.Seek(originalPos, SeekOrigin.Begin);
208200

209201
if (readBytes >= 2)

src/LogExpert.Core/Classes/Log/PositionAwareStreamReaderSystem.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ private int GuessNewLineSequenceLength (StreamReader reader)
125125
var secondChar = reader.Read();
126126
if (secondChar == CHAR_LF) // check \n
127127
{
128-
return Encoding.GetByteCount("\r\n");
128+
// Use stackalloc or SpanOwner instead of string
129+
Span<char> newline = ['\r', '\n'];
130+
return Encoding.GetByteCount(newline);
131+
//return Encoding.GetByteCount("\r\n");
129132
}
130133
}
131134

132-
return Encoding.GetByteCount(((char)firstChar).ToString());
135+
Span<char> singleChar = [(char)firstChar];
136+
return Encoding.GetByteCount(singleChar);
133137
}
134138

135139
return 0;

src/LogExpert.Core/Interface/IConfigManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ public interface IConfigManager
151151
/// This method is supported only on Windows platforms.</remarks>
152152
/// <param name="fileName">The name of the file to add to the file history list. Comparison is case-insensitive.</param>
153153
void AddToFileHistory (string fileName);
154+
155+
/// <summary>
156+
/// Removes the specified file name from the file history list.
157+
/// </summary>
158+
/// <param name="fileName">The name of the file to remove from the file history list. Comparison is case-insensitive.</param>
159+
void RemoveFromFileHistory (string fileName);
154160

155161
/// <summary>
156162
/// Clears the list of recently opened files.

src/LogExpert.Tests/Services/LedIndicatorServiceTests.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Runtime.Versioning;
22

3-
using LogExpert.UI.Services;
3+
using LogExpert.UI.Interface.Services;
4+
using LogExpert.UI.Services.LedService;
45

56
using NUnit.Framework;
67

@@ -9,11 +10,12 @@ namespace LogExpert.Tests.Services;
910
[TestFixture]
1011
[Apartment(ApartmentState.STA)] // Required for UI components
1112
[SupportedOSPlatform("windows")]
12-
public class LedIndicatorServiceTests
13+
public class LedIndicatorServiceTests : IDisposable
1314
{
1415
private LedIndicatorService? _service;
1516
private ApplicationContext? _appContext;
1617
private WindowsFormsSynchronizationContext? _syncContext;
18+
private bool _disposed;
1719

1820
[SetUp]
1921
public void Setup ()
@@ -140,9 +142,9 @@ public void StartStop_DoesNotThrowException ()
140142
_service!.Initialize(Color.Blue);
141143

142144
// Act
143-
_service.Start();
145+
_service.StartService();
144146
Thread.Sleep(500); // Let timer tick a few times
145-
_service.Stop();
147+
_service.StopService();
146148

147149
// Assert - no exception
148150
Assert.That(true, Is.True, "Service started and stopped without exceptions");
@@ -192,7 +194,7 @@ public void Dispose_DisposesAllResources ()
192194
{
193195
// Arrange
194196
_service!.Initialize(Color.Blue);
195-
_service.Start();
197+
_service.StartService();
196198

197199
// Act
198200
_service.Dispose();
@@ -217,7 +219,7 @@ public void Start_WithoutInitialize_ThrowsException ()
217219
// Arrange - don't initialize
218220

219221
// Act & Assert
220-
_ = Assert.Throws<InvalidOperationException>(() => _service!.Start());
222+
_ = Assert.Throws<InvalidOperationException>(() => _service!.StartService());
221223
}
222224

223225
[Test]
@@ -316,4 +318,25 @@ public void GetIcon_WithSyncedState_ReturnsSyncedIcon ()
316318
// The icons should be different (synced has blue indicator on left side)
317319
Assert.That(iconSynced, Is.Not.EqualTo(iconNotSynced));
318320
}
321+
322+
public void Dispose ()
323+
{
324+
Dispose(true);
325+
GC.SuppressFinalize(this);
326+
}
327+
328+
protected virtual void Dispose (bool disposing)
329+
{
330+
if (_disposed)
331+
{
332+
return;
333+
}
334+
335+
if (disposing)
336+
{
337+
_service?.Dispose();
338+
}
339+
340+
_disposed = true;
341+
}
319342
}

0 commit comments

Comments
 (0)