mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-13 19:19:10 +00:00
Code Cleanup
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
<UserControl x:Class="QuickLook.Plugin.CsvViewer.CsvViewerPanel"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:QuickLook.Plugin.CsvViewer"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
x:Name="csvViewer"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
d:DesignHeight="300"
|
||||
d:DesignWidth="300"
|
||||
mc:Ignorable="d">
|
||||
<Grid>
|
||||
<DataGrid x:Name="dataGrid" AlternationCount="2" BorderThickness="0" Background="Transparent" Foreground="{DynamicResource WindowTextForeground}"
|
||||
RowBackground="Transparent" IsReadOnly="True" HeadersVisibility="None" AutoGenerateColumns="False"
|
||||
CanUserReorderColumns="False" ItemsSource="{Binding Path=Rows,ElementName=csvViewer}"
|
||||
AlternatingRowBackground="#1900BFFF" HorizontalGridLinesBrush="#19000000" VerticalGridLinesBrush="#19000000" />
|
||||
|
||||
<DataGrid x:Name="dataGrid"
|
||||
AlternatingRowBackground="#1900BFFF"
|
||||
AlternationCount="2"
|
||||
AutoGenerateColumns="False"
|
||||
Background="Transparent"
|
||||
BorderThickness="0"
|
||||
CanUserReorderColumns="False"
|
||||
Foreground="{DynamicResource WindowTextForeground}"
|
||||
HeadersVisibility="None"
|
||||
HorizontalGridLinesBrush="#19000000"
|
||||
IsReadOnly="True"
|
||||
ItemsSource="{Binding Path=Rows, ElementName=csvViewer}"
|
||||
RowBackground="Transparent"
|
||||
VerticalGridLinesBrush="#19000000" />
|
||||
</Grid>
|
||||
</UserControl>
|
||||
|
@@ -1,20 +1,22 @@
|
||||
// Copyright © 2017 Paddy Xu
|
||||
//
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
@@ -24,82 +26,79 @@ using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
|
||||
namespace QuickLook.Plugin.CsvViewer
|
||||
namespace QuickLook.Plugin.CsvViewer;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for CsvViewerPanel.xaml
|
||||
/// </summary>
|
||||
public partial class CsvViewerPanel : UserControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for CsvViewerPanel.xaml
|
||||
/// </summary>
|
||||
public partial class CsvViewerPanel : UserControl
|
||||
public CsvViewerPanel()
|
||||
{
|
||||
public CsvViewerPanel()
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public List<string[]> Rows { get; private set; } = new List<string[]>();
|
||||
|
||||
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)))
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) { MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true };
|
||||
|
||||
public List<string[]> Rows { get; private set; } = new List<string[]>();
|
||||
|
||||
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)))
|
||||
using (var parser = new CsvParser(sr, conf))
|
||||
{
|
||||
var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) {MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true};
|
||||
|
||||
using (var parser = new CsvParser(sr, conf))
|
||||
var i = 0;
|
||||
while (parser.Read())
|
||||
{
|
||||
var i = 0;
|
||||
while (parser.Read())
|
||||
var row = parser.Record;
|
||||
if (row == null)
|
||||
break;
|
||||
row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, row);
|
||||
|
||||
if (!binded)
|
||||
{
|
||||
var row = parser.Record;
|
||||
if (row == null)
|
||||
break;
|
||||
row = Concat(new[] {$"{i++ + 1}".PadLeft(6)}, row);
|
||||
|
||||
if (!binded)
|
||||
{
|
||||
SetupColumnBinding(row.Length);
|
||||
binded = true;
|
||||
}
|
||||
|
||||
if (i > limit)
|
||||
{
|
||||
Rows.Add(Enumerable.Repeat("...", row.Length).ToArray());
|
||||
break;
|
||||
}
|
||||
|
||||
Rows.Add(row);
|
||||
SetupColumnBinding(row.Length);
|
||||
binded = true;
|
||||
}
|
||||
|
||||
if (i > limit)
|
||||
{
|
||||
Rows.Add(Enumerable.Repeat("...", row.Length).ToArray());
|
||||
break;
|
||||
}
|
||||
|
||||
Rows.Add(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupColumnBinding(int rowLength)
|
||||
private void SetupColumnBinding(int rowLength)
|
||||
{
|
||||
for (var i = 0; i < rowLength; i++)
|
||||
{
|
||||
for (var i = 0; i < rowLength; i++)
|
||||
var col = new DataGridTextColumn
|
||||
{
|
||||
var col = new DataGridTextColumn
|
||||
{
|
||||
FontFamily = new FontFamily("Consolas"),
|
||||
FontWeight = FontWeight.FromOpenTypeWeight(i == 0 ? 700 : 400),
|
||||
Binding = new Binding($"[{i}]")
|
||||
};
|
||||
dataGrid.Columns.Add(col);
|
||||
}
|
||||
}
|
||||
|
||||
public static T[] Concat<T>(T[] x, T[] y)
|
||||
{
|
||||
if (x == null) throw new ArgumentNullException("x");
|
||||
if (y == null) throw new ArgumentNullException("y");
|
||||
var oldLen = x.Length;
|
||||
Array.Resize(ref x, x.Length + y.Length);
|
||||
Array.Copy(y, 0, x, oldLen, y.Length);
|
||||
return x;
|
||||
FontFamily = new FontFamily("Consolas"),
|
||||
FontWeight = FontWeight.FromOpenTypeWeight(i == 0 ? 700 : 400),
|
||||
Binding = new Binding($"[{i}]")
|
||||
};
|
||||
dataGrid.Columns.Add(col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static T[] Concat<T>(T[] x, T[] y)
|
||||
{
|
||||
if (x == null) throw new ArgumentNullException("x");
|
||||
if (y == null) throw new ArgumentNullException("y");
|
||||
var oldLen = x.Length;
|
||||
Array.Resize(ref x, x.Length + y.Length);
|
||||
Array.Copy(y, 0, x, oldLen, y.Length);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
@@ -1,63 +1,62 @@
|
||||
// Copyright © 2017 Paddy Xu
|
||||
//
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using QuickLook.Common.Plugin;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using QuickLook.Common.Plugin;
|
||||
|
||||
namespace QuickLook.Plugin.CsvViewer
|
||||
namespace QuickLook.Plugin.CsvViewer;
|
||||
|
||||
public class Plugin : IViewer
|
||||
{
|
||||
public class Plugin : IViewer
|
||||
private CsvViewerPanel _panel;
|
||||
|
||||
public int Priority => 0;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
private CsvViewerPanel _panel;
|
||||
}
|
||||
|
||||
public int Priority => 0;
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
return !Directory.Exists(path) && (path.ToLower().EndsWith(".csv") || path.ToLower().EndsWith(".tsv"));
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
}
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
context.PreferredSize = new Size { Width = 1000, Height = 700 };
|
||||
}
|
||||
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
return !Directory.Exists(path) && (path.ToLower().EndsWith(".csv") || path.ToLower().EndsWith(".tsv"));
|
||||
}
|
||||
public void View(string path, ContextObject context)
|
||||
{
|
||||
_panel = new CsvViewerPanel();
|
||||
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
context.PreferredSize = new Size {Width = 1000, Height = 700};
|
||||
}
|
||||
context.ViewerContent = _panel;
|
||||
context.Title = $"{Path.GetFileName(path)}";
|
||||
_panel.LoadFile(path);
|
||||
|
||||
public void View(string path, ContextObject context)
|
||||
{
|
||||
_panel = new CsvViewerPanel();
|
||||
context.IsBusy = false;
|
||||
}
|
||||
|
||||
context.ViewerContent = _panel;
|
||||
context.Title = $"{Path.GetFileName(path)}";
|
||||
_panel.LoadFile(path);
|
||||
public void Cleanup()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
context.IsBusy = false;
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
_panel = null;
|
||||
}
|
||||
_panel = null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user