Prevent Zsh from escaping newlines - macos

I'm using Oh My Zsh on OS X. I'm trying to write an autocomplete function that outputs my combined tmux and tmuxinator sessions.
Here's what my autocomplete function looks like:
tmux-and-tmuxinator-sessions-autofill() {
reply=$( tmux-and-tmuxinator-sessions )
}
compctl -K tmux-and-tmuxinator-sessions-autofill ta
tmux-and-tmuxinator-sessions outputs the following:
dotfiles
landonschropp.com
something
something_else
However, If I type ta and hit tab, I get:
ta dotfiles$'\n'landonschropp.com$'\n'something$'\n'something_else
I'm pretty new to Zsh, so any help would be appreciated.

reply should be an array, not a single string.
tmux-and-tmuxinator-sessions-autofill() {
reply=( $(tmux-and-tmuxinator-sessions) )
}

Related

tmux titles-string not executing shell command

I have the following lines in my ~/.tmux.conf
set-option -g set-titles on
set-option -g set-titles-string "#(whoami)##H: $PWD \"#S\" (#W)#F [#I:#P]"
This has worked in the past but after upgrading to 2.0 shell commands are no longer executed. I now see in my title:
#(whoami)#myhostname.local: /Users/lander [..rest..]
According to the man page, this should work:
status-left string
Display string (by default the session name) to the left of the status
bar. string will be passed through strftime(3) and formats (see
FORMATS) will be expanded. It may also contain any of the following
special character sequences:
Character pair Replaced with
#(shell-command) First line of the command's output
#[attributes] Colour or attribute change
## A literal `#'
Well done for reading the code, it's simple really: set-titles-string switched to using formats which don't expand #(). Patching this is easy, and no, it's not good enough to reinstate status_print() to tmux.h, instead, job expansion should be a separate function and used from status_replace() and format_expand(). No idea when this will get done.
Thanks for playing.
The code that performed the task your are mentioning is no longer present in version 2.0. That's the short answer to the question above. Either the documentation hasn't been updated to reflect this, or this was done accidentally and is a bug.
What follows is exactly why I think this is the case. I'm at the end of my lunch break, so I can't create a patch for this right now. If no one else gets around to fixing this from here, I will take a stab at it this weekend.
I checkout out the git repository and took a look at code changes from version 1.9a -> 2.0.
The function that actually does this replacement is status_replace() in status.c. This still seems to work, as the command processing works in the status lines, which still call this function.
In version 1.9a, this was also called from server_client_set_title() in server-client.c, around line 770. It looks like this:
void
server_client_set_title(struct client *c)
{
struct session *s = c->session;
const char *template;
char *title;
template = options_get_string(&s->options, "set-titles-string");
title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
if (c->title == NULL || strcmp(title, c->title) != 0) {
free(c->title);
c->title = xstrdup(title);
tty_set_title(&c->tty, c->title);
}
free(title);
}
In version 2.0, this call has been replaced with (now down around line 947):
void
server_client_set_title(struct client *c)
{
struct session *s = c->session;
const char *template;
char *title;
struct format_tree *ft;
template = options_get_string(&s->options, "set-titles-string");
ft = format_create();
format_defaults(ft, c, NULL, NULL, NULL);
title = format_expand_time(ft, template, time(NULL));
if (c->title == NULL || strcmp(title, c->title) != 0) {
free(c->title);
c->title = xstrdup(title);
tty_set_title(&c->tty, c->title);
}
free(title);
format_free(ft);
}
It looks like calls to format_expand_time() and status_replace() might be mutually exclusive. That is the part that might take a bit of effort to fix -- getting the old function call back in there without breaking whatever new functionality they've just added.

change braces position with sed

I have multiple source codes in which braces are like this
function()
{
if(...)
{
...
}
else
{
...
}
}
I would like to make it look like this:
function() {
if(...) {
...
}
else {
...
}
}
I've tried some tricks with the sed command, but I can't figure out how to get it working properly. Here is my latest try:
sed ":a; N; $!ba; s/\n{/ {/g" myfile
EDIT -
I managed to have this working with that command:
sed "N;/\n *{/s// {/;P;D"
As usual with sed, I quite don't understand why it works, but it does.
Since you're performing code formatting, can I suggest you use a tool more geared to this, and investigate something like AStyle. Here's the info for your specific issue (brace positioning)

C like preprocessor macros in Bash

I'm not really sure a good name for this question so please rename it if you can think of a better one.
In Bash I have a function that I am using to store certain functions. Consider:
menv_funcs=()
function menv_function {
menv_funcs+=($1)
}
I am then using it in this manner:
menv_function fetch
function fetch {
...
}
I would like to use it like this though:
menv_function fetch {
...
}
Essentially I'm looking for something like the preprocessor macros in C would do but I have been unable to find a way. Any ideas?
As far as I'm aware, you can't directly achieve this. However, I can think of two solutions that may be of interest to you.
First of all, you could just declare the functions as usual, and then obtain the list of declared functions through declare -F. This could be done like:
function fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
menv_funcs+=${l#declare -f }
done < <(declare -F)
Which will cause menv_funcs[#] to list all the functions declared at the point of calling the snippet. Of course, this may catch unwanted functions as well.
To avoid this, you may add some prefix to function names and filter the list:
function menv_fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
if [[ ${l} == 'declare -f menv_'* ]]; then
menv_funcs+=${l#declare -f menv_}
fi
done < <(declare -F)
And if you really want to achieve something like macros, you may try to play with eval:
menv_funcs=()
function menv_function {
local name=${1}
local body=${2}
menv_funcs+=( ${name} )
eval "function ${name} ${body}"
}
menv_function fetch "{
:
}"
But note that you will actually need to quote the whole function body and escape everything appropriately.

Bash alias name from a variable

Is there a way to make a bash alias (or function) with its name coming from a variable?
For instance, is it possible to do something along these lines:
create_alias_with_name() {
alias $1="echo a custom alias"
}
Or something along these lines:
create_func_with_name() {
$1() {
"echo inside a function with a variable name"
}
}
In other words, I would prefer to have some kind of function "factory" that can register functions for me. Is this possible or beyond the capabilities of Bash?
Did you even try it? Your first example works fine.
You can make the second work by adding an eval:
create_func_with_name() {
eval "$1() {
echo inside a function with a variable name
}"
}
just in case, one may use a variable both as a part of the alias name and as a part of the alias command:
alias foo${var1}="bar${var2}"

Error when wrapping code in "while" loop

OK, I am a little embarrassed but I gotta ask anyway. I have a perfectly-running script that I wrapped in an infinite loop with a sleep at the end:
while($true){}
That's all I added was this code and all of the curly braces match so why am I getting this error:
"Missing closing statement block in line x, where x is the last line in the file"
function A(){
}
while($true){
[... Do Stuff ...]
Start-Sleep -s 10
}
I remove the encapuslating while{} and all is fine.
Any ideas?
Well, the syntax posted is correct.
My guess is you retyped this into the browser, I would check all your ( ) to make sure you dont have { } in there by mistake.
Otherwise, need to copy and paste your actual code.

Resources