TerminalStudio / flutter_pty

Pty for Flutter. Provides the ability to create processes with pseudo terminal file descriptors.
https://pub.dev/packages/flutter_pty
MIT License
24 stars 21 forks source link

Windows Powershell Error #10

Closed mySingleLive closed 1 year ago

mySingleLive commented 1 year ago

When I use "powershell.exe" instead of "cmd.exe", an error is reported.

Internal Windows PowerShell error. Loading managed Windows PowerShell failed with error 8009001d

Even if I start with cmd.exe and enter "powershell" in it, the same error is reported

My Code:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_pty/flutter_pty.dart';
import 'package:xterm/xterm.dart';

class TerminalTabView extends StatefulWidget {
  const TerminalTabView({super.key});

  @override
  State<TerminalTabView> createState() => _TerminalTabViewState();
}

class _TerminalTabViewState extends State<TerminalTabView> {
  final TerminalController _terminalController = TerminalController();
  late final Pty _pty;
  late final Terminal _terminal = Terminal(maxLines: 10000);

  @override
  void initState() {
    WidgetsBinding.instance.endOfFrame.then(
      (_) {
        if (mounted) {
          _startPty();
        }
      },
    );
  }

  void _startPty() {
    _pty = Pty.start(
      shell,
      columns: _terminal.viewWidth,
      rows: _terminal.viewHeight,
    );

    _pty.output
        .cast<List<int>>()
        .transform(const Utf8Decoder())
        .listen(_terminal.write);

    _pty.exitCode.then((code) {
      _terminal.write('the process exited with exit code $code');
    });

    _terminal.onOutput = (data) {
      _pty.write(const Utf8Encoder().convert(data));
    };

    _terminal.onResize = (w, h, pw, ph) {
      _pty.resize(h, w);
    };
  }

  String get shell {
    if (Platform.isMacOS || Platform.isLinux) {
      return Platform.environment['SHELL'] ?? 'bash';
    }
    if (Platform.isWindows) {
      return 'cmd.exe';
    }
    return 'sh';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color.fromRGBO(30, 30, 30, 1.0),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: TerminalView(
          _terminal,
          controller: _terminalController,
          autofocus: true,
          onSecondaryTapDown: (details, offset) async {},
        ),
      ),
    );
  }
}
umutnacak commented 1 year ago

If using the new PowerShell (https://aka.ms/pwsh) is acceptable for you this one works for windows:

_pty = Pty.start(
    'cmd.exe',
    arguments: ['/k', 'pwsh'],
    columns: _terminal.viewWidth,
    rows: _terminal.viewHeight,
);

Starting with just cmd.exe and entering pwsh works also.

mySingleLive commented 1 year ago

If using the new PowerShell (https://aka.ms/pwsh) is acceptable for you this one works for windows:

_pty = Pty.start(
    'cmd.exe',
    arguments: ['/k', 'pwsh'],
    columns: _terminal.viewWidth,
    rows: _terminal.viewHeight,
);

Starting with just cmd.exe and entering pwsh works also.

I've tried, but it's still wrong

umutnacak commented 1 year ago

Does pwsh command work on a regular terminal? Also that example project doesn't include environment variables. Try this one instead:

_pty = Pty.start(
    'cmd.exe',
    arguments: ['/k', 'pwsh'],
    environment: {...Platform.environment},
    columns: _terminal.viewWidth,
    rows: _terminal.viewHeight,
);
mySingleLive commented 1 year ago

Does pwsh command work on a regular terminal? Also that example project doesn't include environment variables. Try this one instead:

_pty = Pty.start(
    'cmd.exe',
    arguments: ['/k', 'pwsh'],
    environment: {...Platform.environment},
    columns: _terminal.viewWidth,
    rows: _terminal.viewHeight,
);

pwsh command can work on the external cmd.exe terminal.

But the Flutter Pty code still can't work (I've added the environment variables).

'pwsh' is not recognized as an internal or external command, operable program or batch file

This error appears in my Flutter Terminal Widget.

mySingleLive commented 1 year ago

Does pwsh command work on a regular terminal? Also that example project doesn't include environment variables. Try this one instead:

_pty = Pty.start(
    'cmd.exe',
    arguments: ['/k', 'pwsh'],
    environment: {...Platform.environment},
    columns: _terminal.viewWidth,
    rows: _terminal.viewHeight,
);

Oh, it worked!

But I used ['/k', 'powershell'], instead of ['/k', 'pwsh']

Thank you!

umutnacak commented 1 year ago

I'm glad that worked!