microsoft-visualbasic-runtime/ApplicationServices/Terminal/InteractiveIODevice/Shell.vb

110 lines
3.5 KiB
VB.net
Raw Normal View History

2020-05-21 22:27:25 +08:00
#Region "Microsoft.VisualBasic::891f0552ada4acc03a6ba0492cc131f9, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\InteractiveIODevice\Shell.vb"
2018-08-02 20:14:48 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class Shell
'
2020-05-21 22:27:25 +08:00
' Properties: autoCompleteCandidates, dev, History, ps1, Quite
' shell
2018-08-02 20:14:48 +08:00
'
' Constructor: (+1 Overloads) Sub New
' Sub: Run
'
'
' /********************************************************************************/
#End Region
Imports System.IO
Imports Microsoft.VisualBasic.Language
2018-08-02 20:14:48 +08:00
Imports Microsoft.VisualBasic.Language.UnixBash
2020-03-20 21:21:55 +08:00
Namespace ApplicationServices.Terminal
2018-08-02 20:14:48 +08:00
''' <summary>
''' Shell model for console.
''' </summary>
2018-08-02 20:14:48 +08:00
Public Class Shell
Public ReadOnly Property ps1 As PS1
Public ReadOnly Property shell As Action(Of String)
''' <summary>
''' a candidate list for implements auto-complete for console input.
''' </summary>
''' <returns></returns>
Public ReadOnly Property autoCompleteCandidates As New List(Of String)
Public ReadOnly Property dev As TextReader
2018-08-02 20:14:48 +08:00
''' <summary>
''' Command text for exit the shell loop
2019-10-31 19:05:03 +08:00
'''
''' (默认的退出文本为vim的 ``:q`` 命令)
''' </summary>
''' <returns></returns>
2018-08-02 20:14:48 +08:00
Public Property Quite As String = ":q"
Public Property History As String = ":h"
''' <summary>
'''
''' </summary>
''' <param name="ps1">The commandline prompt prefix headers.</param>
''' <param name="exec">How to execute the command line input.</param>
Sub New(ps1 As PS1, exec As Action(Of String), Optional dev As TextReader = Nothing)
Me.ps1 = ps1
Me.shell = exec
Me.dev = dev Or App.StdInput
2018-08-02 20:14:48 +08:00
End Sub
''' <summary>
''' 执行一个配置好的命令行模型, 代码会被一直阻塞在这里
''' </summary>
2018-08-02 20:14:48 +08:00
Public Sub Run()
Dim cli As Value(Of String) = ""
2018-08-02 20:14:48 +08:00
Do While True
Call Console.Write(ps1.ToString)
2018-08-02 20:14:48 +08:00
2020-03-19 20:39:53 +08:00
If Strings.Trim((cli = Console.ReadLine)).StringEmpty Then
Call _shell(cli)
Continue Do
End If
2018-08-02 20:14:48 +08:00
If cli.Value.TextEquals(Quite) Then
2018-08-02 20:14:48 +08:00
Exit Do
Else
Call _shell(cli)
2018-08-02 20:14:48 +08:00
End If
Loop
End Sub
End Class
End Namespace