Skip to content

genfilters: interactive filter generator for rclone + bisync integration - #7578

Open
nielash wants to merge 4 commits into
rclone:masterfrom
nielash:bisync-genfilters
Open

nielash wants to merge 4 commits into
rclone:masterfrom
nielash:bisync-genfilters

Conversation

@nielash

@nielash nielash commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor

What is the purpose of this change?

This adds rclone genfilters, a text based user interface for making filter rules. It shows the remote as a tree in which you include and exclude files and directories with a key press, and checks the rules with rclone's own filters as they change, so what you see is what rclone will include. The rules can also be edited as text (TAB), with the tree updating as you type.

--edit FILE edits a filter file in place (keeping the old version as FILE.bak); otherwise the rules are read from stdin and written to stdout. --exclude-others starts with everything excluded.

It also adds --edit-filters to bisync, which opens the --filters-file in genfilters before the run and offers to do the --resync that changed filters need.

Demo: https://asciinema.org/a/ynXSjMcBUc8Gfg9V

Four commits:

  1. lib/tui: ncdu's drawing helpers (text, popup box and menu, log redirect) moved into a package so genfilters can share them. No change to ncdu.
  2. filter: a small GlobEscape, so genfilters doesn't keep its own copy of the glob special characters. Its test checks every punctuation character against rclone's glob compiler.
  3. genfilters: the command.
  4. bisync: --edit-filters and --edit-filters-path2.

Tested on macOS and Windows 11.

Was the change discussed in an issue or in the forum before?

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • I'm done, this Pull Request is ready for review :-)

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fun idea :-)

I pulled it locally and had a play.

Some immediate thoughts for discussion

  • Needs help - I expect ? or h to bring up the help, likewise CTRL-L to refresh the screen.
  • I'd like to be able to edit the include file and see what happens to the files in real time (maybe too hard!). So press TAB say to move to editing the left hand side and TAB to move back again. Or press e to edit the include file in your default editor (like the way git commit works).
  • I thought the default output to a file, then opening that file wasn't very unixy! Printing the result to stdout would probably be a more unixy default
  • If you provide an output file, then it should use that file as the default input I think, so genfilters effectively edits a filter file for you. Having to supply two flags is non optimal. Maybe the one flag should be called --edit, so you rclone genfilters --edit myfilters remote:path
  • I think we should try to make it as consistent with ncdu as possible so left, right should be enter exit directory and we should use RETURN or SPACE maybe to toggle the selection (might be worth studying some other text ui progs to see what they use).
  • Perhaps ncdu should export some things genfilters should use or we factor ncdu parts into a lib.

Comment thread docs/static/img/genfilters_demo.gif Resolved
@nielash

nielash commented Feb 3, 2024

Copy link
Copy Markdown
Contributor Author

This is a fun idea :-)

I pulled it locally and had a play.

Thanks! 😃

  • Needs help - I expect ? or h to bring up the help, likewise CTRL-L to refresh the screen.

Sounds good to me.

  • I'd like to be able to edit the include file and see what happens to the files in real time (maybe too hard!). So press TAB say to move to editing the left hand side and TAB to move back again. Or press e to edit the include file in your default editor (like the way git commit works).

That might be difficult! But also sounds like a fun challenge. 🙂 I'll look into this!

Aside from the challenges of the text editor part, there's also the question of how to handle unexpected input -- syntax errors, non-existent paths, duplicated rules, etc. Error? Ignore? Fix? Also,genfilters does not currently output some of the more advanced options like wildcards and regex globs, so I'm not sure what it should do if it receives them in an input file. Convert them to static include/exclude file rules? Delete them? Attempt to preserve them? I'll give this some thought. 🙂

  • I thought the default output to a file, then opening that file wasn't very unixy! Printing the result to stdout would probably be a more unixy default

Haha, fair point! There is a --no-open flag, but I agree that should probably just be the default. Printing to stdout in the absence of -o also sounds fine to me.

  • If you provide an output file, then it should use that file as the default input I think

What if the input file doesn't exist though? (In other words, you have no file to start from and you want genfilters to generate one.)

