firecat53 / urlscan

Mutt and terminal url selector (similar to urlview)
GNU General Public License v2.0
213 stars 37 forks source link

Add option for reverse sort #90

Closed pigmonkey closed 4 years ago

pigmonkey commented 5 years ago

I would like to be able to pass urlscan a flag telling it to return the URLs in the reverse order.

For example, given the following file:

$ cat urls.txt
https://github.com/firecat53/urlscan
https://github.com/
https://www.google.com/

urlscan will currently list the URLs in the same order as were given in the file:

$ urlscan -c urls.txt
[1]   < https://github.com/firecat53/urlscan
[2]   < https://github.com/ 
[3]   < https://www.google.com/

Most of the time this is the desired behavior, but some of the time I'd rather the URLs be returned like this:

[1]   < https://www.google.com/
[2]   < https://github.com/ 
[3]   < https://github.com/firecat53/urlscan

This is most often useful when using urlscan on terminal output (such as via tmux-urlscan). In this case, I'm trying to get to the most recent URL, which is at the bottom of the terminal. Telling urlscan to reverse sort the URLs allows me to get to this most recent URL without me having to navigate the list.

firecat53 commented 5 years ago

You know that you can get to the bottom of the list with just a G? Is that any slower than passing a separate flag?

pigmonkey commented 5 years ago

Yes, for my specific use case I would configure tmux-urlscan to always pass this reverse flag, so this would save me two keystrokes (shift, g) every time I use urlscan.

brett commented 5 years ago

It might be nice to have a config option to specify the default sort order, too.

firecat53 commented 5 years ago

I'm on my phone right now, but one thing to consider with sort order is how to handle it relative to message chunks. Pick a complex business email from someplace and give me some ideas about how you envision sorting works when the message is broken into chunks by urlscan with several URLs per chunk. It's not as easy as a straight reverse sort.

pigmonkey commented 5 years ago

My initial thought is that it would just reverse the display of the chunks. I think this file demonstrates what you mean:

$ cat urls.txt
This file might be an email. Here are two URLs:

https://github.com/firecat53/urlscan/
https://github.com/

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Maybe there are some [markdown links](https://www.google.com) as well.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

And some people send <a href="http://www.google.com">html email</a>.

Currently urlscan is going to do something like this:

$ urlscan urls.txt
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This file might be an email. Here are two URLs:

<URL> [1]
<URL> [2]

...

[1]   < https://github.com/firecat53/urlscan/                                                                                                                                                                       >
[2]   < https://github.com/                                                                                                                                                                                         >

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

...
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.

Maybe there are some [markdown links](<URL> [3]) as well.

...

[3]   < https://www.google.com                                                                                                                                                                                      >

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

...
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.

And some people send <a href="<URL> [4]">html email</a>.

[4]   < http://www.google.com 

I could imagine the reverse flag swapping the chunk order, something like this:

$ urlscan --reverse urls.txt

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

...
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.

And some people send <a href="<URL> [1]">html email</a>.

[1]   < http://www.google.com 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

...
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.

Maybe there are some [markdown links](<URL> [2]) as well.

...

[2]   < https://www.google.com                                                                                                                                                                                      >

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This file might be an email. Here are two URLs:

<URL> [3]
<URL> [4]

...

[3]   < https://github.com/firecat53/urlscan/                                                                                                                                                                       >
[4]   < https://github.com/                                                                                                                                                                                         >

However, I definitely think the reverse sort is less useful in this context than it is when parsing terminal output, where you can assume that the most recent URLs (and thus the most likely to be wanted) are at the bottom. I think it would be reasonable to say that the reverse flag is only available in conjunction with --compact (or that it necessarily implies --compact).

firecat53 commented 4 years ago

Give the reverse branch a try. -N does the same thing as -n but prints out the URLs in reverse order. It only address URL output to console and does not affect the TUI at all. Is that what you were looking for?

pigmonkey commented 4 years ago

That does result in the desired ordering, but I want to use it with -c/--compact. My use case for this is quickly opening the most recently printed URLs in my terminal via tmux-urlscan, which is only helpful with urlscan's TUI browser. (I don't need the URL context, though.)

firecat53 commented 4 years ago

Ah. I misunderstood you, then. Let me try again...

firecat53 commented 4 years ago

Just FYI...I have this as a one-liner in my tmux.conf that doesn't require a separate plugin:

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n urlscan "urlscan -d /tmp/tmux-buffer"
firecat53 commented 4 years ago

Ok, give the reverse branch a try again. R will reverse URLs and context both in the TUI and to stdout.

pigmonkey commented 4 years ago

-R works perfectly. Thanks!