@@ -185,6 +185,133 @@ private static List<WinGetPackage> CreatePackageListFromOutput(string[] output,
185185 return resultList ;
186186 }
187187
188+ /// <summary>
189+ /// Converts a <see cref="System.Collections.Generic.List{T}"/>
190+ /// of output lines from a winget process to a list of <see cref="WGetNET.WinGetPinnedPackage"/>'s.
191+ /// </summary>
192+ /// <param name="output">
193+ /// A <see cref="System.Collections.Generic.List{T}"/> of output lines from a winget process.
194+ /// </param>
195+ /// <returns>
196+ /// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPinnedPackage"/>'s.
197+ /// </returns>
198+ public static List < WinGetPinnedPackage > ToPinnedPackageList ( string [ ] output )
199+ {
200+ //Get top line index.
201+ //The array should always contain this line.
202+ //If it dose not contain this line the resulting out of range exception,
203+ //that will be thrown later, will be catched in the calling method.
204+ int labelLine = ArrayManager . GetEntryContains ( output , "------" ) - 1 ;
205+
206+ int [ ] columnList = GetColumnList ( output [ labelLine ] ) ;
207+
208+ //Remove unneeded output Lines
209+ output = ArrayManager . RemoveRange ( output , 0 , labelLine + 2 ) ;
210+
211+ return CreatePinnedPackageListFromOutput ( output , columnList ) ;
212+ }
213+
214+ /// <summary>
215+ /// Creates a pinned package list from output.
216+ /// </summary>
217+ /// <param name="output">
218+ /// The <see langword="array"/> containing the output.
219+ /// </param>
220+ /// <param name="columnList">
221+ /// A <see cref="System.Int32"/> <see langword="array"/> containing the column start indexes.
222+ /// </param>
223+ /// <returns>
224+ /// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPinnedPackage"/>'s.
225+ /// </returns>
226+ private static List < WinGetPinnedPackage > CreatePinnedPackageListFromOutput ( string [ ] output , int [ ] columnList )
227+ {
228+ List < WinGetPinnedPackage > resultList = new ( ) ;
229+
230+ if ( columnList . Length < 3 )
231+ {
232+ return resultList ;
233+ }
234+
235+ for ( int i = 0 ; i < output . Length ; i ++ )
236+ {
237+ // Stop parsing the output when the end of the list is reached.
238+ #if NETCOREAPP3_1_OR_GREATER
239+ if ( string . IsNullOrWhiteSpace ( output [ i ] ) || output [ i ] . Length < columnList [ ^ 1 ] )
240+ {
241+ break ;
242+ }
243+ #elif NETSTANDARD2_0
244+ if ( string . IsNullOrWhiteSpace ( output [ i ] ) || output [ i ] . Length < columnList [ columnList . Length - 1 ] )
245+ {
246+ break ;
247+ }
248+ #endif
249+
250+ #if NETCOREAPP3_1_OR_GREATER
251+ string packageName = output [ i ] [ columnList [ 0 ] ..columnList [ 1 ] ] . Trim ( ) ;
252+ string packageId = output [ i ] [ columnList [ 1 ] ..columnList [ 2 ] ] . Trim ( ) ;
253+ string packageVersion = output [ i ] [ columnList [ 2 ] ..columnList [ 3 ] ] . Trim ( ) ;
254+ string packageSource = output [ i ] [ columnList [ 3 ] ..columnList [ 4 ] ] . Trim ( ) ;
255+ string pinType = string . Empty ;
256+ string pinnedVersion = string . Empty ;
257+ if ( columnList . Length > 5 )
258+ {
259+ pinType = output [ i ] [ columnList [ 4 ] ..columnList [ 5 ] ] . Trim ( ) ;
260+ pinnedVersion = output [ i ] [ columnList [ 5 ] ..] . Trim ( ) ;
261+ }
262+ else
263+ {
264+ pinType = output [ i ] [ columnList [ 4 ] ..] . Trim ( ) ;
265+ }
266+ #elif NETSTANDARD2_0
267+ string packageName = output [ i ] . Substring ( columnList [ 0 ] , ( columnList [ 1 ] - columnList [ 0 ] ) ) . Trim ( ) ;
268+ string packageId = output [ i ] . Substring ( columnList [ 1 ] , ( columnList [ 2 ] - columnList [ 1 ] ) ) . Trim ( ) ;
269+ string packageVersion = output [ i ] . Substring ( columnList [ 2 ] , ( columnList [ 3 ] - columnList [ 2 ] ) ) . Trim ( ) ;
270+ string packageSource = output [ i ] . Substring ( columnList [ 3 ] , ( columnList [ 4 ] - columnList [ 3 ] ) ) . Trim ( ) ;
271+ string pinType = string . Empty ;
272+ string pinnedVersion = string . Empty ;
273+ if ( columnList . Length > 5 )
274+ {
275+ pinType = output [ i ] . Substring ( columnList [ 4 ] , ( columnList [ 5 ] - columnList [ 4 ] ) ) . Trim ( ) ;
276+ pinnedVersion = output [ i ] . Substring ( columnList [ 5 ] ) . Trim ( ) ;
277+ }
278+ else
279+ {
280+ pinType = output [ i ] . Substring ( columnList [ 4 ] ) . Trim ( ) ;
281+ }
282+ #endif
283+
284+ // Check if the id is shortened
285+ bool isShortenedId = CheckShortenedId ( packageId ) ;
286+ if ( isShortenedId )
287+ {
288+ // Remove the char at the end of the shortened id.
289+ packageId = packageId . Remove ( packageId . Length - 1 ) ;
290+ }
291+
292+ WinGetPinnedPackage package = new ( pinType , pinnedVersion , isShortenedId )
293+ {
294+ Name = packageName ,
295+ Id = packageId ,
296+ Version = packageVersion ,
297+ AvailableVersion = packageVersion ,
298+ SourceName = packageSource ,
299+ } ;
300+
301+
302+ resultList . Add ( package ) ;
303+ }
304+
305+ // Check for secondery list in output.
306+ if ( ArrayManager . GetEntryContains ( output , "------" ) != - 1 )
307+ {
308+ List < WinGetPinnedPackage > seconderyList = ToPinnedPackageList ( output ) ;
309+ resultList . AddRange ( seconderyList ) ;
310+ }
311+
312+ return resultList ;
313+ }
314+
188315 /// <summary>
189316 /// Converts a <see cref="System.Collections.Generic.List{T}"/>
190317 /// of output lines from a winget process to a list of <see cref="WGetNET.WinGetSource"/>'s.
0 commit comments