diff --git a/Source/TailBlazer.Domain/FileHandling/FileInfoEx.cs b/Source/TailBlazer.Domain/FileHandling/FileInfoEx.cs index 66b812db..5cc24ec1 100644 --- a/Source/TailBlazer.Domain/FileHandling/FileInfoEx.cs +++ b/Source/TailBlazer.Domain/FileHandling/FileInfoEx.cs @@ -125,7 +125,6 @@ public static IObservable Match(this IObservable }).Switch(); } - public static IEnumerable ReadLines(this FileInfo source, int[] lines, Func isEndOfTail = null) { using (var stream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) @@ -145,18 +144,23 @@ public static IEnumerable ReadLines(this FileInfo source, int[] lines, Fun } } - public static IEnumerable ReadLines(this FileInfo source, IEnumerable lines, Func selector) + public static IEnumerable ReadLines(this FileInfo source, IEnumerable lines, Func selector, Encoding encoding=null) { + encoding = encoding ?? Encoding.Default; + + //TODO: This assumes a windows file whereas Unix / max have length=1. + //This is accounted for on the LineIndexer but we need to pass the length and current encoding here () + const int delimiterLength = 2; + using (var stream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { - foreach (var lineIndex in lines.OrderBy(l=>l.Index)) { - var data = new byte[lineIndex.Size-2]; + var data = new byte[lineIndex.Size- delimiterLength]; stream.Seek(lineIndex.Start, SeekOrigin.Begin); stream.Read(data, 0, data.Length); - var result = Encoding.Default.GetString(data); + var result = encoding.GetString(data); yield return selector(lineIndex, result); } } diff --git a/Source/TailBlazer.Domain/FileHandling/LineIndexer.cs b/Source/TailBlazer.Domain/FileHandling/LineIndexer.cs index 333aba58..2a5c2925 100644 --- a/Source/TailBlazer.Domain/FileHandling/LineIndexer.cs +++ b/Source/TailBlazer.Domain/FileHandling/LineIndexer.cs @@ -49,6 +49,8 @@ public IEnumerable ReadToEnd() _postion = _postion + line.Length + _delimiterSize; yield return _postion; } + + } public void Dispose() diff --git a/Source/TailBlazer.Domain/FileHandling/LineMatcher.cs b/Source/TailBlazer.Domain/FileHandling/LineMatcher.cs index 1375e804..608666dd 100644 --- a/Source/TailBlazer.Domain/FileHandling/LineMatcher.cs +++ b/Source/TailBlazer.Domain/FileHandling/LineMatcher.cs @@ -13,9 +13,7 @@ public class LineMatcher: IDisposable private readonly StreamReader _reader; public Encoding Encoding { get; } - public int TotalCount => _index; - public int Count => _matches; private int _index = -1; @@ -29,6 +27,9 @@ public LineMatcher(FileInfo info, Func predicate, Encoding encoding _stream = File.Open(info.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite); _reader = new StreamReader(_stream, Encoding, true); + + //TODO 1: Get current encoding from _reader.CurrentEncoding and expose so cusumers can read the lines with the same encoding + //TODO 2: Expose line delimiter length so it can be correctly removed from when re-reading a line } public IEnumerable ScanToEnd() @@ -40,11 +41,9 @@ public IEnumerable ScanToEnd() while ((line = _reader.ReadLine()) != null) { _index++; - if (_predicate(line)) - { - _matches++; - yield return _index; - } + if (!_predicate(line)) continue; + _matches++; + yield return _index; } } diff --git a/Source/TailBlazer/Views/VirtualScrollPanel.cs b/Source/TailBlazer/Views/VirtualScrollPanel.cs index e18e5fe8..26019602 100644 --- a/Source/TailBlazer/Views/VirtualScrollPanel.cs +++ b/Source/TailBlazer/Views/VirtualScrollPanel.cs @@ -8,6 +8,11 @@ namespace TailBlazer.Views { + /// + /// This is adapted (butchered!) from VirtualWrapPanel in https://github.com/samueldjack/VirtualCollection + /// + /// See http://blog.hibernatingrhinos.com/12515/implementing-a-virtualizingwrappanel + /// public class VirtualScrollPanel : VirtualizingPanel, IScrollInfo { private const double ScrollLineAmount = 16.0;