lazydocker icon indicating copy to clipboard operation
lazydocker copied to clipboard

Allow sorting by column in list panels

Open jesseduffield opened this issue 3 years ago • 8 comments

Is your feature request related to a problem? Please describe. When there are a large amount of items in a list panel, it can be convenient to sort by a particular column. For example, you may want to know which images have the greatest size, so you'd want to sort by image size descending and look at what's at the top.

Describe the solution you'd like As an example, if I'm in the images panel I want to be able to press a key and see a menu with the options:

  • sort by name (ascending)
  • sort by name (descending)
  • sort by tag (ascending)
  • sort by tag (descending)
  • sort by size (ascending)
  • sort by size (descending)
  • default sort

Both 's' and 'S' are already used in the containers panel for starting and stopping containers, so that's off the cards. 'o' is the next most obvious candidate i.e. 'order', so let's go with that.

Additional context This issue doesn't care about persisting sorting between different runs of the application, nor does it care about configuring a default sort order. Those can be tackled in separate issues.

In lazygit we have the ability to have keybindings against menu items so that you don't need to navigate through the list to select an item. Once we have that feature, we can apply it here so that for example you can press 'o', 's' for sorting by size ascending, and 'o', 'S' for size descending. But that's out of scope for this issue.

We'll need to maintain the sorting state against each list panel, so we can create a SortBy field in each of the list panels' panelStates struct (which you'll find in pkg/gui/gui.go). We'll also need a SortDirection field:

type imagePanelState struct {
	SelectedLine  int
	ContextIndex  int    // for specifying if you are looking at logs/stats/config/etc
	SortBy        string // One of "default", "name", "tag", "size"
	SortAscending bool   // ignored when SortBy is 'default'
}

The purpose of the default sort is for when e.g. we're in the services panel which has some bespoke sorting logic. If we're not on the default sort, any bespoke sorting logic will be ignored.

We can then apply the sort logic in pkg/commands/image.go in the RefreshImages() function.

If you would like to pick this up let me know!

jesseduffield avatar Oct 13 '22 01:10 jesseduffield

Both 's' and 'S' are already used in the containers panel for starting and stopping containers, so that's off the cards.

But why? Different panel, different context, different action.

'o' is the next most obvious candidate i.e. 'order', so let's go with that.

o might have a better function down the road, like open something? Of course, the mapping doesn't have to make sense, but I'd somehow rather save up o for something "better". What about z?

mark2185 avatar Oct 13 '22 05:10 mark2185

although my examples above were for the image panel, the goal is to have this functionality for all list panels

jesseduffield avatar Oct 13 '22 05:10 jesseduffield

good point about using 'o' to open something though

jesseduffield avatar Oct 13 '22 05:10 jesseduffield

Happy to go with 'z'

jesseduffield avatar Oct 13 '22 19:10 jesseduffield

@jesseduffield , I would like to work on this issue, can you guide me how and from where I should start with the development, also like the setup in local for dev?

26tanishabanik avatar Nov 28 '22 19:11 26tanishabanik

I recommend using either github codespaces or a dev container in VSCode (to minimise the setup time). I've neglected to add instructions to the lazydocker contributing guide but you can use the lazygit contribute guide as a guide: https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#running-in-a-vscode-dev-container

jesseduffield avatar Nov 28 '22 23:11 jesseduffield

I recommend using either github codespaces or a dev container in VSCode (to minimise the setup time). I've neglected to add instructions to the lazydocker contributing guide but you can use the lazygit contribute guide as a guide: https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#running-in-a-vscode-dev-container

Got it @jesseduffield , will look into that guide

26tanishabanik avatar Nov 29 '22 07:11 26tanishabanik

@jesseduffield , what should be the binding struct for this feature in keybindings.go ? And where to apply the sorting for all the views?

26tanishabanik avatar Dec 01 '22 21:12 26tanishabanik