-1

I am writing a fish function to play the first result of a search term as audio using yt-dlp and mpv. The problem is that I need the search term as a single string, whereas $argv is an array of strings. How do I convert an array of strings to a single string in fish?

Example code:

function ytaudio
    yt-dlp -f 251 -x ytsearch:$argv -o /tmp/yt-audio.opus
    mpv /tmp/yt-audio.opus
    rm /tmp/yt-audio.opus
end
berinaniesh
  • 198
  • 13

1 Answers1

0

There are a few ways to accomplish this.

  1. The simplest solution is to call the function with a single argument, that is enclose the search term inside double quotes. For example
# Instead of 
$ ytaudio unity fat rat
# Use
$ ytaudio "unity fat rat"

The problem with this solution is that we have to type quotes to enclose the search term each time we call the function. It might not seem like much, but it is annoying if the function is called a lot of times.

  1. Enclose $argv within quotes inside the function. Change the first line of the function to the line below.
yt-dlp -f 251 -x ytsearch:"$argv" -o /tmp/yt-audio.opus
  1. Use string collect of fish. Replace the first line of the function with the lines below.
set search (echo $argv | string collect)
yt-dlp -f 251 -x ytsearch:$search -o /tmp/yt-audio.opus
  1. Using string join of fish (thanks to @glennjackman). Fish has an easy and awesome way to concatenate strings. Replace the first line of the function with the two lines below.
set search (echo $argv | string join " ")
yt-dlp -f 251 -x ytsearch:$search -o /tmp/yt-audio.opus

string join " " means to join strings with a space character.

In the second, third and fourth solution, the function can be called without quotes for the search term

ytaudio unity fat rat
berinaniesh
  • 198
  • 13