Last active
May 14, 2017 00:07
-
-
Save tsuyoshicho/1e6288193047d782b576d8cf5bf9bc13 to your computer and use it in GitHub Desktop.
Nyagos リポジトリブランチ名表示 プロンプトの一例 ref: http://qiita.com/tsuyoshi_cho/items/d029825b6d8d3688da92
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- comment | |
| -- prompt | |
| -- 関数定義がうまくいかないので、クロージャ内のローカル関数にする | |
| nyagos.prompt = function(this) | |
| ------------------------------------------------ | |
| -- strをpatで分割しテーブルを返す | |
| -- code from 'http://lua-users.org/wiki/SplitJoin' | |
| local function split(str, pat) | |
| local t = {} -- NOTE: use {n = 0} in Lua-5.0 | |
| local fpat = "(.-)" .. pat | |
| local last_end = 1 | |
| local s, e, cap = str:find(fpat, 1) | |
| while s do | |
| if s ~= 1 or cap ~= "" then | |
| table.insert(t, cap) | |
| end | |
| last_end = e+1 | |
| s, e, cap = str:find(fpat, last_end) | |
| end | |
| if last_end <= #str then | |
| cap = str:sub(last_end) | |
| table.insert(t, cap) | |
| end | |
| return t | |
| end | |
| ------------------------------------------------ | |
| -- srcの末尾から改行を取り除く | |
| local function chomp(src) | |
| return string.gsub(src, "[\r\n]+$", "") | |
| end | |
| ------------------------------------------------ | |
| -- 最下層nのディレクトリ名だけ表示する文字列生成 | |
| -- fix nyagos 4.0x api | |
| local function getCompressedPath(num) | |
| local wd = nyagos.getwd() | |
| local env = nyagos.env | |
| local path = chomp(wd) | |
| local buff = path | |
| local drive = nil | |
| -- HOME以下の場合 | |
| local home = env.home or env.userprofile | |
| if path:find(home)then | |
| drive = '~' | |
| path = path:gsub(home, '~') | |
| buff = path | |
| end | |
| -- 通常のドライブ | |
| if drive == nil then | |
| drive = buff:match('(%w+:)\\') | |
| buff = buff:gsub('%w+:\\', '') | |
| end | |
| -- UNCパス | |
| if drive == nil then | |
| drive = buff:match('(\\\\.-)\\') | |
| buff = buff:gsub('\\\\.-\\', '') | |
| end | |
| local tbl = split(buff, "[\\/]") | |
| if #tbl > num then | |
| buff = "/..." | |
| for i = #tbl - (num - 1), #tbl do | |
| buff = buff .. '/' .. tbl[i] | |
| end | |
| path = drive .. buff | |
| end | |
| return path | |
| end | |
| ------------------------------------------------ | |
| -- エスケープシーケンスを削除 | |
| local function removeEscapeSequence(src) | |
| -- FIXME : なぜか'$e%[(%d+;)+1m'でマッチしない | |
| return src:gsub('$e%[%d+;%d+;1m',''):gsub('$e%[%d+;1m','') | |
| end | |
| ------------------------------------------------ | |
| -- 文字列の幅を取得 | |
| -- 半角文字:1, 全角文字:2 にカウント | |
| local function getStringWidth(src) | |
| local width = 0 | |
| for p, c in utf8.codes(src) do | |
| if (0 ~= bit32.band(c, 0x7FFFFF80)) then | |
| if (0xFF61 <= c and c <= 0xFF9F) then | |
| width = width + 1 | |
| else | |
| width = width + 2 | |
| end | |
| else | |
| width = width + 1 | |
| end | |
| end | |
| return width | |
| end | |
| ------------------------------------------------ | |
| -- PROMPT生成部分 | |
| -- branch name append | |
| local function makePrompt(pathBlock) | |
| local prompt = '' | |
| if (pathBlock ~= '') then | |
| prompt = pathBlock | |
| else | |
| prompt = '$e[30;40;1m[' .. getCompressedPath(3):gsub('\\', '/') .. ']$e[37;1m' | |
| end | |
| local hgbranch = nyagos.eval('hg branch 2> nul') | |
| local gitbranch = nyagos.eval('git rev-parse --abbrev-ref HEAD 2> nul') | |
| local rprompt = '' | |
| if (hgbranch ~= '') then | |
| rprompt = rprompt .. '$e[30;40;1m[$e[33;40;1m' .. hgbranch .. '$e[30;40;1m]$e[37;1m' | |
| end | |
| if (gitbranch ~= '') then | |
| rprompt = rprompt .. '$e[30;40;1m[$e[33;40;1m' .. gitbranch .. '$e[30;40;1m]$e[37;1m' | |
| end | |
| local pad = nyagos.getviewwidth() - getStringWidth(removeEscapeSequence(prompt .. rprompt)) | |
| for i = 1, pad-1 do | |
| prompt = prompt .. ' ' | |
| end | |
| return prompt .. rprompt .. '\n$ ' | |
| end | |
| ------------------------------------------------ | |
| -- path,title,prompt | |
| local path = getCompressedPath(3):gsub('\\', '/') | |
| local title = '' | |
| local pathBlock = '' | |
| if nyagos.elevated() then | |
| title = path .. " - NYAGOS(admin)" | |
| pathBlock = '$e[30;40;1m[$e[40;31;1m'..path..'$e[30;40;1m]$e[37;1m' | |
| else | |
| title = path .. " - NYAGOS" | |
| pathBlock = '$e[30;40;1m[$e[40;36;1m'..path..'$e[30;40;1m]$e[37;1m' | |
| end | |
| return nyagos.default_prompt(makePrompt(pathBlock),title) | |
| end | |
| ------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment