Class CommandSession

java.lang.Object
org.jline.shell.CommandSession

public class CommandSession extends Object
Encapsulates the execution context for a command, including the terminal, I/O streams, session variables, working directory, and job state.
Since:
4.0
See Also:
  • Constructor Details

    • CommandSession

      public CommandSession()
      Creates a new command session with the system's standard I/O streams. The terminal will be null.
    • CommandSession

      public CommandSession(Terminal terminal)
      Creates a new command session with the specified terminal. I/O streams are derived from the terminal.
      Parameters:
      terminal - the terminal
    • CommandSession

      public CommandSession(Terminal terminal, InputStream in, PrintStream out, PrintStream err)
      Creates a new command session with the specified terminal and I/O streams.
      Parameters:
      terminal - the terminal
      in - the input stream
      out - the output stream
      err - the error stream
  • Method Details

    • terminal

      public Terminal terminal()
      Returns the terminal for this session.
      Returns:
      the terminal, or null if not associated
    • in

      public InputStream in()
      Returns the input stream.
      Returns:
      the input stream
    • out

      public PrintStream out()
      Returns the output stream.
      Returns:
      the output stream
    • setOut

      public void setOut(PrintStream out)
      Sets the output stream.
      Parameters:
      out - the new output stream
    • setIn

      public void setIn(InputStream in)
      Sets the input stream.
      Parameters:
      in - the new input stream
    • err

      public PrintStream err()
      Returns the error stream.
      Returns:
      the error stream
    • setErr

      public void setErr(PrintStream err)
      Sets the error stream.
      Parameters:
      err - the new error stream
    • get

      public Object get(String name)
      Returns the value of a session variable.
      Parameters:
      name - the variable name
      Returns:
      the value, or null if not set
    • put

      public void put(String name, Object value)
      Sets a session variable.
      Parameters:
      name - the variable name
      value - the value
    • remove

      public Object remove(String name)
      Removes a session variable.
      Parameters:
      name - the variable name
      Returns:
      the previous value, or null if not set
    • variables

      public Map<String,Object> variables()
      Returns all session variables.
      Returns:
      unmodifiable view of the variables
    • workingDirectory

      public Path workingDirectory()
      Returns the working directory.
      Returns:
      the working directory, or null
    • setWorkingDirectory

      public void setWorkingDirectory(Path workingDirectory)
      Sets the working directory.
      Parameters:
      workingDirectory - the working directory
    • lastExitCode

      public int lastExitCode()
      Returns the exit code of the last executed command.
      Returns:
      the last exit code
    • setLastExitCode

      public void setLastExitCode(int lastExitCode)
      Sets the exit code of the last executed command.
      Parameters:
      lastExitCode - the exit code
    • foregroundJob

      public Job foregroundJob()
      Returns the current foreground job.
      Returns:
      the foreground job, or null
    • setForegroundJob

      public void setForegroundJob(Job foregroundJob)
      Sets the current foreground job.
      Parameters:
      foregroundJob - the foreground job
    • fork

      public CommandSession fork(InputStream in, PrintStream out, PrintStream err)
      Creates a child session that shares the same terminal but uses independent I/O streams.

      The child session gets a copy of the current variables and working directory. This is used for concurrent pipeline execution where each stage needs its own I/O streams.

      Parameters:
      in - the input stream for the child session
      out - the output stream for the child session
      err - the error stream for the child session
      Returns:
      a new child session