LoginSignup
6
7

More than 5 years have passed since last update.

nyagosでgitの状態を表示してみたかった

Last updated at Posted at 2015-08-26

追記

初稿で投稿したのはとんでもないバグの塊だったので直しました。
あまりに長くなったのでどう考えても別ファイルにしたほうが良い

はじめに

自分は物忘れが激しく、
addし忘れたり、commitし忘れたり、pushし忘れたり、branch切り忘れたり色々とひどい。
なのでgitの状態をコンソール上で表示したかった。

スクリーンショット

なんで、C:\直下にProjectなんてフォルダ作ってるんだよ!って突っ込みはご容赦いただきたい。
以前、%USERPROFILE%に半角スペースのあるフォルダで開発しており、相当痛い目にあってきたのだ。
現在Win10以外のWinOSがないので、nyagos.exeの単体動作は他のWinと少し違うかもしれない。

nyagos.exe

image

conemu

image

.nyagos晒し

-- This is a sample .nyagos written with Lua.
-- Edit and put it on %USERPROFILE% or %HOME%

-- Simple Prompt for CMD.EXE
set{
    PROMPT='[$P]['..os.date("%Y-%m-%d %H:%M:%S") ..']'
}

-- code from 'http://lua-users.org/wiki/SplitJoin'
string.split = function (str, pat)
    local t = {}
    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

function gis()
  local gs = {}
  local c  = {}
  c.dc     = "$e[36;40;1m" -- default color
  c.bc     = "$e[33;1m" -- brunch color
  c.ok     = "$e[32;1m✔"
  c.okc    = "$e[32;1m" -- color only
  c.ng     = "$e[31;1m○"
  c.ngc    = "$e[31;1m" -- color only

  gs.gitbool = nyagos.eval('git rev-parse --is-inside-work-tree 2>nul')
  if gs.gitbool == 'true' then
    --nyagos.evalは出力をtrimする(?)ので--branch必須
    gs.gitst = nyagos.eval('git status --porcelain --branch 2>nul')
    local t = {}
    t = gs.gitst:split("\n")
    for i, v in ipairs(t) do
      if i == 1 then
        gs.remote = v:match("^##.*")
        -- local branch name
        gs.lbn = nyagos.eval("git rev-parse --abbrev-ref HEAD 2>nul")
        -- ahead or behind
        if gs.remote then
          -- remote branch name
          gs.rbn = gs.remote:match("[^%.]%w*/"..gs.lbn) or c.ng.."/"..gs.lbn
          gs.bst = gs.remote:match("%[.*%]")
          if gs.bst then
            local ab = gs.bst:gsub("[%[%]]", "")
            ab = ab:split(",")
            for i, v in ipairs(ab) do
              local oltab = v:split(" ")
              local lastlng = oltab[2] == "1" and " commit." or " commits."
              if oltab[1] == "ahead" then
                -- ahead
                gs.ahead = c.ngc..oltab[1].." "..c.ngc..oltab[2]..lastlng
                gs.push = c.ng
              elseif oltab[1] == "behind" then
                -- behind
                gs.behind = c.ngc..oltab[1].." "..c.ngc..oltab[2]..lastlng
                gs.pull = c.ng
              end
              gs.ahead = gs.ahead or ""
              gs.behind = gs.behind or ""
              gs.synced = gs.synced or ""
            end
          else
            gs.ahead = gs.ahead or ""
            gs.behind = gs.behind or ""
            if gs.rbn == c.ng.."/"..gs.lbn then
              gs.synced =  c.ng.."Not Yet Add Remote Repository!"
              gs.push = gs.push or c.bc.."-"
              gs.pull = gs.pull or c.bc.."-"
            else
              gs.synced = c.ok.."All Synced!"
              gs.push = gs.push or c.ok
              gs.pull = gs.pull or c.ok
            end
          end
        end
      else
        -- untrackcheck
        if gs.add ~= c.ng then
          gs.add = v:match("^%?%?") and c.ng or c.ok
        end
        -- modifycheck
        if gs.mod ~= c.ng then
          gs.mod = v:match("M%s[^%s]") and c.ng or c.ok
        end
        -- commitcheck
        if gs.commit ~= c.ng then
          gs.commit = v:match("[AM]%s%s") and c.ng or c.ok
        end
      end
    end
    gs.add = gs.add or c.ok
    gs.mod = gs.mod or c.ok
    gs.commit = gs.commit or c.ok
    gs.push = gs.push or c.ok
    gs.pull = gs.pull or c.ok
    gs.result = c.dc.."$_(Untrack:"..gs.add..c.dc.." Modify:"..gs.mod..c.dc.." Commit:"..gs.commit..c.dc.." Push:"..gs.push..c.dc.." Pull:"..gs.pull..c.dc..c.dc.." Branch:("..c.bc..gs.rbn..c.dc..")["..gs.ahead..gs.behind..gs.synced..c.dc.."]"..c.dc..")"
  end
  return gs.result
end

-- Coloring Prompt for NYAGOS.exe
local prompter = nyagos.prompt
nyagos.prompt = function(this)
    if gis() then
      branch = '$e[36;40;1m'..this..gis()..'$_$e[31;1m$$$s'..'$e[37;1m'
    else
      branch = '$e[36;40;1m'..this..'$_$e[31;1m$$$s'..'$e[37;1m'
    end
    return prompter(branch)
end



-- vim:set ft=lua: --

やってること

やってる事自体は非常に単純かつカッコ悪い。

  • gitリポジトリかをチェック
  • 未追跡のものがないかチェック
  • コミットされていないものがないかチェック
  • プッシュされていないものがないかチェック
  • 現在のブランチを取得

上記を行い、文字列が出ないようにエラーコードは捨てている。
エラーコード捨てんなや!と怒られそうだが、捨てないとgit init直後などがやかましい状態になってしまうのです。
最初はstateによってtrue, falseを返す関数を作っていたのだが、死ぬほど重くなったのでテーブルにぶち込んで使っている。

6
7
0

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
6
7