Bring history back

1. bring history back, disabled in
56d08663410916df0a4e408da6e4af3d2a2722c0
2. fix #632 #722
3. hotkey: ctrl+H
This commit is contained in:
bao-qian
2016-06-23 22:17:47 +01:00
parent b589a1a13e
commit 15c5e9833a
5 changed files with 231 additions and 237 deletions

View File

@@ -0,0 +1,45 @@
using System;
namespace Wox.Storage
{
public class HistoryItem
{
public string Query { get; set; }
public DateTime ExecutedDateTime { get; set; }
public string GetTimeAgo()
{
return DateTimeAgo(ExecutedDateTime);
}
private string DateTimeAgo(DateTime dt)
{
var span = DateTime.Now - dt;
if (span.Days > 365)
{
int years = (span.Days / 365);
if (span.Days % 365 != 0)
years += 1;
return $"about {years} {(years == 1 ? "year" : "years")} ago";
}
if (span.Days > 30)
{
int months = (span.Days / 30);
if (span.Days % 31 != 0)
months += 1;
return $"about {months} {(months == 1 ? "month" : "months")} ago";
}
if (span.Days > 0)
return $"about {span.Days} {(span.Days == 1 ? "day" : "days")} ago";
if (span.Hours > 0)
return $"about {span.Hours} {(span.Hours == 1 ? "hour" : "hours")} ago";
if (span.Minutes > 0)
return $"about {span.Minutes} {(span.Minutes == 1 ? "minute" : "minutes")} ago";
if (span.Seconds > 5)
return $"about {span.Seconds} seconds ago";
if (span.Seconds <= 5)
return "just now";
return string.Empty;
}
}
}

View File

@@ -2,111 +2,36 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
namespace Wox.Storage
{
public class QueryHistory
public class History
{
public List<HistoryItem> History = new List<HistoryItem>();
public List<HistoryItem> Items { get; set; } = new List<HistoryItem>();
private int MaxHistory = 300;
private int cursor;
public static PluginMetadata MetaData { get; } = new PluginMetadata
{ ID = "Query history", Name = "Query history" };
public HistoryItem Previous()
{
if (History.Count == 0 || cursor == 0) return null;
return History[--cursor];
}
public HistoryItem Next()
{
if (History.Count == 0 || cursor >= History.Count - 1) return null;
return History[++cursor];
}
public void Reset()
{
cursor = History.Count;
}
private int _maxHistory = 300;
public void Add(string query)
{
if (string.IsNullOrEmpty(query)) return;
if (History.Count > MaxHistory)
if (Items.Count > _maxHistory)
{
History.RemoveAt(0);
Items.RemoveAt(0);
}
if (History.Count > 0 && History.Last().Query == query)
if (Items.Count > 0 && Items.Last().Query == query)
{
History.Last().ExecutedDateTime = DateTime.Now;
Items.Last().ExecutedDateTime = DateTime.Now;
}
else
{
History.Add(new HistoryItem
Items.Add(new HistoryItem
{
Query = query,
ExecutedDateTime = DateTime.Now
});
}
Reset();
}
public List<HistoryItem> GetHistory()
{
return History.OrderByDescending(o => o.ExecutedDateTime).ToList();
}
}
public class HistoryItem
{
public string Query { get; set; }
public DateTime ExecutedDateTime { get; set; }
public string GetTimeAgo()
{
return DateTimeAgo(ExecutedDateTime);
}
private string DateTimeAgo(DateTime dt)
{
TimeSpan span = DateTime.Now - dt;
if (span.Days > 365)
{
int years = (span.Days / 365);
if (span.Days % 365 != 0)
years += 1;
return String.Format("about {0} {1} ago",
years, years == 1 ? "year" : "years");
}
if (span.Days > 30)
{
int months = (span.Days / 30);
if (span.Days % 31 != 0)
months += 1;
return String.Format("about {0} {1} ago",
months, months == 1 ? "month" : "months");
}
if (span.Days > 0)
return String.Format("about {0} {1} ago",
span.Days, span.Days == 1 ? "day" : "days");
if (span.Hours > 0)
return String.Format("about {0} {1} ago",
span.Hours, span.Hours == 1 ? "hour" : "hours");
if (span.Minutes > 0)
return String.Format("about {0} {1} ago",
span.Minutes, span.Minutes == 1 ? "minute" : "minutes");
if (span.Seconds > 5)
return String.Format("about {0} seconds ago", span.Seconds);
if (span.Seconds <= 5)
return "just now";
return string.Empty;
}
}
}