Ejemplo n.º 1
0
        public TextField(
            IResizeableRegion region,
            string name,
            TerminalColors colors,
            char background = '░')
            : base(region, name)
        {
            this.lockObject    = new object();
            this.onKeyAccepted = new Observable <TextFieldKeyAcceptedEventArgs>();
            this.onComplete    = new Observable <string>();
            this.colors        = colors;
            this.background    = background;

            this.characters = new List <CharInfo>();

            this.scroll = 0;

            this.AddDisposable(
                this.Region.OnChanged.Subscribe(
                    new Observer <RegionChangedEventArgs>(
                        x =>
            {
                lock (this.lockObject)
                {
                    this.RequestRedraw(x.BeforeChange, x.AfterChange);
                }
            })));
        }
Ejemplo n.º 2
0
        public TerminalColors(TerminalColors other)
        {
            Foreground   = other.Foreground;
            Background   = other.Background;
            Cursor       = other.Cursor;
            CursorAccent = other.CursorAccent;
            Selection    = other.Selection;

            Black   = other.Black;
            Red     = other.Red;
            Green   = other.Green;
            Yellow  = other.Yellow;
            Blue    = other.Blue;
            Magenta = other.Magenta;
            Cyan    = other.Cyan;
            White   = other.White;

            BrightBlack   = other.BrightBlack;
            BrightRed     = other.BrightRed;
            BrightGreen   = other.BrightGreen;
            BrightYellow  = other.BrightYellow;
            BrightBlue    = other.BrightBlue;
            BrightMagenta = other.BrightMagenta;
            BrightCyan    = other.BrightCyan;
            BrightWhite   = other.BrightWhite;
        }
Ejemplo n.º 3
0
        private async Task <TerminalSize> CreateXtermView(TerminalOptions options, TerminalColors theme, IEnumerable <KeyBinding> keyBindings)
        {
            var serializedOptions     = JsonConvert.SerializeObject(options);
            var serializedTheme       = JsonConvert.SerializeObject(theme);
            var serializedKeyBindings = JsonConvert.SerializeObject(keyBindings);
            var size = await ExecuteScriptAsync($"createTerminal('{serializedOptions}', '{serializedTheme}', '{serializedKeyBindings}')").ConfigureAwait(true);

            return(JsonConvert.DeserializeObject <TerminalSize>(size));
        }
 public TextFieldKeyAcceptedEventArgs(
     TerminalKeyInfo acceptedKey,
     int indexInField,
     TerminalColors colors)
 {
     this.AcceptedKey  = acceptedKey;
     this.IndexInField = indexInField;
     this.Colors       = colors;
     this.Reject       = false;
 }
Ejemplo n.º 5
0
        private Task <TerminalSize> CreateXtermViewAsync(TerminalOptions options, TerminalColors theme, IEnumerable <KeyBinding> keyBindings)
        {
            var serializedOptions     = JsonConvert.SerializeObject(options);
            var serializedTheme       = JsonConvert.SerializeObject(theme);
            var serializedKeyBindings = JsonConvert.SerializeObject(keyBindings);

            return(ExecuteScriptAsync(
                       $"createTerminal('{serializedOptions}', '{serializedTheme}', '{serializedKeyBindings}')")
                   .ContinueWith(t => JsonConvert.DeserializeObject <TerminalSize>(t.Result)));
        }
Ejemplo n.º 6
0
 public Range(
     ushort startIndexInclusive,
     ushort endIndexExclusive,
     TerminalColors attributes,
     ushort delay = 0)
 {
     this.StartIndexInclusive = startIndexInclusive;
     this.EndIndexExclusive   = endIndexExclusive;
     this.Colors = attributes;
     this.Delay  = delay;
 }
Ejemplo n.º 7
0
 private void TerminalColor_BackgroundColorChanged(object sender, EventArgs e)
 {
     if (TerminalColor.BackgroundColor == null)
     {
         this.control.OutputBackground = null;
     }
     else
     {
         this.control.OutputBackground = (Brush)this.control.FindResource(TerminalColors.FindBackgroundKey(TerminalColor.BackgroundColor));
     }
 }
