バッチファイル実行後にバルーンを表示させる
Intro
バッチファイル実行後にバルーンを表示させるvimscriptを書いてみました。
以下の機能を持ちます
- ファイル実行時(バッチファイル)の結果をScratchバッファに出力する
- 実行終了と同時にバルーンを表示させる
バルーンの表示に、使用したソフトは以下のとおりです。
ns9log - Balloonly: Windowsでballoon ToolTipを表示するだけのプログラムCommentsAdd Startoton
http://d.hatena.ne.jp/ns9tks/20091217/1261056787
実行前 | 実行後 |
---|---|
test.batを読み込んでいる状態 | バルーンが表示される、Scratchに実行結果を出力 |
動作環境
導入手順
- Balloonlyを入手する
- gvim.exeの入っているフォルダにballoonly.exeを入れる
もしくは、置いた場所にパスを通す - .vimrcに追記か、ファイル名を付けて/pluginフォルダに保存する
- テスト用にtest.batを作成する
Contents
Vimscript Source
.vimrcに以下を追加するか、ファイル名(ex:execbat.vim)を付けて保存して/pluginフォルダにでも配置する
"----------------------------------------------------------------------------- " 設定 "----------------------------------------------------------------------------- " 上書き:write, 追記:add let g:output_buffer_mode = "add" " 0 - 30000 msec let g:exec_batfile_compleate_sec = 5000 " balloで表示させる文字列 let g:exec_batfile_compleate_msg = "Compleate" " 現在のバッファをstartコマンドで実行する nnoremap <silent><SPACE>xx :call ExecEndBallo()<CR> " 補助関数は以下のサイトで掲載されていたものを改変して使用 " http://vimwiki.net/?tips%2F107 "----------------------------------------------------------------------------- " 補助関数 "----------------------------------------------------------------------------- " 指定した名前を持つバッファが既に存在するならそこにカーソルを移動。 " 存在しないなら作成。 function! SingletonBuffer(bufnr, split) let winlist = FindWindowsByBufnr(a:bufnr) if empty(winlist) if a:split split endif exe "b " . a:bufnr else exe winlist[0] . "wincmd w" endif endfunction " 指定したバッファを含んでいるウィンドウ番号のリストを返す。 function! FindWindowsByBufnr(bufnr) return filter(range(1, winnr("$")), 'winbufnr(v:val)==' . a:bufnr) endfunction " 上書き:write, 追記:add let g:output_buffer_mode = "add" " 0 - 30000 msec let g:exec_batfile_compleate_sec = 5000 " balloで表示させる文字列 let g:exec_batfile_compleate_msg = "Compleate" function! ExecEndBallo() " 実行するバッチファイル let a:exec_batfile_fullpath = shellescape(expand("%:p")) let a:exec_batfile_name = bufname("%") if exists("g:output_buffer_bname") if (bufexists(g:output_buffer_bname) > 0) let a:bufexists_flag = 1 else let a:bufexists_flag = 0 endif else let a:bufexists_flag = 0 endif " Buffer Exists if (a:bufexists_flag) " 存在する場合は、そのバッファを呼び出す call SingletonBuffer(g:output_buffer_nr, 1) else " Scratchバッファ作成 execute "Scratch" let g:output_buffer_bname = bufexists("%") let g:output_buffer_nr = bufnr("%") endif if (g:output_buffer_mode == "write") " 既存の内容を削除 execute "%delete" let a:position = getpos(".") else execute "%" let a:position = getpos(".") endif execute "r!echo /--------------------------------------------/" execute "r!echo / Exec File : " . a:exec_batfile_name execute "r!echo / Exec Full : " . a:exec_batfile_fullpath execute "r!echo / Output Date : " . strftime("%Y/%m/%d %H:%M:%S") execute "r!echo /--------------------------------------------/" execute "r!echo /--------------------------------------------/" execute "r!echo / Start Execute /" execute "r!echo /--------------------------------------------/" " 標準出力を読み込み execute "r!" . a:exec_batfile_fullpath if (executable("balloonly.exe")) execute '!start balloonly.exe ' \ . g:exec_batfile_compleate_sec . ' "' \ . a:exec_batfile_name . '" "' \. g:exec_batfile_compleate_msg .'"' endif execute "r!echo /--------------------------------------------/" execute "r!echo / End Execute /" execute "r!echo /--------------------------------------------/" "実行開始位置に戻る call setpos(".", a:position) endfunction
BAT File
test.batとして、以下を好きな場所に保存する
(実行終了まで3秒くらい時間がかかるバッチファイル)
@echo off rem 以下を参考にtest.batを作成した rem http://trash-area.com/archives/219 echo testプログラム cd "C:\" ping localhost -n 2 > nul ping localhost -n 2 > nul ping localhost -n 2 > nul dir /a
Comment
- 調べてもバッファの操作する関数が上手く見つけられない
- 内部コマンドの実行結果をバッファに出力するやり方がこれしか分からなかった(r!echo 内容)
もっと良いやり方がある気がする - vimscriptで頑張らなくてもバッチファイルの最後尾にballoonly.exeを書くだけでも、バルーンは表示できる
- bitbucketにソースファイル挙げようと思ったけど、まだ色々悩ましい部分が実はあるのでひとまずソース丸ごと掲載して公開
まだ完成というより、"動いた!!"という感じでもっと煮詰めたい部分があったり。