LinuxForum.Fans
Тема

Tee для stdin

Автор: Fat-Zer 4 сообщений 2536 просмотров
#237939

нужна утилитка, которая сможет брать вход из нескольких мест и выводить всё это в stdout.
причём не как cat, последовательно, а как только в одном из входов появилось что-то приятное, то сразу выводить.
для чего оно надо: в одном терминале запущен (g)vim, во втором - интерпретатор, можно вводить команды в интерпретатор вручную, а можно выделять текст в вим и посылать в интерпретатор (через трубу например).

#238435

Может что-то наподобие этого:
[console]
tail -f file1.txt file2.txt
[/console]

не, че т это вроде не то

#238452

c tail идея интересная, но с пайпами он как-то криво работает... хотя надо попинать его ещё... на предмет ввода из двух труб и клячей -fFq, а также дескрипторов.

пока пытаюсь на питоне сделать... общая идея такая:

text
#!/usr/bin/python

import threading import sys

class MyThread(threading.Thread): def run(self): while 1: f=open("/tmp/pipe") scrpt = f.read() sys.stdout.write(scrpt) sys.stdout.flush() f.close();

thr = MyThread() thr.start() while 1: s = sys.stdin.read(1) sys.stdout.write(s) sys.stdout.flush()

только надо как-нить аккуратней завершать...

#238667

так... значит вот что у меня получилось:
~/.vim/plugin/piper.vim

Показать
text
" a vim part a script to stream text from vimto some interpreter

let s:username= $USER if strlen(s:username)==0 let s:username= system("id -un")[:-2] endif

if strlen(s:username)==0 echoerr "Can't determin your username" let g:DefaultPiperDir="/tmp/piperiser" else let g:DefaultPiperDir="/tmp/" . s:username . "/piperiser" endif

let s:mainfile = "" let b:scriptpipe=""

nmap <script> <silent> <Plug>PiperSetMainScript :call <SID>ExecuteScript()<CR> nmap <script> <silent> <Plug>PiperResetMainScript :call <SID>ResetMainScript()<CR> nmap <script> <silent> <Plug>PiperPipeScript :call <SID>PipeScript()<CR> vmap <script> <silent> <Plug>PiperPipeScriptSnip :call <SID>PipeScriptSnip()<CR>

function s:SetMainScript() let s:mainfile = bufname('%') echo 'Set' . s:mainfile . ' as the starting program script for the program.' endfunction

function s:ResetMainScript() echo s:mainfile . ' is no longer the starting script.' let s:mainfile = "" endfunction

"function s:PipeMainScript() " how to write specific file? " if strlen(s:mainfile)==0 " echo 'ERROR: no mainfile was seted' " else " write! b:scriptpipe " fi "endfunction

function s:PipeScript() if strlen(b:scriptpipe)==0 echoerr "You haven't seted up the output pipe for the buffer." else exe "write!" b:scriptpipe endif endfunction

function s:PipeScriptSnip() if strlen(b:scriptpipe)==0 echoerr "You haven't seted up the output pipe for the buffer." else exe a:firstline . "," . a:lastline . "write!" b:scriptpipe endif endfunction

~/.vim/tfplugin/python/piper.vim

Показать
text
let b:scriptpipe = DefaultPiperDir . "/python"

" Map keys to special function calls nmap <F11> <Plug>PiperSetMainScript<CR> nmap <C-F11> <Plug>PiperResetMainScript<CR>

nmap <C-X> <Plug>PiperPipeScript<CR> vmap <C-X> <Plug>PiperPipeScriptSnip<CR>

~/.vim/ftplugin/sql/psql_piper.vim

Показать
text
let b:scriptpipe = DefaultPiperDir . "/psql"

" Map keys to special function calls nmap <F11> <Plug>PiperSetMainScript<CR> nmap <C-F11> <Plug>PiperResetMainScript<CR>

nmap <C-X> <Plug>PiperPipeScript<CR> vmap <C-X> <Plug>PiperPipeScriptSnip<CR>

piperise

Показать
text

#!/bin/bash

usage() { echo "usage: $0 <program> [parametrs] ..." }

PIPEDIR=${PIPERISER_PIPEDIR:-"/tmp/${USER:="$(id -un)"}/piperiser"} FMASK="${PAIPERISER_FMASK:-0700}"; PIPE=${PIPE:-"$PIPEDIR/$1"} NORMAL_PIPE="$PIPE"

[ -z "$1" ] && usage && exit 100500 mkdir -p "$(dirname "$PIPE")"

if [ -e "$PIPE" ]; then for I in {1..1024}; do if [ ! -e "$PIPE.$I" ]; then PIPE="$PIPE.$I"; break; fi done echo "pipe was seted to $PIPE"; fi

mknod $PIPE p || ( echo ERROR! && exit 100500 ) chmod $FMASK $PIPE

TMP_HELPER_SCRIPT=$(mktemp)

cat >${TMP_HELPER_SCRIPT} <<EOF #!/usr/bin/python import sys from fcntl import ioctl import termios

while 1: f=open("${PIPE}") txt = f.read() for c in txt: ioctl(sys.stdout.fileno(), termios.TIOCSTI, c) f.close EOF

python $TMP_HELPER_SCRIPT & PIPERISER_HELPER=$!

$@

rm -f $TMP_HELPER_SCRIPT

kill $PIPERISER_HELPER; rm -f $PIPE

чтобы работало интерпретатор надо запускать, как piperise <интерпретатор>
фичи: послать на интерпретацию весь буфер или выделить кусок и послать... по <C-X>. пока выделяются только целые строки, как зделать, чтоб можно было оперировать кусками строк - хз.

установка "мейн-скрипта" пока не работает - не знаю. как записать в файл определённый буфер.

за идею того, как взаимодействовать с интерпретатором спасибо тов. parafin с #gentoo-ru. за помощь с vim-скриптингом спасибо людям на #vim.

замечания/идеи?

Войдите или зарегистрируйтесь, чтобы ответить.