0

Is there an easy way to have arbitrary text editors call special logic based on a file extension?

For example, let's say I had a zip file that had a collection of files.

text.txt image.jpg othercrap.crap 

Is there a way to have text editors automatically run commands when opening the file via something like vi myarchive.zip based on the file extension?

2
  • 1
    sounds like taskwarrior Commented Sep 20, 2023 at 21:12
  • Since everyone seems to be hung up on the backstory I'll trim the question Commented Sep 21, 2023 at 21:20

2 Answers 2

0

I don't understand a lot of your description other than "it would just be slick if I could just use vi on it and the text parts would automatically know how to format themselves" but this might be what you're trying to do (untested):

#!/usr/bin/env bash tmp=$(mktemp) || exit 1 trap 'rm -f "$tmp"; exit' 0 edit_zip() { unzip "$1" > "$tmp" && vi "$tmp" && zip "$tmp" > "$1" } edit_pdf() { pdf2text "$1" > "$tmp" && vi "$tmp" && text2pdf "$tmp" > "$1" } act_ext="${1}_${2##*.}" if declare -F "$act_ext" > /dev/null; then "$act_ext" "$3" else printf 'Operation "%s" not supported.\n' "$*" fi 

The above would be the contents of myprog which you'd call as myprog edit myfile.zip as shown in your question.

unzip and zip above may not take exactly those args or be exactly the commands you want to run on a zip file and pdf2text and text2pdf may not really exist or really do what you'd want to edit a PDF, the above is just to give you the idea of writing a separate function to do whatever it is you want to do per operation (action+extension) you want to support, e.g. edit and .zip, etc. and then having that appropriate function be automatically called based on the arguments passed to your script.

-1

Using Raku (formerly known as Perl_6)

A. You can design/run your own class Proc objects, which are representations of external command calls. Save below as a script and run:

my $proc = run 'echo', 'Hallo world', :out; my $captured-output = $proc.out.slurp: :close; say "Output was $captured-output.raku()";# OUTPUT: «Output was "Hallo world\n"␤» 

B. Maybe you don't want to get your shell involved, so instead you can write your own Raku one-liners and point them at files in a particular dir() directory. Below returns .jpeg files and their creation dates, one line per .jpeg file:

~$ raku -e 'for dir( test => /:i '.' jpe?g $/ ) -> $file { say join "\t", $file, $file.IO.created.DateTime; } 

https://docs.raku.org/type/Proc
https://docs.raku.org/routine/dir


C. Raku has the Ake module/script, which is useful for designing/running tasks:

~/Ake_Morning$ cat Akefile task 'buy-food', { say 'Bought a salad.' } task 'shower', { say 'Showered.' } task 'morning' => <shower buy-food>; task 'dinner' => <buy-food>, { say 'Yummy!' } 

Then run the ake binary on the Akefile:

~/Ake_Morning$ ake Task “default” does not exist Did you mean one of these? buy-food dinner help morning shower ~/Ake_Morning$ ake morning Showered. Bought a salad. 

https://github.com/Raku/ake


D. Raku also has the Sparrow6 (DSL) and Tomtit (task-runner) Ecosystem, which appears to be yaml-based. Sparrow can run tasks for you in 6 different languages: 1. Raku, 2. Perl, 3. Bash, 4. Python, 5. Ruby, and 6. Powershell.

https://github.com/melezhik/Tomtit
https://github.com/melezhik/Sparrow6/blob/master/documentation/dsl.md
https://raku.org

1
  • So I voted down initially because I wasn't asking for a suggestions for command line task managers, but then I saw that my question probably had more info than necessary. I wanted to undo my vote because I do appreciate the suggestion, but now it's too late. I'm sorry Commented Sep 21, 2023 at 22:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.