r3bl-org/r3bl-open-core

Support unix pipes (as a bin target `rt`)

Closed this issue · 1 comments

Create a bin target for this crate that can be used in unix pipes. The idea is to enable something like the following.

cat Cargo.toml | rt -s single -c "echo foo \'%\'"

Due to the way in which unix pipes are implemented, it is not possible to pipe the stdout of this command to anything else. Unix pipes are non blocking. So there is no way to stop the pipe mid way. Instead, the rt binary simply takes a command that it will run after the user has made their selection. Using the selected item(s) and applying them to this command.

This bin target named rt can be used in scripts to select a single line from the output of a program (that outputs to stdout). Very much like grep but for making selections using tuify.

Helpful links:

  1. This will require a check to see whether the terminal is interactive mode or not, ie whether stdin is piped. More info here.

  2. And it will require the use of clap.

Note: Windows powershell also supports pipelining: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.3
GitHub Copilot: Yes, PowerShell supports command pipelining using the | operator. This allows you to pass the output of one command as input to another command. Here's an example:

Get-ChildItem | Where-Object { $_.Extension -eq ".txt" } | ForEach-Object { Get-Content > $_ }

In this example, Get-ChildItem retrieves a list of files, which is then piped to Where-Object to filter the list to only include files with a .txt extension. The resulting list is then piped to ForEach-Object, which reads the contents of each file and outputs them to the console.
You can chain together as many commands as you like using the | operator.

Video of this bin target in action

tuify-pipes-alpha-2023-09-10_11.22.12.mp4