My first draft of this actually had no --input-file feature, only output! Input was a later development.

  • Having to supply two flags is non optimal. Maybe the one flag should be called --edit, so you rclone genfilters --edit myfilters remote:path

In addition to my above question about non-existent input, what if you want to "Save as" the output to a different path than the input? Perhaps the input and output flags should stay but an --edit FILE flag should be added as shorthand for --input-file FILE -o FILE? (sidenote: I tried to grab the -i shorthand but it was already in use for --interactive...)

I think we should try to make it as consistent with ncdu as possible so left, right should be enter exit directory and we should use RETURN or SPACE maybe to toggle the selection (might be worth studying some other text ui progs to see what they use).

Sure, that makes sense to me. (Although we don't really "enter" directories the way ncdu does -- but I know what you mean!)

Perhaps ncdu should export some things genfilters should use or we factor ncdu parts into a lib.

That would be helpful! 🙂 I frequently find myself wanting to reuse code from other rclone packages, but often they are in private functions so it becomes a question of whether to start making intrusive changes outside the package I'm working on... I sometimes wish more things were exported by default, but I also understand why they're not!

I'll play around with these ideas and work on a revised version!

@edc-w edc-w removed the new feature label Jun 17, 2024
@ncw

ncw commented Aug 23, 2024

Copy link
Copy Markdown
Member

X-Reply-Style: better-late-than-never

This is a fun idea :-)
I pulled it locally and had a play.

Thanks! 😃

  • Needs help - I expect ? or h to bring up the help, likewise CTRL-L to refresh the screen.

Sounds good to me.

  • I'd like to be able to edit the include file and see what happens to the files in real time (maybe too hard!). So press TAB say to move to editing the left hand side and TAB to move back again. Or press e to edit the include file in your default editor (like the way git commit works).

That might be difficult! But also sounds like a fun challenge. 🙂 I'll look into this!

Aside from the challenges of the text editor part, there's also the question of how to handle unexpected input -- syntax errors, non-existent paths, duplicated rules, etc. Error? Ignore? Fix? Also,genfilters does not currently output some of the more advanced options like wildcards and regex globs, so I'm not sure what it should do if it receives them in an input file. Convert them to static include/exclude file rules? Delete them? Attempt to preserve them? I'll give this some thought. 🙂

Good thought. If there is an error in the input file we should just print the error nicely and stop. If the user enters something incorrectly in the editor then we should display an error and make them edit it again or cancel.

  • I thought the default output to a file, then opening that file wasn't very unixy! Printing the result to stdout would probably be a more unixy default

Haha, fair point! There is a --no-open flag, but I agree that should probably just be the default. Printing to stdout in the absence of -o also sounds fine to me.

  • If you provide an output file, then it should use that file as the default input I think

What if the input file doesn't exist though? (In other words, you have no file to start from and you want genfilters to generate one.)

The unix way would be to pipe the input file on stdin and pipe it to separate output file, or have an --edit flag

My first draft of this actually had no --input-file feature, only output! Input was a later development.

  • Having to supply two flags is non optimal. Maybe the one flag should be called --edit, so you rclone genfilters --edit myfilters remote:path

That works for me.

