1 #Region "Microsoft.VisualBasic::12826d208ac41a93beab4e252c673311, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\InteractiveIODevice\HistoryStacks.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 HistoryStacks
35     
36     '         Properties: HistoryList
37     
38     '         Constructor: (+2 OverloadsSub New
39     
40     '         Function: __getDefaultPath, __getHistory, MoveFirst, MoveLast, MoveNext
41     '                   MovePrevious, Save, ToString
42     
43     '         Sub: __init, PushStack, StartInitialize
44     '         Structure History
45     
46     '             FunctionToString
47     
48     
49     
50     
51     ' /********************************************************************************/
52
53 #End Region
54
55 Imports System.Xml.Serialization
56 Imports Microsoft.VisualBasic.ComponentModel
57 Imports Microsoft.VisualBasic.Serialization.JSON
58
59 Namespace Terminal
60
61     Public Class HistoryStacks : Inherits ITextFile
62         Implements ISaveHandle
63
64         Dim _historyList As List(Of String)
65         Dim _lsthistory As List(Of History)
66
67         ''' <summary>
68         ''' 指向<see cref="_historyList"></see>
69         ''' </summary>
70         ''' <remarks></remarks>
71         Dim p As Integer
72
73         Public Property HistoryList As List(Of History)
74             Get
75                 Return _lsthistory
76             End Get
77             Set(value As List(Of History))
78                 _lsthistory = value
79             End Set
80         End Property
81
82         Dim LastHistory As History
83
84         Sub New()
85             LastHistory = New History With {
86                 .Date = Now.ToString,
87                 .Histories = New List(Of String)
88             }
89         End Sub
90
91         Sub New(path As String)
92             Call Me.New
93             FilePath = path
94         End Sub
95
96         Public Sub StartInitialize()
97             Call __init()
98             _historyList = (From his As History In _lsthistory Select his.Histories).Unlist
99             p = _historyList.Count - 1
100             If p < 0 Then p = 0
101         End Sub
102
103         Public Sub PushStack(s As String)
104             If _historyList.IsNullOrEmpty Then
105                 Call __init()
106             End If
107
108             Call LastHistory.Histories.Add(s)
109             Call _historyList.Insert(p, s)
110         End Sub
111
112         Private Sub __init()
113             _historyList = New List(Of String)
114             If _lsthistory.IsNullOrEmpty Then _lsthistory = New List(Of History)
115             Call _lsthistory.Add(LastHistory)
116         End Sub
117
118         Public Function MovePrevious() As String
119             p -= 1
120             Return __getHistory()
121         End Function
122
123         Public Function MoveNext() As String
124             p += 1
125             Return __getHistory()
126         End Function
127
128         Public Function MoveFirst() As String
129             p = 0
130             Return __getHistory()
131         End Function
132
133         Public Function MoveLast() As String
134             p = _historyList.Count - 1
135             Return __getHistory()
136         End Function
137
138         Private Function __getHistory() As String
139             If p < 0 Then
140                 p = 0
141             End If
142
143             If _historyList.IsNullOrEmpty Then
144                 Call __init()
145                 Return ""
146             End If
147
148             If p > _historyList.Count - 1 Then
149                 p = _historyList.Count - 1
150             End If
151
152             Dim s As String = _historyList(p)
153             Return s
154         End Function
155
156         Public Structure History
157
158             Public [Date] As String
159             Public Histories As List(Of String)
160
161             Public Overrides Function ToString() As String
162                 Return Me.GetJson
163             End Function
164         End Structure
165
166         Public Overrides Function ToString() As String
167             Return String.Join(";  ", _historyList.Take(3).ToArray) & "......."
168         End Function
169
170         Public Overrides Function Save(Optional Path As String = ""Optional encoding As System.Text.Encoding = NothingAs Boolean
171             Path = MyBase.getPath(Path)
172             Return Me.GetXml.SaveTo(Path, encoding)
173         End Function
174
175         Protected Overrides Function __getDefaultPath() As String
176             Return FilePath
177         End Function
178     End Class
179 End Namespace