Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Hello, sorted world!
  • Loading branch information
TheAMM committed Dec 30, 2018
0 parents commit 011dd7e
Show file tree
Hide file tree
Showing 13 changed files with 1,203 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mpv_sort_script.conf
mpv_sort_script.lua
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libs"]
path = libs
url = git@github.com:TheAMM/mpv_script_libs.git
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SCRIPT_NAME=mpv_sort_script
CONCAT_FILE=concat.json
CONCAT_TOOL=./libs/concat_tool.py

SRC=$(wildcard libs/*.lua) $(wildcard src/*.lua)

.PHONY: release watch clean

$(SCRIPT_NAME).lua: $(CONCAT_FILE) $(SRC)
$(CONCAT_TOOL) -r "$<" -o "$@"

$(SCRIPT_NAME).conf: $(SCRIPT_NAME).lua
mpv av://lavfi:anullsrc --end 0 --msg-level cplayer=fatal --quiet --no-config --script "$<" --script-opts $(SCRIPT_NAME)-example-config="$@"

release: $(SCRIPT_NAME).lua $(SCRIPT_NAME).conf

watch: $(CONCAT_FILE)
-$(CONCAT_TOOL) -w $<

clean:
-rm $(SCRIPT_NAME).lua $(SCRIPT_NAME).conf
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# `mpv_sort_script.lua`

## What is it?

`mpv_sort_script.lua` is a script for sorting directory entries by name, age, size or randomly. An entire directory tree can be sorted at once, as well.
The script hooks file loading to check if the given path is a directory, enumerates the entries and sorts them.
(The script does *not* allow sorting the playlist during playback, yet!)

## How do I install it?

Grab a release from the [releases page](https://github.com/TheAMM/mpv_sort_script/releases), an automatic build [from here](https://raw.githubusercontent.com/TheAMM/mpv_sort_script/build/mpv_sort_script.lua) or [see below](#development) how to "build" (concatenate) the script yourself.
Place the `mpv_sort_script.lua` to your mpv's `scripts` directory.

For example:
* Linux/Unix/Mac: `~/.config/mpv/scripts/mpv_sort_script.lua`
* Windows: `%APPDATA%\Roaming\mpv\scripts\mpv_sort_script.lua`

See the [Files section](https://mpv.io/manual/master/#files) in mpv's manual for more info.

## How do I use it?

Prepend a directory path with `sort:` (or `rsort:` for recursive sorting), for example:

```shell
$ mpv sort:~/Videos
$ mpv sort:/tmp/files
$ mpv sort:.
$ mpv rsort:~/Videos # recurse into all subdirectories and sort
```
This will sort using the default sort options, see [Configuration](#configuration) below.

Recursive sorting with `rsort:` means the script will find all files in the directory tree (up until `max_recurse_depth`) and sort them all in one go. Otherwise the script will sort each folder separately, when it comes across them.

You may specify the sorting method and order as well:
```shell
$ mpv sort-name:. # sort by name, using default order (depends on config)
$ mpv sort-size-:. # sort by size, descending (biggest to smallest)
$ mpv sort-date+:. # sort by date, ascending (oldest to newest)
$ mpv sort-random:. # sort files randomly
```

## Configuration

Create a file called `mpv_sort_script.conf` inside your mpv's `lua-settings` directory.

For example:
* Linux/Unix/Mac: `~/.config/mpv/lua-settings/mpv_sort_script.conf`
* Windows: `%APPDATA%\Roaming\mpv\lua-settings\mpv_sort_script.conf`

See the [Files section](https://mpv.io/manual/master/#files) in mpv's manual for more info.

You can grab an example config [from here](https://raw.githubusercontent.com/TheAMM/mpv_sort_script/build/mpv_sort_script.conf) or use mpv to save an example config:
```shell
$ mpv --idle --script-opts sort-example-config=example.conf
```
The example configuration documents itself, so read that for all the options.

## Development

This project uses git submodules. After cloning (or fetching updates), run `git submodule update --init` to update the `libs` submodule, which contains parts most of my scripts share and use.

Included in the libs directory is `concat_tool.py` I use for automatically concatenating files upon their change, and also mapping changes to the output file back to the source files. It's really handy on stack traces when mpv gives you a line and column on the output file - no need to hunt down the right place in the source files!

The script requires Python 3, so install that. Nothing more, though. Call it with `libs/concat_tool.py concat.json`.

You may also, of course, just `cat` the files together yourself. See the [`concat.json`](concat.json) for the order.

### Donation

If you *really* get a kick out of this (weirdo), you can [paypal me](https://www.paypal.me/TheAMM) or send bitcoins to `1K9FH7J3YuC9EnQjjDZJtM4EFUudHQr52d`. Just having the option there, is all.
19 changes: 19 additions & 0 deletions concat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"output" : "mpv_sort_script.lua",

"files" : [
"src/license_blurb.lua",
"<version>",
"libs/helpers.lua",
"libs/option_parser.lua",
"src/options.lua",
"src/alphanum.lua",
"src/main.lua"
],

"version_file" : "version.txt",
"version_template_file" : "src/version.tmpl.lua",

"header_prefix" : "--[ ",
"header_suffix" : " ]--"
}
1 change: 1 addition & 0 deletions libs
Submodule libs added at 4fa8ce
48 changes: 48 additions & 0 deletions src/alphanum.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- alphanum.lua (C) Andre Bogus
--[[ based on the python version of ned batchelder
Distributed under same license as original
Released under the MIT License - https://opensource.org/licenses/MIT
Copyright 2007-2017 David Koelle
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

-- split a string into a table of number and string values
function splitbynum(s)
local result = {}
for x, y in (s or ""):gmatch("(%d*)(%D*)") do
if x ~= "" then table.insert(result, tonumber(x)) end
if y ~= "" then table.insert(result, y) end
end
return result
end

-- compare two strings
function alnumcomp(x, y)
local xt, yt = splitbynum(x), splitbynum(y)
for i = 1, math.min(#xt, #yt) do
local xe, ye = xt[i], yt[i]
if type(xe) == "string" then ye = tostring(ye)
elseif type(ye) == "string" then xe = tostring(xe) end
if xe ~= ye then return xe < ye end
end
return #xt < #yt
end
16 changes: 16 additions & 0 deletions src/license_blurb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--[[
Copyright (C) 2018 AMM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]--
Loading

0 comments on commit 011dd7e

Please sign in to comment.