Skip to content

Commit

Permalink
Fix CSV viewer not support formats other than UTF8
Browse files Browse the repository at this point in the history
This may have the disadvantage of slowing down the startup speed of large CSV files.
  • Loading branch information
emako committed Dec 13, 2024
1 parent 302aad4 commit 4ab015e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using UtfUnknown;

namespace QuickLook.Plugin.CsvViewer;

Expand All @@ -39,14 +41,17 @@ public CsvViewerPanel()
InitializeComponent();
}

public List<string[]> Rows { get; private set; } = new List<string[]>();
public List<string[]> Rows { get; private set; } = [];

public void LoadFile(string path)
{
const int limit = 10000;
var binded = false;

using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
var encoding = CharsetDetector.DetectFromFile(path).Detected?.Encoding ??
Encoding.Default;

using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding))
{
var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) { MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true };

Expand All @@ -58,7 +63,7 @@ public void LoadFile(string path)
var row = parser.Record;
if (row == null)
break;
row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, row);
row = Concat([$"{i++ + 1}".PadLeft(6)], row);

if (!binded)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="27.1.1" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4ab015e

Please sign in to comment.