LoginSignup
2
2

More than 5 years have passed since last update.

Nyagos リポジトリブランチ名表示 プロンプトの一例

Last updated at Posted at 2017-04-13

nyagos 4.1.9 で動くprompt表示(ブランチ名あり)の初期化ファイル
05/14追記 一部修正

以前いろいろなところから寄せ集めてたの(4.0.xくらい用)が動かなくなったので大改造したもの。

参考
http://lua-users.org/wiki/SplitJoin
http://qiita.com/nocd5/items/b9a149c69416da9cddec
http://qiita.com/nocd5/items/be69928401489da1c107

.nyagos
-- 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
------------------------------------------------

こんな風になる
Gyazo caputre for ConEmu

2
2
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2