(You should preserve the old file with --edit - rename it as a backup file maybe? And --edit should work with a a non-existent file.

I think we should try to make it as consistent with ncdu as possible so left, right should be enter exit directory and we should use RETURN or SPACE maybe to toggle the selection (might be worth studying some other text ui progs to see what they use).

Sure, that makes sense to me. (Although we don't really "enter" directories the way ncdu does -- but I know what you mean!)

Perhaps ncdu should export some things genfilters should use or we factor ncdu parts into a lib.

That would be helpful! 🙂 I frequently find myself wanting to reuse code from other rclone packages, but often they are in private functions so it becomes a question of whether to start making intrusive changes outside the package I'm working on... I sometimes wish more things were exported by default, but I also understand why they're not!

Yes we should try to be deliberate about which bits are exported otherwise the codebase will degenerate into a tangled mess!

I'll play around with these ideas and work on a revised version!

:-)

How did you get on?

@nielash

nielash commented Sep 18, 2024

Copy link
Copy Markdown
Contributor Author

How did you get on?

I haven't had a chance to play around with it yet, but I do still plan to! 😅

Before this change, the helpers `ncdu` used to draw text, popup boxes and
menus, and to keep log output off the screen, were private to `cmd/ncdu`.

After this change, they live in a new `lib/tui` package, so other text based
user interfaces can share them. `ncdu` uses them unchanged.
Before this change, code making filter rules for a name had to escape the
glob special characters itself, keeping its own copy of the list in
`globToRegexp`.

After this change, `filter.GlobEscape` returns a glob matching a name
literally. Its test checks every punctuation character against
`GlobPathToRegexp` and `GlobStringToRegexp`, so a new special character
fails it.
@nielash
nielash requested a review from ivandeex as a code owner November 3, 2026 21:25
@nielash

nielash commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

How did you get on?

@ncw Better late than never, part 2 😅 -- I finally got back to this. It's rebased onto master and reworked based on your feedback:

  • Help and refresh: ? toggles the help, ^L redraws.
  • Editing the rules: TAB switches to the rules as text, and the tree updates as you type. A line genfilters can't use is shown in red with the error, and the tree keeps the last good rules. If you try to leave with an error, it asks whether to keep editing or discard the edits. A bad input file prints the error (with the line number) and stops.
  • Unixy I/O: the rules go to stdout, and are read from stdin if it isn't a terminal. --edit FILE edits a file in place, creating it if needed, and keeps the old version as FILE.bak. Opening the file is opt-in now (--open). --input-file, -o and --no-open are gone.
  • ncdu-style keys: ←/→ (and h/l) close and open directories, SPACE/RETURN toggle.
  • Sharing with ncdu: ncdu's drawing helpers (Line/Print, the popup box and menu, the log redirect) are now in a lib/tui package, in a commit of their own, and genfilters uses them. ncdu's behavior is unchanged.
  • No GIF: dropped in favor of an asciinema, like ncdu's: https://asciinema.org/a/ynXSjMcBUc8Gfg9V

The other big change is that it doesn't use tview anymore. Since you moved ncdu to tcell/v3 (2a49dd9), keeping tview (still on tcell/v2) would have brought v2 back along with the Windows duplicate-key bug, so it's on tcell/v3 directly, like ncdu. I tested it on Windows 11 and the keys behave.

On rules it can't represent: it reads exactly what it could have written (anchored file or dir/** rules, each before the rules for the directories containing it, then + ** or - **) and gives an error for anything else (wildcards, unanchored patterns, regex, rules that would never be used), rather than guessing. Reading a rule back goes through the engine too: genfilters asks rclone's glob compiler which name a rule matches, so it keeps no list of special characters of its own. To keep the rest from drifting from the filter engine, a test runs thousands of random rule files through both and fails on any disagreement. The only addition to fs/filter is GlobEscape. One wrinkle you'll see in the escaping: filter files trim each line, so a name ending in a space is written as name[ ], which is the one spelling that matches it both in memory and from a file.

For bisync, --edit-filters now runs in Bisync() before the defaults are set, so the resync it offers is a normal --resync. The old version set Resync after setResyncDefaults, which a test showed would have let Path2 win; another test checks the resync gets the same defaults as --resync, including --slow-hash-sync-only. It's also rejected over rc, since it's interactive.

Before this change, filter rules for `--filter-from` or bisync's
`--filters-file` had to be written by hand, where a small mistake can
include or exclude far more than intended.

After this change, `rclone genfilters remote:path` shows the remote as a
tree in which files and directories are included or excluded with a key
press. The rules are checked with rclone's own filters as they change, so
the tree shows what rclone will include, and they can also be edited as
text with the tree updating as you type. `--edit FILE` edits a filter file
in place, keeping the old version as `FILE.bak`; otherwise the rules are
read from standard input and written to standard output.
`--exclude-others` starts with everything excluded (`- **`).
Before this change, a bisync `--filters-file` had to be written by hand,
where a small mistake could make bisync include or exclude far more than
intended.

After this change, `--edit-filters` opens the `--filters-file` in
`rclone genfilters` before the run, showing Path1 (or Path2 with
`--edit-filters-path2`). If the filters change, bisync offers to do the
`--resync` they need, and syncs nothing if declined. The flags are
interactive, so the rc rejects them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants