All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cedarsoft.utils.AbstractCmdLine Maven / Gradle / Ivy

package eu.cedarsoft.utils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.List;

/**
 * Base class that delegates all methods to the a few core methods.
 */
public abstract class AbstractCmdLine implements CmdLine {
  public void warning( @NotNull String message, @NotNull Object... objects ) {
    getOut().println( getConsolePrinter().createWarning( message, objects ) );
  }

  public void error( @NotNull String message, @NotNull Object... objects ) {
    getOut().println( getConsolePrinter().createError( message, objects ) );
  }

  public void success( @NotNull String message, @NotNull Object... objects ) {
    getOut().println( getConsolePrinter().createSuccess( message, objects ) );
  }

  public void outNl() {
    getOut().println();
  }

  public void out( @NotNull String message, @NotNull Object... objects ) {
    if ( objects.length == 0 ) {
      getOut().println( message );
    } else {
      getOut().println( MessageFormat.format( message, objects ) );
    }
  }

  public void out( @NotNull Process process ) {
    try {
      BufferedReader defaultIn = null;
      try {
        defaultIn = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
        String line;
        while ( ( line = defaultIn.readLine() ) != null ) {
          out( line );
        }
      } finally {
        if ( defaultIn != null ) {
          defaultIn.close();
        }
      }

      BufferedReader errorIn = null;
      try {
        errorIn = new BufferedReader( new InputStreamReader( process.getErrorStream() ) );
        String line;
        while ( ( line = errorIn.readLine() ) != null ) {
          error( line );
        }
      } finally {
        if ( errorIn != null ) {
          errorIn.close();
        }
      }
    } catch ( IOException e ) {
      throw new RuntimeException( e );
    }
  }

  @NotNull
  public  T readSelection( @NotNull String message, @NotNull List elements ) {
    return readSelection( message, elements, null );
  }

  protected  void printPossibleElements( @NotNull List elements, @Nullable Renderer presenter ) {
    if ( presenter == null ) {
      //noinspection AssignmentToMethodParameter
      presenter = new DefaultRenderer();
    }
    int index = 0;
    for ( T element : elements ) {
      out( "\t" + index + '\t' + presenter.render( element, null ) );
      index++;
    }
  }

  @NotNull
  public  T readSelection( @NotNull String message, @NotNull List elements, @Nullable Renderer presenter ) {
    out( message );
    printPossibleElements( elements, presenter );
    return elements.get( readInt( "Enter the number of the element you want to select", 0, elements.size() ) );
  }

  @NotNull
  public String read( @NotNull String message, @NotNull List elements ) {
    return read( message, elements, null );
  }

  @NotNull
  public String read( @NotNull String message, @NotNull List elements, @Nullable Renderer presenter ) {
    out( message );
    printPossibleElements( elements, presenter );

    String read = read( "Enter the value. Or enter the number of the element you want to select" );
    try {
      int index = Integer.parseInt( read );
      if ( index >= 0 && index < elements.size() ) {
        return elements.get( index );
      }
    } catch ( NumberFormatException ignore ) {
    }

    return read;
  }

  @NotNull
  public abstract PrintStream getOut();

  @NotNull
  protected abstract ConsolePrinter getConsolePrinter();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy