双引号字符串中使用bash TAB-完成 [英] Bash TAB-completion inside double-quoted string

查看:130
本文介绍了双引号字符串中使用bash TAB-完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我写的命令行(C语言)一个Twitter客户端。我目前正在做TAB-完成Twitter的屏幕名称,像这样:


  

鸣叫@ S<标签>

  @sourcebits @spolsky


不过,我不能让它工作中旬字符串,例如:


  

鸣叫富吧@ S<标签>


由于猛砸将字符串作为一个词。我找不到在Bash的手册页提示一个简单的解决任何事情,所以我决定砍它周围。下面是我的半完成解决问题的方法。我只是分裂空格输入的字符串,取最后一个字(简化,现在)并将其发送到 compgen (变量 $ last_word 下面)。但是,我需要 $preFIX 追加到该TAB-完成生产,因为它替换整个输入字符串(记住字符串的开头:它被视为一个词)。这就是我卡住了。

问题

如何才能做到这一点?

code等。

  __ cltwitter_complete(){
  本地缓存=$ HOME / .cltwitter_users.cache
  本地字符串= $ {COMP_WORDS [COMP_CWORD]}
  当地last_word = $ {字符串## *}
  当地preFIX = $ {字符串%*}  COM prePLY =()  #如果[! -f $ {}缓存] || [`找到$ {}缓存+ -mmin 60`=!]然后
  #cltwitter更新缓存
  #fi  如果[$ last_word== \\@ *]];然后#如果字开头的报价
    last_word = $ {last_word:2}
  ELIF [$ last_word== @ *];然后#如果用户在前面把'@'
    last_word = $ {last_word:1}
  科幻  COM prePLY =($(compgen -W'猫$ cache`-P @ - $ last_word))
}完整-F __cltwitter_complete鸣叫

从Bash的手册页有关章节:


  

COMP_WORDS


  
  

    

一个数组变量(参见下面的数组)包括在个别字词
    当前命令行。这句话被分割在shell元字符作为外壳
    解析器将它们分开
。这个变量只有在shell可用
    功能由可编程完成设施调用。


  

解决方案

看可编程完成:的 http://www.faqs.org/docs/bashman/bashref_103.html

Problem

I'm writing a Twitter client for the command line (in C). I'm currently working on doing TAB-completion for Twitter screen names, like so:

tweet "@s<TAB>
@sourcebits @spolsky

However, I can't get it to work mid-string, e.g.:

tweet "Foo bar @s<TAB>

since Bash treats the string as one word. I couldn't find anything in the Bash man page suggesting a simple fix, so I decided to hack around it. Below is my half-done solution to the problem. I simply split the incoming string by spaces, take the last word (simplification, for now) and send it to compgen (the variable $last_word below). However, I need to append $prefix to the beginning of the string that the TAB-completion produces, as it replaces the whole input string (remember: it's treated as one word). That's where I'm stuck.

Question

How can this be done?

Code etc.

__cltwitter_complete () {
  local cache="$HOME/.cltwitter_users.cache"
  local string=${COMP_WORDS[COMP_CWORD]}
  local last_word=${string##* }
  local prefix=${string% *}

  COMPREPLY=()

  #if [ ! -f ${cache} ] || [ "`find ${cache} -mmin +60`" != "" ]; then
  #  cltwitter-update-cache
  #fi

  if [[ "$last_word" == \"@* ]]; then  # if word is beginning of a quotation
    last_word=${last_word:2}
  elif [[ "$last_word" == @* ]]; then  # if user put '@' in front
    last_word=${last_word:1}
  fi

  COMPREPLY=( $( compgen -W "`cat $cache`" -P @ -- $last_word ) )
}

complete -F __cltwitter_complete tweet

Relevant section from the Bash man page:

COMP_WORDS

An array variable (see Arrays below) consisting of the individual words in the current command line. The words are split on shell metacharacters as the shell parser would separate them. This variable is available only in shell functions invoked by the programmable completion facilities.

解决方案

Look at programmable completion: http://www.faqs.org/docs/bashman/bashref_103.html

这篇关于双引号字符串中使用bash TAB-完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