1 #Region "Microsoft.VisualBasic::f9bc48f01d419a67eab0db3c5464136d, Microsoft.VisualBasic.Core\Language\Language\UnixBash\PS1.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class PS1
35     
36     '         Constructor: (+2 OverloadsSub New
37     '         Function: A, d, Fedora12, n, r
38     '                   T, tl, ToString, u, v
39     '                   W, wl
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     ''' <summary>
53     ''' PS (Prompt Sign)
54     ''' </summary>
55     Public Class PS1 : Inherits BaseClass
56
57         ''' <summary>
58         ''' ``\H`` 完整的主机名称
59         ''' </summary>
60         Shared ReadOnly H As String
61         ''' <summary>
62         ''' ``\h`` 仅取主机名称的第一个名字
63         ''' </summary>
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         ''' <summary>
82         ''' 
83         ''' </summary>
84         ''' <param name="ps1"></param>
85         ''' <remarks>
86         ''' Example as:
87         ''' 
88         ''' ```vbnet
89         ''' ' Fedora 12
90         ''' PS1='[\u@\h \w \A #\#]\$ '
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         ''' <summary>
112         ''' Fedora 12 PS1 variable of the bash shell: ``[\u@\h \w \A #\#]\$ ``
113         ''' </summary>
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         ''' <summary>
137         ''' Display the commandline prompt.
138         ''' </summary>
139         ''' <returns></returns>
140         Public Overrides Function ToString() As String
141             Return __ps1()
142         End Function
143
144         Dim __n As Integer
145
146         ''' <summary>
147         ''' ``\#`` 下达的第几个指令。
148         ''' </summary>
149         ''' <returns></returns>
150         Public Function n() As String
151             __n += 1
152             Return __n.ToString
153         End Function
154
155         ''' <summary>
156         ''' ``\$`` 提示字符,如果是``root``时,提示字符为``#``,否则就是``$``。
157         ''' </summary>
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         ''' <summary>
169         ''' ``\T`` 显示时间, 12 小时的时间格式!
170         ''' </summary>
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         ''' <summary>
178         ''' ``\t`` 显示时间, 为 24 小时格式, 如: ``HH:MM:SS``
179         ''' </summary>
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         ''' <summary>
187         ''' ``\A`` 显示时间, 24小时格式: ``HH:MM``
188         ''' </summary>
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         ''' <summary>
196         ''' ``\u`` 目前使用者的账号名称
197         ''' </summary>
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         ''' <summary>
206         ''' ``\v`` ``BASH``的版本信息
207         ''' </summary>
208         ''' <returns></returns>
209         Public Shared Function v() As String
210             Return __version
211         End Function
212
213         ''' <summary>
214         ''' ``\w`` 完整的工作目录名称。家目录会以``~``取代
215         ''' </summary>
216         ''' <returns></returns>
217         Public Shared Function wl() As String
218             Return FileIO.FileSystem.CurrentDirectory
219         End Function
220
221         ''' <summary>
222         ''' ``\W`` 利用``<see cref="basename"/>``取得工作目录名称,所以仅会列出最后一个目录名。
223         ''' </summary>
224         ''' <returns></returns>
225         Public Shared Function W() As String
226             Return FileIO.FileSystem.CurrentDirectory.BaseName
227         End Function
228
229         ''' <summary>
230         ''' ``\d`` 代表日期, 格式为``Weekday Month Date``, 例如``Mon Aug 1``
231         ''' </summary>
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