Ejemplo n.º 8
0
        internal SolidColorBrush GetFontForegroundBrush(TerminalFont font)
        {
            var color = font.Foreground;

            if (font.Bold)
            {
                return(GetBrush(TerminalColors.MakeBold(color)));
            }
            else
            {
                return(GetBrush(color));
            }
        }
Ejemplo n.º 9
0
        private void TerminalColor_BackgroundColorChanged(object sender, EventArgs e)
        {
            var backgroundColor = TerminalColor.BackgroundColor;

            this.control.Dispatcher.InvokeAsync(() =>
            {
                if (backgroundColor == null)
                {
                    this.control.OutputBackground = null;
                }
                else
                {
                    this.control.OutputBackground = (Brush)this.control.FindResource(TerminalColors.FindBackgroundKey(backgroundColor));
                }
            });
        }
Ejemplo n.º 10
0
        public void Connect(IStreamNotifier notifier, ConnectionSettings settings)
        {
            this.settings = settings;
            stream        = notifier.Stream as ShellStream;
            var terminal = new XtermTerminal(notifier)
            {
                Size        = new Terminal.Point(App.DefaultTerminalCols, App.DefaultTerminalRows),
                DefaultFont = new TerminalFont()
                {
                    Foreground = TerminalColors.GetBasicColor(7)
                }
            };

            terminal.StreamException += Terminal_StreamException;
            terminal.TitleChanged    += (sender, e) =>
            {
                Title = e.Title;
            };

            Terminal = terminal;
        }
        public Task ChangeTheme(TerminalColors theme)
        {
            var serialized = JsonConvert.SerializeObject(theme);

            return(ExecuteScriptAsync($"changeTheme('{serialized}')"));
        }
Ejemplo n.º 12
0
        private static Range ParseSpan(
            string token,
            ushort startIndexInclusive,
            ushort endIndexExclusive,
            TerminalColors defaultColors,
            ushort defaultDelay)
        {
            token = token.Remove(token.Length - 1).Substring(SpanStart.Length);

            Dictionary <string, List <string> > result = new Dictionary <string, List <string> >();

            string[] pairs = token.Split(Space, StringSplitOptions.RemoveEmptyEntries);
            foreach (string pair in pairs)
            {
                string[] components = pair.Split('=');
                string   key        = components[0];
                if (!result.TryGetValue(key, out List <string> values))
                {
                    values      = new List <string>();
                    result[key] = values;
                }

                if (components.Length == 2)
                {
                    string value = components[1];
                    if (value.Length > 1)
                    {
                        if (value[0] == '"' && value[value.Length - 1] == '"')
                        {
                            value = value.Substring(1, value.Length - 2);
                        }
                    }

                    values.Add(value);
                }
            }

            TerminalColor fg    = defaultColors.Foreground;
            TerminalColor bg    = defaultColors.Background;
            ushort        dtime = defaultDelay;

            foreach (KeyValuePair <string, List <string> > kvp in result)
            {
                switch (kvp.Key)
                {
                case "fg":
                    if (!Enum.TryParse <TerminalColor>(kvp.Value[0], out fg))
                    {
                        throw new InvalidOperationException(
                                  $"Unrecognized color '{kvp.Value[0]}'");
                    }

                    break;

                case "bg":
                    if (!Enum.TryParse <TerminalColor>(kvp.Value[0], out bg))
                    {
                        throw new InvalidOperationException(
                                  $"Unrecognized color '{kvp.Value[0]}'");
                    }

                    break;

                case "dtime":
                    dtime = ushort.Parse(kvp.Value[0]);

                    break;

                default:
                    throw new InvalidOperationException(
                              $"Unrecognized key '{kvp.Key}'");
                }
            }

            return(new Range(
                       startIndexInclusive,
                       endIndexExclusive,
                       new TerminalColors(fg, bg),
                       dtime));
        }
