17
submitted 2 weeks ago by thingsiplay@beehaw.org to c/linux@lemmy.ml

A week ago I started a little script to format the output of file and path listings from other programs. It got a little bit out of hand and I implemented lot of advanced features into the fmt commands; kind of a sub language to define how the output should be formatted and structured. Entire idea is to give it paths, process the stream (not the file content itself, but the path representing a file) and output them again.

fpath accepts two type of input: a) either as arguments like fpath *.txt or b) from stdin like ls -1 | fpath . With additional options and commands the output can be colored and reformatted to something entirely different. In example with -F option the advanced formatting is possible, such as fpath -F '{.size} {name}' as a simple example.

There is lot of functionality (based on Python, yes this is a Python script), such as {reverse}{name}{/reverse} to reverse font color and background of the segment that is enclosed by the command, a slice to get a subset as a range from the entire path {-1:}, or {center:80}text{/center} to add spaces to get centered text, or just {ext} to output the extension, {mime} to output the file mime type, or even execute arbitrary programs {!grep:a}{path}{/!} to reduce output that has an a in the path.

Did I over engineer it again? Or just ignore most stuff and use it with the most simple options, should be enough anyway: fpath -t -s red *.txt

all 7 comments
sorted by: hot top controversial new old
[-] joeldebruijn@lemmy.ml 3 points 2 weeks ago

Did you over engineer? Yes! Also: this is very cool!!

[-] NuclearDolphin@lemmy.ml 2 points 2 weeks ago

I like this as an idea, but there'd need to be some sort of automatic hooking for me to want to use it. eza/lsd are good enough for ls output for me.

Doing stuff like this is much more cozy in nushell, since piping is a lot less messy

I find myself writing way less stuff like this since making the switch...partially because they output of a lot of builtins is already pretty.

[-] thingsiplay@beehaw.org 2 points 2 weeks ago

fpath is not a replacement for a ls like command, but to change its output (I am an eza user myself). fpath takes output of eza to transform it into whatever I want. And its not just usable with an list program such ls or eza, but also operates on stdin like grep, even non existent files to maybe form something on a different place with same name in example. I still use ls (which is eza for me as an alias) for regular day operation.

fpath is useful for output from find command or baloosearch6 (KDE's file indexer, that only outputs full path of matching search). In this case eza does not help me. Sometimes it can get messy with using sed and grep mulitple times and Bash substitution to get file extension and so on. Or I use baloosearch6 "Super Mario World" | fpath -F{name} -x*.pcm to just show filename, excluding all paths matching *.pcm glob (not regex) for whatever reason I want to use. The thing is, this is just a moment to be in. Next moment I can easily adjust the output to output it with or without other information.

[-] tsonfeir@lemm.ee 2 points 2 weeks ago

What is a real world example of how this could be used?

[-] acockworkorange@mander.xyz 4 points 2 weeks ago

Aliases to common commands so it gets automatically formatted.

[-] thingsiplay@beehaw.org 2 points 2 weeks ago

It's a bit hard to show examples that justifies such a tool. But it's not about making things possible, as there are other tools like ls, grep and sed and awk and Bash commands to do all the formatting and output. But its a little bit easier and more flexible to have fpath, which understands paths and has dedicated functionality to support that. It's more about being flexible and doing it in place easily. At least for me, because I know the tool.

Let's say I want to output some information about files that come as a result from my file indexer baloosearch6 (from KDE). It only outputs full paths. Let's say I want to show only its names and the file type information (or any other) next to it:

baloosearch6 "Super Mario" | grep /snes/ | fpath -F'{name}\n\t{file}\n'
this post was submitted on 16 May 2024
17 points (100.0% liked)

Linux

44870 readers
1108 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS