nvbn / thefuck

Magnificent app which corrects your previous console command.
MIT License
84.95k stars 3.43k forks source link

Proposition: rules with no alternative command and only side effects #1337

Open njokipal opened 1 year ago

njokipal commented 1 year ago

The output of thefuck --version (something like The Fuck 3.1 using Python 3.5.0 and Bash 4.4.12(1)-release): The Fuck 3.32 using Python 3.10.7 and ZSH 5.9

Anything else you think is relevant:

It would be nice to be able to have rules where no corrected command is specifically run instead of a failty one, but simply run another command that has side effects. E.g. I have a rule for firebase CLI where at times I get

$ firebase emulators:start --import=./dump --export-on-exit
...

✔  functions: Loaded functions definitions from source: <omitted>.
^C 
⚠  emulators: Received SIGINT (Ctrl-C) 2 times. You have forced the Emulator Suite to exit without waiting for 2 subprocesses to finish. These processes may still be running on your machine: 

┌────────────────────┬────────────────┬────────┐
│ Emulator           │ Host:Port      │ PID    │
├────────────────────┼────────────────┼────────┤
│ Firestore Emulator │ localhost:8180 │ 164748 │
├────────────────────┼────────────────┼────────┤
│ Pub/Sub Emulator   │ localhost:8185 │ 164793 │
└────────────────────┴────────────────┴────────┘

To force them to exit run:

kill 164748 164793

firebase-kill.py

from thefuck.types import Command

def match(command: Command) -> bool:
    return 'You have forced the Emulator Suite to exit without waiting' in command.output

def get_new_command(command: Command) -> str | list[str]:
    import re
    # ...
    # To force them to exit run:
    #
    #     kill 101663 101714
    kill_pids = re.findall('(kill ((\\d+\\s?)+))', command.output)
    return kill_pids[0][0].strip() or None

enabled_by_default = True

def side_effect(old_command: Command, fixed_command: str) -> None:
    # actually subprocess.call(<cmd>, shell=True) here
    return

priority = 1000

requires_output = True

Result:

$ fuck
kill 101663 101714 (+side effect) [enter/↑/↓/ctrl+c]

What I would want, is to simply have a side effect: kill 101663 101714 [enter/↑/↓/ctrl+c] since nothing was "fixed" as this is actually house-keeping/cleanup after the original command was run