Skip to content

Commit

Permalink
renamed Top parameter to Take
Browse files Browse the repository at this point in the history
see #8
  • Loading branch information
h0lg committed Oct 25, 2024
1 parent 5d44caa commit 412903c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
16 changes: 8 additions & 8 deletions Shell/Program.CommandScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ private static (Option<IEnumerable<string>> channels, Option<IEnumerable<string>
}

private static ChannelScope[]? CreateChannelScopes(InvocationContext ctx, Option<IEnumerable<string>> aliases,
Option<ushort> top, Option<float> cacheHours)
=> ctx.Parsed(aliases)?.Select(alias => new ChannelScope(alias, ctx.Parsed(top), ctx.Parsed(cacheHours))).ToArray();
Option<ushort> take, Option<float> cacheHours)
=> ctx.Parsed(aliases)?.Select(alias => new ChannelScope(alias, ctx.Parsed(take), ctx.Parsed(cacheHours))).ToArray();

private static PlaylistScope[]? CreatePlaylistScopes(InvocationContext ctx, Option<IEnumerable<string>> playlists,
Option<ushort> top, Option<float> cacheHours)
=> ctx.Parsed(playlists)?.Select(playlist => new PlaylistScope(playlist, ctx.Parsed(top), ctx.Parsed(cacheHours))).ToArray();
Option<ushort> take, Option<float> cacheHours)
=> ctx.Parsed(playlists)?.Select(playlist => new PlaylistScope(playlist, ctx.Parsed(take), ctx.Parsed(cacheHours))).ToArray();

private static VideosScope? CreateVideosScope(InvocationContext ctx, Option<IEnumerable<string>> videos)
{
var ids = ctx.Parsed(videos);
return ids == null ? null : new(ids.ToList());
}

