예제 #1
0
        public TerminalWidget(Func <bool> focusProvider)
        {
            var shortcutDictionary = new Dictionary <KeyEventArgs, Action>
            {
                { CreateKey(Key.C, ModifierKeys.Shift | ModifierKeys.Control), CopyMarkedField },
                { CreateKey(Key.V, ModifierKeys.Shift | ModifierKeys.Control), PasteMarkedField },
                { CreateKey(Key.Insert, ModifierKeys.Shift), PasteMarkedField },
                { CreateKey(Key.PageUp, ModifierKeys.Shift), () => terminal.PageUp() },
                { CreateKey(Key.PageDown, ModifierKeys.Shift), () => terminal.PageDown() },
                { CreateKey(Key.Plus, ModifierKeys.Shift | ModifierKeys.Control), FontSizeUp },
                { CreateKey(Key.Minus, ModifierKeys.Control), FontSizeDown },
                { CreateKey(Key.NumPadAdd, ModifierKeys.Control), FontSizeUp },
                { CreateKey(Key.NumPadSubtract, ModifierKeys.Control), FontSizeDown },
                { CreateKey(Key.K0, ModifierKeys.Control), SetDefaultFontSize },
                { CreateKey(Key.NumPad0, ModifierKeys.Control), SetDefaultFontSize }
            };

            modifyLineEndings         = ConfigurationManager.Instance.Get("termsharp", "append-CR-to-LF", false);
            terminal                  = new Terminal(focusProvider);
            terminalInputOutputSource = new TerminalIOSource(terminal);
            IO              = new IOProvider(terminalInputOutputSource);
            IO.BeforeWrite += b =>
            {
                // we do not check if previous byte was '\r', because it should not cause any problem to
                // send it twice
                if (modifyLineEndings && b == '\n')
                {
                    IO.Write((byte)'\r');
                }
            };

            terminal.InnerMargin    = new WidgetSpacing(5, 5, 5, 5);
            terminal.Cursor.Enabled = true;
            terminal.ContextMenu    = CreatePopupMenu();

            // We set the default font as a fall-back option.
            terminal.CurrentFont = Xwt.Drawing.Font.SystemMonospaceFont;
#if EMUL8_PLATFORM_LINUX
            // Here we try to load the robot font; unfortunately it doesn't work on OSX and Windows. Moreover, on some versions of
            // OSX it passes with no error (and no effect), on the others - it hangs. That's why we try to set the font and then
            // we check if we succeeded.
            var fontFile = typeof(TerminalWidget).Assembly.FromResourceToTemporaryFile("RobotoMono-Regular.ttf");
            Xwt.Drawing.Font.RegisterFontFromFile(fontFile);
#endif
            var fontFace = ConfigurationManager.Instance.Get("termsharp", "font-face", "Roboto Mono");
            defaultFontSize = ConfigurationManager.Instance.Get("termsharp", "font-size", (int)PredefinedFontSize, x => x >= MinFontSize);
            var font = Xwt.Drawing.Font.FromName(fontFace);
            if (!font.Family.Contains(fontFace))
            {
                Logger.Log(LogLevel.Warning, "The font '{0}' defined in the config file cannot be loaded.", fontFace);
                font = terminal.CurrentFont;
            }
            terminal.CurrentFont = font.WithSize(defaultFontSize);

            if (!FirstWindowAlreadyShown)
            {
                terminal.AppendRow(new LogoRow());
                FirstWindowAlreadyShown = true;
                firstWindow             = true;
            }
            else
            {
                terminal.AppendRow(new MonospaceTextRow(""));
            }

            var encoder = new TermSharp.Vt100.Encoder(x =>
            {
                terminal.ClearSelection();
                terminal.MoveScrollbarToEnd();
                terminalInputOutputSource.HandleInput(x);
            });

            terminal.KeyPressed += (s, a) =>
            {
                a.Handled = true;

                var modifiers = a.Modifiers;
                if (!Misc.IsOnOsX)
                {
                    modifiers &= ~(ModifierKeys.Command);
                }

                foreach (var entry in shortcutDictionary)
                {
                    if (modifiers == entry.Key.Modifiers)
                    {
                        if (a.Key == entry.Key.Key)
                        {
                            entry.Value();
                            return;
                        }
                    }
                }
                encoder.Feed(a.Key, modifiers);
            };
            Content = terminal;
        }