1 | #Region "Microsoft.VisualBasic::581f46ffd31dd39cc2a9383be95c77ba, Microsoft.VisualBasic.Core\CommandLine\Interpreters\InteractiveConsole.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 InteractiveConsole |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' |
38 | ' Function: RunApp |
39 | ' |
40 | ' Sub: RunAppInternal |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Imports System.IO |
48 | Imports Microsoft.VisualBasic.ApplicationServices.Terminal |
49 | Imports Microsoft.VisualBasic.Language |
50 | Imports Microsoft.VisualBasic.Language.UnixBash |
51 | Imports Microsoft.VisualBasic.Linq |
52 | Imports Microsoft.VisualBasic.Scripting.Expressions |
53 | |
54 | Namespace CommandLine |
55 | |
56 | ''' <summary> |
57 | ''' interactive console in ``/i`` mode, example: |
58 | ''' |
59 | ''' ```bash |
60 | ''' # This command will makes your CLI program enter |
61 | ''' # the interactive console mode. |
62 | ''' # |
63 | ''' # type quit() for exit. |
64 | ''' # |
65 | ''' App /i |
66 | ''' ``` |
67 | ''' </summary> |
68 | ''' <remarks></remarks> |
69 | Public NotInheritable Class InteractiveConsole : Inherits Interpreter |
70 | |
71 | Sub New(App As Type) |
72 | Call MyBase.New(App) |
73 | End Sub |
74 | |
75 | Public Function RunApp() As Integer |
76 | Dim input As Value(Of String) = "" |
77 | Dim ps1 As PS1 = PS1.Fedora12 |
78 | |
79 | Call Console.Write(ps1.ToString) |
80 | Call Console.Write(" ") |
81 | |
82 | Do While Not (input = Console.ReadLine).TextEquals("quit()") |
83 | With input.Value |
84 | If Not .StringEmpty Then |
85 | Call RunAppInternal(CLITools.TryParse(.ByRef)) |
86 | End If |
87 | End With |
88 | |
89 | Call Console.Write(ps1.ToString) |
90 | Call Console.Write(" ") |
91 | Loop |
92 | |
93 | Return 0 |
94 | End Function |
95 | |
96 | ''' <summary> |
97 | ''' Contains sevral build in command about file system operation and the program CLI interpreter commands |
98 | ''' </summary> |
99 | ''' <param name="cmd"></param> |
100 | Private Sub RunAppInternal(cmd As CommandLine) |
101 | Select Case cmd.Name.ToLower |
102 | |
103 | Case "ls" ' list directory |
104 | |
105 | Dim directory$ = If(cmd.Tokens.ElementAtOrDefault(1), App.CurrentDirectory) |
106 | Dim directories = FileIO.FileSystem.GetDirectories(directory) |
107 | Dim files = FileIO.FileSystem.GetFiles(directory) |
108 | |
109 | For Each dir As String In directories |
110 | Call Console.WriteLine("<directory> " & dir.Replace("\"c, "/"c).Split("/"c).Last) |
111 | Next |
112 | |
113 | Dim table = files _ |
114 | .Select(Function(path) |
115 | Return { |
116 | path.Replace("\"c, "/"c).Split("/"c).Last, |
117 | New FileInfo(path).Length & " Bytes" |
118 | } |
119 | End Function) _ |
120 | .ToArray |
121 | |
122 | Call PrintAsTable.Print(table, New StreamWriter(Console.OpenStandardOutput)) |
123 | |
124 | Case "cd" ' change directory |
125 | |
126 | App.CurrentDirectory = cmd.Tokens.ElementAtOrDefault(1) |
127 | |
128 | Case "cat" ' display text file content |
129 | |
130 | Call Console.WriteLine(cmd.Tokens.ElementAtOrDefault(1).ReadAllText) |
131 | |
132 | Case "help" ' view commandline help |
133 | |
134 | Call MyBase.Execute(args:=New CommandLine With {.Name = "?"}) |
135 | |
136 | Case "/@set" |
137 | |
138 | ' /@set var value |
139 | Dim var$ = cmd.Tokens.ElementAtOrDefault(1) |
140 | Dim value$ = cmd _ |
141 | .Tokens _ |
142 | .ElementAtOrDefault(2) _ |
143 | .Interpolate(AddressOf App.GetVariable, escape:=False) |
144 | |
145 | Call App.JoinVariable(var, value) |
146 | |
147 | Case "/@get" |
148 | |
149 | ' /@get var |
150 | Call Console.WriteLine(App.GetVariable(cmd.Tokens.ElementAtOrDefault(1))) |
151 | |
152 | Case Else |
153 | |
154 | Call MyBase.Execute(args:=cmd) |
155 | |
156 | End Select |
157 | End Sub |
158 | End Class |
159 | End Namespace |