| 1 |
#Region "Microsoft.VisualBasic::f9bc48f01d419a67eab0db3c5464136d, Microsoft.VisualBasic.Core\Language\Language\UnixBash\PS1.vb"
|
| 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 |
#End Region
|
| 45 |
|
| 46 |
Imports System.Text
|
| 47 |
Imports System.Text.RegularExpressions
|
| 48 |
Imports Microsoft.VisualBasic.ApplicationServices.Development
|
| 49 |
|
| 50 |
Namespace Language.UnixBash
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Public Class PS1 : Inherits BaseClass
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
Shared ReadOnly H As String
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
Shared ReadOnly hl As String
|
| 65 |
|
| 66 |
Shared Sub New()
|
| 67 |
H = Environment.MachineName
|
| 68 |
If H Is Nothing Then
|
| 69 |
H = ""
|
| 70 |
End If
|
| 71 |
hl = H.Split("."c).FirstOrDefault
|
| 72 |
If hl Is Nothing Then
|
| 73 |
hl = ""
|
| 74 |
End If
|
| 75 |
End Sub
|
| 76 |
|
| 77 |
ReadOnly __ps1 As Func(Of String)
|
| 78 |
|
| 79 |
Const ps_regx As String = "\\[dHhtTAuvwW#$]"
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
<param name="ps1"></param>
|
| 85 |
<remarks>
|
| 86 |
Example as:
|
| 87 |
|
| 88 |
```vbnet
|
| 89 |
|
| 90 |
PS1=
|
| 91 |
```
|
| 92 |
</remarks>
|
| 93 |
Sub New(ps1 As String)
|
| 94 |
ps1 = ps1.Replace("\\", "\")
|
| 95 |
|
| 96 |
Dim ms As String() = Regex.Matches(ps1, ps_regx).ToArray
|
| 97 |
|
| 98 |
ps1 = ps1.Replace("\", "\")
|
| 99 |
|
| 100 |
__ps1 = Function() As String
|
| 101 |
Dim sb As New StringBuilder(ps1)
|
| 102 |
|
| 103 |
For Each s As String In ms
|
| 104 |
Call sb.Replace(s, __pss(s)())
|
| 105 |
Next
|
| 106 |
|
| 107 |
Return sb.ToString
|
| 108 |
End Function
|
| 109 |
End Sub
|
| 110 |
|
| 111 |
|
| 112 |
Fedora 12 PS1 variable of the bash shell: ``[\u@\h \w \A #\#]\$ ``
|
| 113 |
|
| 114 |
<returns></returns>
|
| 115 |
Public Shared Function Fedora12() As PS1
|
| 116 |
Return New PS1("[\u@\h \w \A #\#]\$ ")
|
| 117 |
End Function
|
| 118 |
|
| 119 |
ReadOnly __pss As Dictionary(Of String, Func(Of String)) =
|
| 120 |
New Dictionary(Of String, Func(Of String)) From {
|
| 121 |
_
|
| 122 |
{"\d", AddressOf d},
|
| 123 |
{"\H", Function() H},
|
| 124 |
{"\h", Function() hl},
|
| 125 |
{"\t", AddressOf tl},
|
| 126 |
{"\T", AddressOf T},
|
| 127 |
{"\A", AddressOf A},
|
| 128 |
{"\u", AddressOf u},
|
| 129 |
{"\v", AddressOf v},
|
| 130 |
{"\w", AddressOf wl},
|
| 131 |
{"\W", AddressOf W},
|
| 132 |
{"\#", AddressOf n},
|
| 133 |
{"\$", AddressOf r}
|
| 134 |
}
|
| 135 |
|
| 136 |
|
| 137 |
Display the commandline prompt.
|
| 138 |
|
| 139 |
<returns></returns>
|
| 140 |
Public Overrides Function ToString() As String
|
| 141 |
Return __ps1()
|
| 142 |
End Function
|
| 143 |
|
| 144 |
Dim __n As Integer
|
| 145 |
|
| 146 |
|
| 147 |
``\#`` 下达的第几个指令。
|
| 148 |
|
| 149 |
<returns></returns>
|
| 150 |
Public Function n() As String
|
| 151 |
__n += 1
|
| 152 |
Return __n.ToString
|
| 153 |
End Function
|
| 154 |
|
| 155 |
|
| 156 |
``\$`` 提示字符,如果是``root``时,提示字符为``#``,否则就是``$``。
|
| 157 |
|
| 158 |
<returns></returns>
|
| 159 |
Public Shared Function r() As String
|
| 160 |
Dim wk As String = FileIO.FileSystem.CurrentDirectory
|
| 161 |
If String.Equals(wk, "/") OrElse String.Equals(wk, "C:\", StringComparison.OrdinalIgnoreCase) Then
|
| 162 |
Return "#"
|
| 163 |
Else
|
| 164 |
Return "$"
|
| 165 |
End If
|
| 166 |
End Function
|
| 167 |
|
| 168 |
|
| 169 |
``\T`` 显示时间, 12 小时的时间格式!
|
| 170 |
|
| 171 |
<returns></returns>
|
| 172 |
Public Shared Function T() As String
|
| 173 |
Dim dt As Date = Now
|
| 174 |
Return $"{dt.Hour}:{dt.Minute}:{dt.Second}"
|
| 175 |
End Function
|
| 176 |
|
| 177 |
|
| 178 |
``\t`` 显示时间, 为 24 小时格式, 如: ``HH:MM:SS``
|
| 179 |
|
| 180 |
<returns></returns>
|
| 181 |
Public Shared Function tl() As String
|
| 182 |
Dim dt As Date = Now
|
| 183 |
Return $"{dt.Hour}:{dt.Minute}:{dt.Second}"
|
| 184 |
End Function
|
| 185 |
|
| 186 |
|
| 187 |
``\A`` 显示时间, 24小时格式: ``HH:MM``
|
| 188 |
|
| 189 |
<returns></returns>
|
| 190 |
Public Shared Function A() As String
|
| 191 |
Dim dt As Date = Now
|
| 192 |
Return $"{dt.Hour}:{dt.Minute}"
|
| 193 |
End Function
|
| 194 |
|
| 195 |
|
| 196 |
``\u`` 目前使用者的账号名称
|
| 197 |
|
| 198 |
<returns></returns>
|
| 199 |
Public Shared Function u() As String
|
| 200 |
Return Environment.UserName
|
| 201 |
End Function
|
| 202 |
|
| 203 |
Shared ReadOnly __version As String = ApplicationInfoUtils.GetProductVersion(GetType(PS1).Assembly)
|
| 204 |
|
| 205 |
|
| 206 |
``\v`` ``BASH``的版本信息
|
| 207 |
|
| 208 |
<returns></returns>
|
| 209 |
Public Shared Function v() As String
|
| 210 |
Return __version
|
| 211 |
End Function
|
| 212 |
|
| 213 |
|
| 214 |
``\w`` 完整的工作目录名称。家目录会以``~``取代
|
| 215 |
|
| 216 |
<returns></returns>
|
| 217 |
Public Shared Function wl() As String
|
| 218 |
Return FileIO.FileSystem.CurrentDirectory
|
| 219 |
End Function
|
| 220 |
|
| 221 |
|
| 222 |
``\W`` 利用``<see cref="basename"/>``取得工作目录名称,所以仅会列出最后一个目录名。
|
| 223 |
|
| 224 |
<returns></returns>
|
| 225 |
Public Shared Function W() As String
|
| 226 |
Return FileIO.FileSystem.CurrentDirectory.BaseName
|
| 227 |
End Function
|
| 228 |
|
| 229 |
|
| 230 |
``\d`` 代表日期, 格式为``Weekday Month Date``, 例如``Mon Aug 1``
|
| 231 |
|
| 232 |
<returns></returns>
|
| 233 |
Public Shared Function d() As String
|
| 234 |
Dim dt As Date = Now
|
| 235 |
Dim w As String = WeekdayName(Weekday(dt), FirstDayOfWeekValue:=FirstDayOfWeek.Sunday)
|
| 236 |
Dim m As String = MonthName(dt.Month)
|
| 237 |
Return $"{w} {m} {dt.Day}"
|
| 238 |
End Function
|
| 239 |
End Class
|
| 240 |
End Namespace
|