Skip to content

Commit

Permalink
keep qurey text
Browse files Browse the repository at this point in the history
highlight title in user repo query
show all repo if user repo query is empty
  • Loading branch information
8LWXpg committed Jul 5, 2024
1 parent 25da1d5 commit 7bb4a97
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions GitHubRepo/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public List<Result> Query(Query query)
}

List<GitHubRepo> repos;
string user;
string target;

if (search.StartsWith('/'))
Expand All @@ -121,37 +122,62 @@ public List<Result> Query(Query query)
];
}

var cacheKey = _defaultUser;
user = _defaultUser;
target = search[1..];

repos = _cache.GetOrAdd(cacheKey, () => DefaultUserRepoQuery(cacheKey));
repos = _cache.GetOrAdd(user, () => DefaultUserRepoQuery(user));
}
else
{
var split = search.Split('/', 2);

var cacheKey = split[0];
user = split[0];
target = split[1];

repos = _cache.GetOrAdd(cacheKey, () => UserRepoQuery(cacheKey));
repos = _cache.GetOrAdd(user, () => UserRepoQuery(user));
}

List<Result> results = repos.ConvertAll(repo =>
if (string.IsNullOrEmpty(target))
{
return repos.ConvertAll(repo => new Result
{
Title = repo.FullName,
SubTitle = repo.Description,
QueryTextDisplay = search,
IcoPath = repo.Fork ? _iconFork : _iconRepo,
ContextData = new ResultData(repo.HtmlUrl),
Action = action => Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, repo.HtmlUrl),
});
}

List<Result> results = [];
foreach (GitHubRepo repo in repos)
{
MatchResult match = StringMatcher.FuzzySearch(target, repo.FullName.Split('/', 2)[1]);
return new Result
if (match.Score <= 0)
{
continue;
}

for (var i = 0; i < match.MatchData.Count; i++)
{
match.MatchData[i] += user.Length + 1;
}

results.Add(new Result
{
Title = repo.FullName,
SubTitle = repo.Description,
QueryTextDisplay = repo.FullName,
QueryTextDisplay = search,
IcoPath = repo.Fork ? _iconFork : _iconRepo,
Score = match.Score,
TitleHighlightData = match.MatchData,
ContextData = new ResultData(repo.HtmlUrl),
Action = action => Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, repo.HtmlUrl),
};
});
});
}

return string.IsNullOrEmpty(target) ? results : results.Where(r => r.Score > 0).ToList();
return results;

static List<GitHubRepo> UserRepoQuery(string user) => GitHub.UserRepoQuery(user).Result.Match(
ok: r => r,
Expand All @@ -171,7 +197,7 @@ public List<Result> Query(Query query, bool delayedExecution)
{
Title = repo.FullName,
SubTitle = repo.Description,
QueryTextDisplay = repo.FullName,
QueryTextDisplay = query.Search,
IcoPath = repo.Fork ? _iconFork : _iconRepo,
ContextData = new ResultData(repo.HtmlUrl),
Action = action => Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, repo.HtmlUrl),
Expand Down

0 comments on commit 7bb4a97

Please sign in to comment.