piotrmurach / tty-prompt

A beautiful and powerful interactive command line prompt
https://ttytoolkit.org
MIT License
1.47k stars 136 forks source link

Add `Choice#index` and `Choice#<=>` #143

Closed DanielVartanov closed 4 years ago

DanielVartanov commented 4 years ago

Describe the change

It adds #index and #<=> methods to Choice which makes them sortable inside Choices.

Why are we doing this?

In PR https://github.com/piotrmurach/tty-prompt/pull/141 Piotr suggested

I wonder if we could extend the Choice with index attribute or similar and make it comparable. This would simplify and speed up sorting: @selected.sort!

Benefits

@selected.sort_by! { |choice| choices.index(choice) } can be replaced with @selected.sort!

Drawbacks

Requirements

Put an X between brackets on each line if you have done the item: [X] Tests written & passing locally? [X] Code style checked? [X] Rebased with master branch? [X] Documentation updated?

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.01%) to 97.352% when pulling 03544ebe5b7e70f9df06aed67b974f23c4218a1a on DanielVartanov:make-choices-sortable into b330a32eb0d509b9d515d59819b9745e0cea0b42 on piotrmurach:master.

DanielVartanov commented 4 years ago

Tests failed for older versions of Ruby (<2.3). @piotrmurach if you don't mind I'll fix them only if you choose this path, no point of polishing it if the other path is chosen

DanielVartanov commented 4 years ago

Great, I thought the same the very moment I realised I had to remove immutability :+1: Anyway it was good to explore this path, as you see there are something which are better be cherry-picked

piotrmurach commented 4 years ago

Thank you for exploring this path, helped me see the impact of this idea pretty clearly.