package ssh

import (
	
	
	
	
)

// ErrUnsupported is returned when the platform does not support PTY.
var ErrUnsupported = errors.New("pty unsupported")

// NewPtyWriter creates a writer that handles when the session has a active
// PTY, replacing the \n with \r\n.
func ( io.Writer) io.Writer {
	return ptyWriter{
		w: ,
	}
}

var _ io.Writer = ptyWriter{}

type ptyWriter struct {
	w io.Writer
}

func ( ptyWriter) ( []byte) (int, error) {
	 := len()
	// normalize \n to \r\n when pty is accepted.
	// this is a hardcoded shortcut since we don't support terminal modes.
	 = bytes.Replace(, []byte{'\n'}, []byte{'\r', '\n'}, -1)
	 = bytes.Replace(, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'}, -1)
	,  := .w.Write()
	if  >  {
		 = 
	}
	return , 
}

// NewPtyReadWriter return an io.ReadWriter that delegates the read to the
// given io.ReadWriter, and the writes to a ptyWriter.
func ( io.ReadWriter) io.ReadWriter {
	return readWriterDelegate{
		w: NewPtyWriter(),
		r: ,
	}
}

var _ io.ReadWriter = readWriterDelegate{}

type readWriterDelegate struct {
	w io.Writer
	r io.Reader
}

func ( readWriterDelegate) ( []byte) ( int,  error) {
	return .r.Read()
}

func ( readWriterDelegate) ( []byte) ( int,  error) {
	return .w.Write()
}

// Start starts a *exec.Cmd attached to the Session. If a PTY is allocated,
// it will use that for I/O.
// On Windows, the process execution lifecycle is not managed by Go and has to
// be managed manually. This means that c.Wait() won't work.
// See https://github.com/charmbracelet/x/blob/main/exp/term/conpty/conpty_windows.go
func ( *Pty) ( *exec.Cmd) error {
	return .start()
}