#cli #pipe #filter #utility

app proximity-sort

Simple command-line utility for sorting inputs by proximity to a path argument

11 stable releases

1.3.0 Feb 11, 2023
1.2.0 Jul 4, 2022
1.1.0 Dec 19, 2021
1.0.7 Aug 3, 2020
1.0.2 Sep 20, 2018

#237 in Command line utilities

Download history 20/week @ 2023-10-15 15/week @ 2023-10-22 36/week @ 2023-10-29 9/week @ 2023-11-05 7/week @ 2023-11-12 17/week @ 2023-11-19 39/week @ 2023-11-26 29/week @ 2023-12-03 11/week @ 2023-12-10 8/week @ 2023-12-17 24/week @ 2023-12-24 5/week @ 2023-12-31 16/week @ 2024-01-07 22/week @ 2024-01-14 22/week @ 2024-01-21 26/week @ 2024-01-28

87 downloads per month

MIT/Apache

13KB
240 lines

proximity-sort

Crates.io Codecov Dependency status

This script provides a simple command-line utility that sorts its inputs by their path proximity to a given path. For example, for a path foo/bar.txt, the following input:

quox.txt
foo/bar.txt
foo/baz.txt

Would yield an output of:

foo/bar.txt
foo/baz.txt
quox.txt

The lines are sorted by the number of leading path components shared between the input path and the provided path.

This program was primarily written to allow context-aware suggestions for fzf (requested in junegunn/fzf.vim#360 and junegunn/fzf.vim#492) without making modifications to fzf itself (see junegunn/fzf#1380).

Installation

If you have Rust installed, you can install this with:

cargo install proximity-sort

Usage

It can be used with fzf by running:

$ fd -t f | proximity-sort path/to/file | fzf --tiebreak=index

And you can add it to your .vimrc with:

function! s:list_cmd()
  let base = fnamemodify(expand('%'), ':h:.:S')
  return base == '.' ? 'fd -t f' : printf('fd -t f | proximity-sort %s', expand('%'))
endfunction

command! -bang -nargs=? -complete=dir Files
  \ call fzf#vim#files(<q-args>, {'source': s:list_cmd(),
  \                               'options': '--tiebreak=index'}, <bang>0)

Paths of the same proximity are sorted alphabetically:

$ echo "banana\napple/pie\napple" | proximity-sort . -s
> apple
> banana
> apple/pie

Dependencies

~3–14MB
~152K SLoC