-
Notifications
You must be signed in to change notification settings - Fork 0
/
.nyagos
138 lines (130 loc) · 4.5 KB
/
.nyagos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- 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
------------------------------------------------