Ejemplo n.º 13
0
        public Catena(
            string value,
            TerminalColors defaultColors,
            ushort defaultDelay = 0)
        {
            List <Range> ranges = new List <Range>();

            if (value.Length == 0)
            {
                goto end;
            }

            int startIndex = 0;

            while (true)
            {
                // 1. Find the next '<span '
                //    a. If not found, goto exit
                int startOfNextSpan = value.IndexOf("<span ", startIndex);
                if (startOfNextSpan < 0)
                {
                    goto exit;
                }

                // 2. Find the next >
                //    a. If not found, goto exit
                int endOfNextSpan = value.IndexOf('>', startOfNextSpan);
                if (endOfNextSpan < 0)
                {
                    goto exit;
                }

                int lengthOfNextSpan = endOfNextSpan - startOfNextSpan + 1;

                // 3. Find the next </span>
                //    b. If not found, goto exit
                const string closeOfSpan           = "</span>";
                int          closeOfNextSpan       = value.IndexOf(closeOfSpan, endOfNextSpan);
                int          closeOfNextSpanLength = closeOfSpan.Length;
                if (closeOfNextSpan < 0)
                {
                    goto exit;
                }

                // 4. Copy the '<span ...>'
                string copy = value.Substring(startOfNextSpan, lengthOfNextSpan);

                // 5. Remove the '</span>'
                value = value.Remove(closeOfNextSpan, closeOfNextSpanLength);

                // 6. Remove the '<span ...>'
                value = value.Remove(startOfNextSpan, lengthOfNextSpan);

                // 7. Parse the copied '<span ...>',
                //    a. Don't forget to correct for the removal of the '<span ...>', or else the indices will be wrong!
                Range range = ParseSpan(
                    copy,
                    (ushort)startOfNextSpan, // the start is unchanged after the removal
                    (ushort)(closeOfNextSpan - lengthOfNextSpan),
                    defaultColors,
                    defaultDelay);

                // 8. Add the Range to the list
                ranges.Add(range);

                // 9. Goto 1
                startIndex = range.EndIndexExclusive;
            }

exit:
            LinkedList <Range> linkedList = new LinkedList <Range>(ranges);

            if (ranges.Count == 0)
            {
                linkedList.AddFirst(
                    new Range(
                        0,
                        (ushort)value.Length,
                        defaultColors,
                        defaultDelay));
            }
            else if (linkedList.First.Value.StartIndexInclusive != 0)
            {
                linkedList.AddFirst(
                    new Range(
                        0,
                        linkedList.First.Value.StartIndexInclusive,
                        defaultColors,
                        defaultDelay));
            }

            if (ranges.Count != 0)
            {
                // We already explcitly ensured the first value reaches the next span. Now, we just need to check:
                // * Current range's start == next range's end
                // * If there is no next range, next range's end must equal value's length. Otherwise, add a new range.
                LinkedListNode <Range>?current = linkedList.First;
                while (current != null)
                {
                    LinkedListNode <Range> next = current.Next;
                    if (next == null && current.Value.EndIndexExclusive != value.Length)
                    {
                        linkedList.AddAfter(
                            current,
                            new Range(
                                current.Value.EndIndexExclusive,
                                (ushort)value.Length,
                                defaultColors,
                                defaultDelay));

                        current = null;
                    }
                    else if (next != null && current.Value.EndIndexExclusive != next.Value.StartIndexInclusive)
                    {
                        linkedList.AddAfter(
                            current,
                            new Range(
                                current.Value.EndIndexExclusive,
                                next.Value.StartIndexInclusive,
                                defaultColors,
                                defaultDelay));

                        current = current.Next;
                    }
                    else
                    {
                        current = current.Next;
                    }
                }
            }

            ranges = linkedList.ToList();

end:
            this.Value          = value;
            this.Ranges         = new Ranges(ranges);
            this.ContainsDelays = this.Ranges.Any(x => x.Delay > 0);
        }
Ejemplo n.º 14
0
 internal static ConsoleCharAttributes ToCharAttributes(this TerminalColors colors)
 {
     return((ConsoleCharAttributes)(((ushort)colors.Background << 4) | (ushort)colors.Foreground));
 }