Subject: Re: Support of range selection in the downloader
From: Ondřej Jirman
Date: Thu, 15 Oct 2020 10:36:06 +0200
Hello,

On Thu, Oct 15, 2020 at 09:22:05AM +0200, Aurélien ANTUNES wrote:
> Hi,
> 
> Thanks, just tested and it works great.
> 
> Also the off by one was actually intentional, since we are replacing one
> token with n tokens, we only need to grow the list by n-1.
> But that's not really a concern, since nums is already a little bigger than
> needed.

I see, thanks for explanation. Also, thanks for testing and for the patch. :)

kind regards,
	o.

> Have a good day.
> 
> On Thu, 15 Oct 2020 at 01:38, Ondřej Jirman <megatools@megous.com> wrote:
> 
> > Hi,
> >
> > On Thu, Oct 15, 2020 at 12:45:41AM +0200, Aurélien ANTUNES wrote:
> > > Hi,
> > >
> > > I have made a little patch to support selecting a range in the
> > downloader.
> > > I am not experienced with GObject, so I hope it's ok.
> > >
> > > And thanks for developing megatools.
> >
> > Thank you. :) I applied it and cleaned it up a bit. Please check out the
> > cleanup
> > and test if it still works.
> >
> > thank you and regards,
> >         o.
> >
> > >
> > >
> > >
> > >
> > > From da0f61965967958404877ca034d433fe80edf9b7 Mon Sep 17 00:00:00 2001
> > > From: aurelien <aurelien.antunes@gmail.com>
> > > Date: Thu, 15 Oct 2020 00:29:26 +0200
> > > Subject: [PATCH] Add range selection to the downloader
> > >
> > > ---
> > >  tools/dl.c | 22 ++++++++++++++++++----
> > >  1 file changed, 18 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/tools/dl.c b/tools/dl.c
> > > index 62df8dc..5026e6a 100644
> > > --- a/tools/dl.c
> > > +++ b/tools/dl.c
> > > @@ -137,14 +137,28 @@ static gint* parse_number_list(const gchar* input,
> > > gint* count)
> > >   return NULL;
> > >
> > >   gchar** tokens = g_regex_split(re, input, 0);
> > > - guint i = 0, n_tokens = g_strv_length(tokens);
> > > + guint i = 0, j = 0, n_tokens = g_strv_length(tokens), n_nums =
> > n_tokens;
> > >   gint* nums = g_new0(gint, n_tokens);
> > >
> > >   *count = 0;
> > > -        while (i < n_tokens) {
> > > + while (i < n_tokens) {
> > >   if (g_regex_match_simple("^\\d{1,7}$", tokens[i], 0, 0)) {
> > >   nums[*count] = atoi(tokens[i]);
> > >   *count += 1;
> > > + } else if (g_regex_match_simple("^\\d{1,7}-\\d{1,7}$", tokens[i], 0,
> > 0)) {
> > > + gchar** tokens_range = g_regex_split_simple("-", tokens[i], 0, 0);
> > > + int min = atoi(tokens_range[0]), max = atoi(tokens_range[1]);
> > > + if (min < max) {
> > > + n_nums += max - min;
> > > + nums = g_renew(gint, nums, n_nums);
> > > + for (j = min; j <= max; ++j) {
> > > + nums[*count] = j;
> > > + *count += 1;
> > > + }
> > > + } else {
> > > + g_printerr("WARNING: Skipping empty range '%s'\n", tokens[i]);
> > > + }
> > > + g_strfreev(tokens_range);
> > >   } else {
> > >   g_printerr("WARNING: Skipping non-numeric value '%s'\n", tokens[i]);
> > >   }
> > > @@ -160,7 +174,7 @@ static GSList *prompt_and_filter_nodes(GSList *nodes)
> > >  {
> > >   GSList *chosen_nodes = NULL;
> > >
> > > -        gc_free gchar* input = tool_prompt_input();
> > > + gc_free gchar* input = tool_prompt_input();
> > >   if (input == NULL)
> > >   return g_slist_copy(nodes);
> > >
> > > @@ -276,7 +290,7 @@ static GSList* pick_nodes(void)
> > >   position++;
> > >   }
> > >
> > > - g_print("Enter numbers of files or folders to download separated by
> > > spaces (or type 'all' to download everything):\n> ");
> > > + g_print("Enter numbers of files or folders to download separated by
> > > spaces (or type 'all' to download everything, or a range with two numbers
> > > separated by '-'):\n> ");
> > >
> > >   chosen_nodes = prompt_and_filter_nodes(nodes);
> > >
> > > --
> > > 2.28.0
> >
> >
> >