Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements subpagination for ddgr #22

Merged
merged 10 commits into from Nov 23, 2017
Merged

Implements subpagination for ddgr #22

merged 10 commits into from Nov 23, 2017

Conversation

mosegontar
Copy link
Collaborator

Here's a provisional implementation of subpagination for ddgr.

Pagination is set by using the -n (or --num) option which takes an integer N to paginate the results by. For example: ddgr python -n 5 will paginate by 5 results at a time.

If the number of fetched results left to display is less than the pagination value, new results from the next Ddg results page are fetched and appended to the list of results already returned. Use of the p command in the omniprompt will show the previous N results, where N is the pagination value.

If the f command is used in the omni prompt, the index is reset to 0 so that the first set of results are displayed.

Please let me know if this is in line with what you are thinking/what changes should be made.

@mosegontar mosegontar changed the title This PR implements subpagination for ddgr Implements subpagination for ddgr Nov 19, 2017
@jarun
Copy link
Owner

jarun commented Nov 19, 2017

I think we are on the same page. When we press p, we never fetch results, am I right?

I will review this in 1/2 days.

@jarun
Copy link
Owner

jarun commented Nov 19, 2017

The indices do not start from 0. We have to hide (and store) the true first real index of the current page (as offset)) and start from 1 in each page. When users enters the visible index to open a link in browser, we have to add that to current offset.

If we don't do that, we will see this:

ddgr (? for help) n

 (41) Hello World:) (2013) - IMDb  [www.imdb.com]
Directed by Rafael Mathé, Etienne Larraguetta. With Mikael Alhawi, Remi Baconnet, Jean-Paul Bezzina, Tatiana Campana. In a close future, a private company developed a technology aimed at boosting
our brain capacity. But it requires from its clients to store their memory data on one single server.

 (42) Hello, World | whitehouse.gov  [obamawhitehouse.archives.gov]
Though it wasn't true for President Obama when he became the first president to write a line of code back in December, it's traditional for the first program a new coder writes to be one that prints
out the message "Hello, world."

 (43) Hello, world! - The complete C# Tutorial  [csharp.net-tutorials.com]
A chapter on Hello, world! in the complete Microsoft C# .NET tutorial using Visual Studio Express 2012

 (44) Hello World Personalized Baby Book | I See Me!  [www.iseeme.com]
Hello, World! makes a perfect gift to welcome a new baby into the world! Parents will treasure this adorable keepsake book, as it is personalized with the new baby's name and photograph.

 (45) Lesson: The "Hello World!" Application (The Java™ Tutorials ...  [docs.oracle.com]
This beginner Java tutorial describes getting started with Java and setting up your Netbeans IDE

 (1) The History of Hello World - The Software Guild  [www.thesoftwareguild.com]
Hello World is a program used to simply explain core structure and syntax of a programming language. Learn about the history, modern use, and more here!

 (2) Hello-Hello: Best Language Learning apps for iPad, iPhone ...  [hello-hello.com]
Learn a new language anytime, anywhere with Hello-Hello. Top-selling apps for schools, libraries, companies and more!

 (3) Hello World - CircleCI  [circleci.com]
Hello World. This document describes how to configure your project to run on CircleCI 2.0. Prerequisites. If this will be your very first CircleCI project, complete the steps in the Sign Up & Try
CircleCI document.

 (4) "Hello, world." - vndb.org  [vndb.org]
Title "Hello, world." Aliases: ハローワールド: Length: Very long (> 50 hours) Developer: Nitroplus: Publishers Nitroplus: Relations: Shares characters

 (5) Hello World (TV Series 2016- ) - IMDb  [www.imdb.com]
The leading information resource for the entertainment industry. Find industry contacts & talent representation. Manage your photos, credits, & more

ddgr (? for help)

@jarun
Copy link
Owner

jarun commented Nov 19, 2017

Why don't we update the index when we add it to our cache?

@jarun
Copy link
Owner

jarun commented Nov 19, 2017

We try to fetch whenever we hit the last page. As we are caching the results, can we store the index (initialized to a negative value) to indicate which is our last index the first time we hit it? That way we can save the redundant fetch every time user reaches the last page.

@jarun
Copy link
Owner

jarun commented Nov 20, 2017

Self note: document the option in README, manpage, add to completion-scripts. (owner @jarun)

@jarun
Copy link
Owner

jarun commented Nov 20, 2017

It would be a good idea to default num to 10. 30/40 results per page needs scrolling up/down every time.

@jarun
Copy link
Owner

jarun commented Nov 20, 2017

Let n=0 disable num as a fallback.

@mosegontar
Copy link
Collaborator Author

I've made the following updates

  • When paginating and fetching a new page of results, we update DdgCmd._urltable dict with the new set of results. The key for each result is equal to its index value + the previous total number of results retrieved. So if page 1 has 30 results, the 1st result on Page 2 will be added to the dict as 31.
  • Updated do_open and cmdloop to calculate correct index to open when paginating.
  • Added a display_index parameter to Result.print() to set the displayed index value instead of the true index value.
  • Set default --num value to 10.

There is a significant item remaining to be fixed: if pagination values are large, they can cause the index to increase to a value larger than the size of the results, and this causes problems with the output.

@jarun
Copy link
Owner

jarun commented Nov 23, 2017

Self-notes (owner @jarun):

  • I'll take up any review comments I might add.
  • We need to fix the large num issue.
  • Even if we are on the first page, f/p refreshes the list. Need to fix that.

@@ -1139,18 +1140,25 @@ class DdgCmd(object):
parser = DdgParser()
parser.feed(page.text)

self.results = parser.results
if self.options.pagination:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this getting set???

@@ -1267,8 +1290,15 @@ class DdgCmd(object):
@no_argument
def do_next(self):
# If no results were fetched last time, we have hit the last page already
if self._ddg_url._qrycnt == 0:
if self._ddg_url._qrycnt == 0 and self.index >= len(self.results):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this additional condition do???

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will keep having these issues in deciphering stuff if we never get a single line of code comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You add the check self.options.pagination in every other place and then fuse this kind of a condition without any check???

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this line is the issue with the huge pagination value.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have hidden it by limiting n to 25. If anyone is trying to set n greater than that he is trying to automate stuff. He should patch the code for himself. ;)

if self.options.pagination:
self.results.extend(parser.results)
offset = len(self._urltable)
for r in self.results:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are re-indexing results from offset??? Why???

@jarun jarun merged commit 82e05b9 into jarun:master Nov 23, 2017
@jarun
Copy link
Owner

jarun commented Nov 23, 2017

I'll probably have to do a massive rework on this. Anyway...

print('No results.', file=sys.stderr)
else:
sys.stderr.write(prelude)
for r in self.results:
r.print()
for i, r in enumerate(results):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken for n=0

@jarun
Copy link
Owner

jarun commented Nov 23, 2017

I think we are reasonably good now! 👍

print(json.dumps(results_object, indent=2, sort_keys=True, ensure_ascii=False))
else:
# Regular output
if not self.results:
if not self.results or self.index > len(self.results):
Copy link
Owner

@jarun jarun Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the second check be self.index >= len(self.results)?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of the check completely.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants