https://github.com/jonhoo/proximity-sort
Simple command-line utility for sorting inputs by proximity to a path argument
https://github.com/jonhoo/proximity-sort
Last synced: over 1 year ago
JSON representation
Simple command-line utility for sorting inputs by proximity to a path argument
- Host: GitHub
- URL: https://github.com/jonhoo/proximity-sort
- Owner: jonhoo
- License: apache-2.0
- Created: 2018-09-18T05:24:03.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2023-02-11T20:11:42.000Z (over 3 years ago)
- Last Synced: 2025-03-31T04:07:33.630Z (over 1 year ago)
- Language: Rust
- Size: 76.2 KB
- Stars: 123
- Watchers: 5
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# proximity-sort
[](https://crates.io/crates/proximity-sort)
[](https://codecov.io/gh/jonhoo/proximity-sort)
[](https://deps.rs/repo/github/jonhoo/proximity-sort)
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`](https://github.com/junegunn/fzf) (requested in
[junegunn/fzf.vim#360](https://github.com/junegunn/fzf.vim/issues/360)
and
[junegunn/fzf.vim#492](https://github.com/junegunn/fzf.vim/issues/492))
without making modifications to `fzf` itself (see
[junegunn/fzf#1380](https://github.com/junegunn/fzf/pull/1380)).
## Installation
If you have [Rust installed](https://www.rust-lang.org/tools/install), you can
install this with:
```shell
cargo install proximity-sort
```
## Usage
It can be used with `fzf` by running:
```console
$ fd -t f | proximity-sort path/to/file | fzf --tiebreak=index
```
And you can add it to your `.vimrc` with:
```vim
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(, {'source': s:list_cmd(),
\ 'options': '--tiebreak=index'}, 0)
```
Paths of the same proximity are sorted alphabetically:
```console
$ echo "banana\napple/pie\napple" | proximity-sort . -s
> apple
> banana
> apple/pie
```