这些空间来自哪里?

时间:2014-10-17 15:54:48

标签: autocomplete terminal tcl

我在这里看不到什么...尝试创建一个简单的脚本来获取用户输入,仅限于常规字符(az,AZ,0-9和 - ),并使用选项卡自动完成定义的短语列表。 ..工作,有点......

#!/usr/local/scripts/packages/bin/expect

set completes { "red one" "blue two" "green three" "four more"  }

proc enableRaw {{channel stdin}} {
   exec /bin/stty raw -echo <@$channel
}

proc disableRaw {{channel stdin}} {
   exec /bin/stty -raw echo <@$channel
}

proc complete { partial } {
  global completes
  set index [lsearch $completes "${partial}*"]
  if { $index != -1 } {
    return [lindex $completes $index]
  } else {
    return $partial
  }
}

enableRaw

set c [read stdin 1]
scan $c %c ascii
set word ""
while {$ascii != 10} {
    switch $ascii {
      9 {
        # TAB
        set word [complete $word]
        puts -nonewline "\n$word"
      }
      127 {A
        # BACKSPACE
        puts -nonewline $c
        set word [string range $word 0 [expr [string length $word] - 2]]
      }
      default {
        switch -regex " $c" {
          " [ a-zA-Z0-9\-]" {
            puts -nonewline $c
            set word "$word$c"
          }
          default {}
        }
      }
    }
    flush stdout
    set c [read stdin 1]
    scan $c %c ascii
}

disableRaw
puts "\n>$word<"

但是输出会增加一些额外的空间,我不知道它们来自哪里!!!

我希望:

bash-3.2$ ./complete_on_tab.exp
red                                    <- tab entered
red one
>red one<
bash-3.2$

但我看到了:

bash-3.2$ ./complete_on_tab.exp
red                                    <- tab entered
   red one                             <- why the indent?
>red one<
bash-3.2$

第二行之前的空格来自哪里?

0 个答案:

没有答案