package gq.baijie.onetab.client.cli;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import gq.baijie.onetab.api.ProgressOrResult;
import gq.baijie.onetab.api.Result;
import gq.baijie.onetab.api.StorageService;
import gq.baijie.onetab.impl.BasicStorageService;

public class Main {

  public static void main(String[] args) {
    final Result<Configuration, Configurations.FromArgumentsFailure> config =
        Configurations.fromArguments(args);
    if (config.failed()) {
      System.out.printf("args error [%s]: %s%n", config.cause().type, config.cause().cause);
      return;
    }

    final InputStream input;
    try {
      input = Files.newInputStream(config.result().importFilePath);
    } catch (IOException e) {
      System.out.println("Cannot open open: " + e.getMessage());
      return;
    }

    final StorageService storageService = new BasicStorageService();
    storageService.retrieve(StorageService.TYPE_DEFAULT, input)
        .filter(ProgressOrResult::isResult)
        .subscribe(result -> {
          if (result.getResult().failed()) {
            result.getResult().cause().printStackTrace();
          } else {
            result.getResult().result().getSections().forEach(section -> {
              section.getItems()/*.stream()
//                  .filter(item->item.getTitle() == null)
                  .filter(item->item.getTitle().contains("|"))*/
                  .forEach(item -> {
                    if (item.getTitle() != null) {
                      System.out.print(item.getLink() + " | ");
                      System.out.println(item.getTitle());
                    } else {
                      System.out.println(item.getLink());
                    }
                  });
              // add an empty line after section's items
              System.out.println();
            });
          }
        });
  }

}

@@ -116,16 +116,50 @@
 Files;%0A%0A
+import javax.annotation.Nonnull;%0A%0A
 import g
@@ -276,16 +276,56 @@
 ervice;%0A
+import gq.baijie.onetab.api.WebArchive;%0A
 import g
@@ -1251,32 +1251,44 @@
 e %7B%0A            
+printResult(
 result.getResult
@@ -1302,230 +1302,458 @@
 lt()
-.getSections().forEach(section -%3E %7B%0A              section.getItems()/*.stream()%0A//          
+);%0A          %7D%0A        %7D);%0A  %7D%0A%0A  private static void printResult(@Nonnull WebArchive webArchive) %7B%0A    if (webArchive.getSections().isEmpty()) %7B%0A      return;%0A
     
+%7D%0A
     
-.filter(item-%3Eitem.getTitle() == null)%0A                  .filter(item-%3Eitem.getTitle().contains(%22%7C%22))*/%0A                  
+printSection(webArchive.getSections().get(0));%0A    webArchive.getSections().stream().skip(1).forEach(section -%3E %7B%0A      System.out.println();%0A      printSection(section);%0A    %7D);%0A  %7D%0A%0A  private static void printSection(@Nonnull WebArchive.Section section) %7B%0A    section.getItems()
 .for
@@ -1767,30 +1767,16 @@
 em -%3E %7B%0A
-              
       if
@@ -1808,38 +1808,24 @@
 ) %7B%0A        
-       
-       
 System.out.p
@@ -1854,30 +1854,16 @@
 %22 %7C %22);%0A
-              
         
@@ -1895,38 +1895,24 @@
 etTitle());%0A
-              
       %7D else
@@ -1922,30 +1922,16 @@
         
-       
-       
 System.o
@@ -1968,171 +1968,10 @@
     
-              %7D%0A                  %7D);%0A              // add an empty line after section's items%0A              System.out.println();%0A            %7D);%0A          %7D%0A    
+%7D%0A
     

improve the output logic of cli