private static (Option<ushort> top, Option<float> cacheHours) AddPlaylistLikeCommandOptions(Command command)
private static (Option<ushort> take, Option<float> cacheHours) AddPlaylistLikeCommandOptions(Command command)
{
Option<ushort> top = new([topName, "-t"], () => 50,
Option<ushort> take = new([takeName, "-t"], () => 50,
"The number of videos to search, counted from the top of the playlist;"
+ " effectively limiting the search scope to the top partition of it."
+ " You may want to gradually increase this to include all videos in the list while you're refining your query."
Expand All @@ -48,9 +48,9 @@ private static (Option<ushort> top, Option<float> cacheHours) AddPlaylistLikeCom
+ " Note this doesn't apply to the videos themselves because their contents rarely change after upload."
+ $" Use '--{clearCacheCommand}' to clear videos associated with a playlist or channel if that's what you're after.");

command.AddOption(top);
command.AddOption(take);
command.AddOption(cacheHours);
return (top, cacheHours);
return (take, cacheHours);
}
}
}
2 changes: 1 addition & 1 deletion Shell/Program.HandleArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static partial class Program
{
static partial class CommandHandler
{
private const string clearCacheCommand = "clear-cache", topName = "--top", orderByName = "--order-by",
private const string clearCacheCommand = "clear-cache", takeName = "--take", orderByName = "--order-by",
quoteIdsStartingWithDash = " Note that if the video ID starts with a dash, you have to quote it"
+ @" like ""-1a2b3c4d5e"" or use the entire URL to prevent it from being misinterpreted as a command option.";

Expand Down
6 changes: 3 additions & 3 deletions Shell/Program.ListKeywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ private static Command ConfigureListKeywords(Func<ListKeywords, Task> listKeywor
command.AddAlias(Actions.listKeywords[..1]); // first character

var (channels, playlists, videos) = AddScopes(command);
(Option<ushort> top, Option<float> cacheHours) = AddPlaylistLikeCommandOptions(command);
(Option<ushort> take, Option<float> cacheHours) = AddPlaylistLikeCommandOptions(command);
(Option<bool> html, Option<string> fileOutputPath, Option<OutputCommand.Shows?> show) = AddOutputOptions(command);
Option<bool> saveAsRecent = AddSaveAsRecent(command);

command.SetHandler(async (ctx) => await listKeywords(
new ListKeywords
{
Channels = CreateChannelScopes(ctx, channels, top, cacheHours),
Playlists = CreatePlaylistScopes(ctx, playlists, top, cacheHours),
Channels = CreateChannelScopes(ctx, channels, take, cacheHours),
Playlists = CreatePlaylistScopes(ctx, playlists, take, cacheHours),
Videos = CreateVideosScope(ctx, videos)
}
.BindOuputOptions(ctx, html, fileOutputPath, show)
Expand Down
8 changes: 4 additions & 4 deletions Shell/Program.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ private static Command ConfigureSearch(Func<SearchCommand, Task> search)

var (channels, playlists, videos) = AddScopes(command);
(Option<IEnumerable<string>> query, Option<ushort> padding, Option<IEnumerable<SearchCommand.OrderOptions>> orderBy) = AddSearchCommandOptions(command);
(Option<ushort> top, Option<float> cacheHours) = AddPlaylistLikeCommandOptions(command);
(Option<ushort> take, Option<float> cacheHours) = AddPlaylistLikeCommandOptions(command);
(Option<bool> html, Option<string> fileOutputPath, Option<OutputCommand.Shows?> show) = AddOutputOptions(command);
Option<bool> saveAsRecent = AddSaveAsRecent(command);

command.SetHandler(async (ctx) => await search(
new SearchCommand
{
Channels = CreateChannelScopes(ctx, channels, top, cacheHours),
Playlists = CreatePlaylistScopes(ctx, playlists, top, cacheHours),
Channels = CreateChannelScopes(ctx, channels, take, cacheHours),
Playlists = CreatePlaylistScopes(ctx, playlists, take, cacheHours),
Videos = CreateVideosScope(ctx, videos)
}
.BindSearchOptions(ctx, query, padding, orderBy)
Expand Down Expand Up @@ -78,7 +78,7 @@ private static (Option<IEnumerable<string>> query, Option<ushort> padding, Optio
$"Order the video search results by '{nameof(SearchCommand.OrderOptions.uploaded)}'"
+ $" or '{nameof(SearchCommand.OrderOptions.score)}' with '{nameof(SearchCommand.OrderOptions.asc)}' for ascending."
+ $" The default is descending (i.e. latest respectively highest first) and by '{nameof(SearchCommand.OrderOptions.score)}'."
+ $" Note that the order is only applied to the results with the search scope itself being limited by the '{topName}' parameter for playlists."
+ $" Note that the order is only applied to the results with the search scope itself being limited by the '{takeName}' parameter for playlists."
+ " Note also that for un-cached videos, this option is ignored in favor of outputting matches as soon as they're found"
+ " - but simply repeating the search will hit the cache and return them in the requested order.")
{ AllowMultipleArgumentsPerToken = true };
Expand Down
10 changes: 5 additions & 5 deletions SubTubular/CommandScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public abstract class PlaylistLikeScope : CommandScope

// public options
public string Alias { get; set; }
public ushort Top { get; set; }
public ushort Take { get; set; }
public float CacheHours { get; set; }

protected PlaylistLikeScope(string alias, ushort top, float cacheHours)
protected PlaylistLikeScope(string alias, ushort take, float cacheHours)
{
Alias = alias.Trim();
Top = top;
Take = take;
CacheHours = cacheHours;
}

Expand All @@ -78,14 +78,14 @@ public override IEnumerable<string> Describe()
}

[Serializable]
public class PlaylistScope(string alias, ushort top, float cacheHours) : PlaylistLikeScope(alias, top, cacheHours)
public class PlaylistScope(string alias, ushort take, float cacheHours) : PlaylistLikeScope(alias, take, cacheHours)
{
internal const string StorageKeyPrefix = "playlist ";
protected override string KeyPrefix => StorageKeyPrefix;
}

[Serializable]
public class ChannelScope(string alias, ushort top, float cacheHours) : PlaylistLikeScope(alias, top, cacheHours)
public class ChannelScope(string alias, ushort take, float cacheHours) : PlaylistLikeScope(alias, take, cacheHours)
{
internal const string StorageKeyPrefix = "channel ";
protected override string KeyPrefix => StorageKeyPrefix;
Expand Down
8 changes: 4 additions & 4 deletions SubTubular/Youtube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private async IAsyncEnumerable<VideoSearchResult> SearchPlaylistAsync(SearchComm
var index = await videoIndexRepo.GetAsync(storageKey);
if (index == null) index = videoIndexRepo.Build(storageKey);
var playlist = await RefreshPlaylistAsync(scope, cancellation);
var videoIds = playlist.GetVideoIds().Take(scope.Top).ToArray();
var videoIds = playlist.GetVideoIds().Take(scope.Take).ToArray();
scope.SetVideos(videoIds);
var indexedVideoIds = index.GetIndexed(videoIds);

Expand Down Expand Up @@ -148,15 +148,15 @@ private async Task<Playlist> RefreshPlaylistAsync(PlaylistLikeScope scope, Cance

// return fresh playlist with sufficient videos loaded
if (DateTime.UtcNow.AddHours(-Math.Abs(scope.CacheHours)) <= playlist.Loaded
&& scope.Top <= playlist.GetVideoIds().Count()) return playlist;
&& scope.Take <= playlist.GetVideoIds().Count()) return playlist;

// playlist cache is outdated or lacking sufficient videos
scope.Report(VideoList.Status.refreshing);

try
{
// load and update videos in playlist while keeping existing video info
var freshVideos = await GetVideos(scope, cancellation).CollectAsync(scope.Top);
var freshVideos = await GetVideos(scope, cancellation).CollectAsync(scope.Take);
playlist.Loaded = DateTime.UtcNow;
playlist.AddVideoIds(freshVideos.Select(v => v.Id.Value).ToArray());
await dataStore.SetAsync(scope.StorageKey, playlist);
Expand Down Expand Up @@ -315,7 +315,7 @@ async Task GetKeywords(string videoId, CommandScope scope)
Task.Run(async () =>
{
var playlist = await RefreshPlaylistAsync(scope, cancellation);
var videoIds = playlist.GetVideoIds().Take(scope.Top).ToArray();
var videoIds = playlist.GetVideoIds().Take(scope.Take).ToArray();
scope.SetVideos(videoIds);
scope.Report(VideoList.Status.searching);
foreach (var videoId in videoIds) await GetKeywords(videoId, scope);
Expand Down
14 changes: 7 additions & 7 deletions Ui/Scope.fs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module Scope =
| ValidationSucceeded
| ValidationFailed of exn
| ToggleSettings of bool
| TopChanged of float option
| TakeChanged of float option
| CacheHoursChanged of float option
| Remove
| RemoveVideo of string
Expand Down Expand Up @@ -238,13 +238,13 @@ module Scope =
/// creates a scope on user command, marking it as Added for it to be focused
let add scopeType =
let aliases = ""
let top = uint16 50
let take = uint16 50
let cacheHours = float32 24

let scope =
match scopeType with
| IsChannel -> ChannelScope(aliases, top, cacheHours) :> CommandScope
| IsPlaylist -> PlaylistScope(aliases, top, cacheHours)
| IsChannel -> ChannelScope(aliases, take, cacheHours) :> CommandScope
| IsPlaylist -> PlaylistScope(aliases, take, cacheHours)
| IsVideos -> VideosScope(VideosInput.splitAndClean aliases)

init scope aliases true
Expand Down Expand Up @@ -286,9 +286,9 @@ module Scope =
| ValidationSucceeded -> { model with Error = null }, Cmd.none, DoNothing
| ValidationFailed exn -> { model with Error = exn.Message }, Cmd.none, DoNothing

| TopChanged top ->
| TakeChanged take ->
match model.Scope with
| PlaylistLike scope -> scope.Top <- top |> Option.defaultValue 50 |> uint16
| PlaylistLike scope -> scope.Take <- take |> Option.defaultValue 50 |> uint16
| _ -> ()

model, Cmd.none, DoNothing
Expand Down Expand Up @@ -455,7 +455,7 @@ module Scope =
(HStack(5) {
Label "search top"

NumericUpDown(0, float UInt16.MaxValue, Some(float playlistLike.Top), TopChanged)
NumericUpDown(0, float UInt16.MaxValue, Some(float playlistLike.Take), TakeChanged)
.formatString("F0")
.tooltip ("number of videos to search")

Expand Down

0 comments on commit 412903c

Please sign in to comment.