private void suggest()
        {
            var searchState = _textInputState = TextInputStateCurrent;

            _language = Language;

            var suggest = _spellchecker.Suggest(_language, searchState);

            if (isSuggestUpToDate())
            {
                Suggested?.Invoke(suggest, searchState);
            }
        }
        private void suggest()
        {
            var searchState = _textInputState = TextInputStateCurrent;

            var suggest = _searcher.Suggest(searchState, Ui);

            if (isSuggestUpToDate())
            {
                var handled = Suggested?.Invoke(suggest, searchState);

                if (handled == false)
                {
                    _textInputState = null;
                }
            }
        }
Exemple #3
0
        private bool trySuggest()
        {
            var searchState = _textInputState = TextInputStateCurrent;

            var suggest = Suggest(searchState);

            if (!IsSuggestUpToDate())
            {
                return(false);
            }

            var handled = Suggested?.Invoke(suggest, searchState);

            if (handled == false)
            {
                _textInputState = null;
            }

            return(handled == true);
        }
Exemple #4
0
 public static IObservable <Suggested> SelectSuggested(this IObservable <XElement> observable)
 {
     return(observable.Select(x => Suggested.Parse(x)));
 }
        private Suggested ParseSuggest(Stream response)
        {
            var doc = XElement.Load(response);

            return(Suggested.Parse(doc));
        }
Exemple #6
0
        public String ReadLineWithSuggestion(ConsoleColor?ForegroundColor, String PromptText, Func <String, int, bool, bool, String> Suggester, bool EnableCancellation)
        {
            var ct = GetCursorAndText();

            SaveColor();
            try
            {
                SetColor(null, ForegroundColor);
                Console.Write(PromptText);
                var Confirmed = new LinkedList <KeyValuePair <Char, KeyValuePair <int, int> > >();
                var Suggested = new LinkedList <KeyValuePair <Char, KeyValuePair <int, int> > >();
                LinkedListNode <KeyValuePair <Char, KeyValuePair <int, int> > > CurrentCharNode = null;
                int ConfirmedLastTop  = Console.CursorTop;
                int ConfirmedLastLeft = Console.CursorLeft;
                int SuggestedLastTop  = Console.CursorTop;
                int SuggestedLastLeft = Console.CursorLeft;
                void ClearSuggestion()
                {
                    Suggested = new LinkedList <KeyValuePair <Char, KeyValuePair <int, int> > >();
                }
                void RefreshSuggestion()
                {
                    if (Suggester == null)
                    {
                        return;
                    }
                    var v          = new String(Confirmed.Select(p => p.Key).Concat(Suggested.Select(p => p.Key)).ToArray());
                    var vSuggested = Suggester(v, Confirmed.Count, false, false).Substring(Confirmed.Count);

                    Suggested = new LinkedList <KeyValuePair <Char, KeyValuePair <int, int> > >(vSuggested.Select(c => new KeyValuePair <Char, KeyValuePair <int, int> >(c, new KeyValuePair <int, int>(SuggestedLastTop, SuggestedLastLeft))));
                }
                void CycleSuggestion(bool CyclePrevious)
                {
                    if (Suggester == null)
                    {
                        return;
                    }
                    var v          = new String(Confirmed.Select(p => p.Key).Concat(Suggested.Select(p => p.Key)).ToArray());
                    var vSuggested = Suggester(v, Confirmed.Count, true, CyclePrevious).Substring(Confirmed.Count);

                    Suggested = new LinkedList <KeyValuePair <Char, KeyValuePair <int, int> > >(vSuggested.Select(c => new KeyValuePair <Char, KeyValuePair <int, int> >(c, new KeyValuePair <int, int>(SuggestedLastTop, SuggestedLastLeft))));
                }
                void MoveSuggestionToConfirmed()
                {
                    foreach (var n in Suggested.ToList())
                    {
                        Console.Write(n.Key);
                        Confirmed.AddLast(n);
                    }
                    Suggested.Clear();
                    ConfirmedLastTop  = SuggestedLastTop;
                    ConfirmedLastLeft = SuggestedLastLeft;
                    RefreshCharsAfterCursor();
                }
                void RefreshCharsAfterCursor()
                {
                    var Top  = Console.CursorTop;
                    var Left = Console.CursorLeft;

                    MoveCursorToPosition(SuggestedLastTop, SuggestedLastLeft);
                    BackspaceCursorToPosition(Top, Left);
                    var Next = CurrentCharNode;

                    while (Next != null)
                    {
                        Next.Value = new KeyValuePair <Char, KeyValuePair <int, int> >(Next.Value.Key, new KeyValuePair <int, int>(Console.CursorTop, Console.CursorLeft));
                        Console.Write(Next.Value.Key);
                        Next = Next.Next;
                    }
                    ConfirmedLastTop  = Console.CursorTop;
                    ConfirmedLastLeft = Console.CursorLeft;
                    Next = Suggested.First;
                    SetColor(ConsoleColor.Blue, ConsoleColor.Yellow);
                    while (Next != null)
                    {
                        Next.Value = new KeyValuePair <Char, KeyValuePair <int, int> >(Next.Value.Key, new KeyValuePair <int, int>(Console.CursorTop, Console.CursorLeft));
                        Console.Write(Next.Value.Key);
                        Next = Next.Next;
                    }
                    SetColor(null, null);
                    SuggestedLastTop  = Console.CursorTop;
                    SuggestedLastLeft = Console.CursorLeft;
                    MoveCursorToPosition(Top, Left);
                }
                while (true)
                {
                    var Top  = Console.CursorTop;
                    var Left = Console.CursorLeft;
                    var cps  = GetConsolePositionState();
                    var ki   = Console.ReadKey(true);
                    if (Console.BufferWidth != cps.BufferWidth)
                    {
                        var BackupTop      = Console.CursorTop;
                        var BackupLeft     = Console.CursorLeft;
                        var cpsBackup      = GetConsolePositionState();
                        var BackupCharNode = CurrentCharNode;
                        SetConsolePositionState(cps);
                        if ((SuggestedLastTop > Top) || ((SuggestedLastTop == Top) && (SuggestedLastLeft > Left)))
                        {
                            MoveCursorToPosition(SuggestedLastTop, SuggestedLastLeft);
                        }
                        CurrentCharNode = Confirmed.First;
                        if (CurrentCharNode != null)
                        {
                            BackspaceCursorToPosition(CurrentCharNode.Value.Value.Key, CurrentCharNode.Value.Value.Value);
                        }
                        else
                        {
                            BackspaceCursorToPosition(SuggestedLastTop, SuggestedLastLeft);
                        }
                        SetConsolePositionState(cpsBackup);
                        SuggestedLastTop  = Console.CursorTop;
                        SuggestedLastLeft = Console.CursorLeft;
                        RefreshCharsAfterCursor();
                        CurrentCharNode = BackupCharNode;
                        MoveCursorToPosition(BackupTop, BackupLeft);
                        Top  = Console.CursorTop;
                        Left = Console.CursorLeft;
                    }
                    if (EnableCancellation && (ki.Key == ConsoleKey.Escape))
                    {
                        ClearSuggestion();
                        RefreshCharsAfterCursor();
                        throw new Shell.UserCancelledException();
                    }
                    if (ki.Key == ConsoleKey.Enter)
                    {
                        Console.WriteLine();
                        break;
                    }
                    if (ki.Key == ConsoleKey.LeftArrow)
                    {
                        if (CurrentCharNode == null)
                        {
                            CurrentCharNode = Confirmed.Last;
                        }
                        else
                        {
                            if (CurrentCharNode.Previous == null)
                            {
                                continue;
                            }
                            CurrentCharNode = CurrentCharNode.Previous;
                        }
                        if (CurrentCharNode == null)
                        {
                            MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                        }
                        else
                        {
                            MoveCursorToPosition(CurrentCharNode.Value.Value);
                        }
                    }
                    else if (ki.Key == ConsoleKey.RightArrow)
                    {
                        if (CurrentCharNode == null)
                        {
                            MoveSuggestionToConfirmed();
                            MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                            continue;
                        }
                        else
                        {
                            CurrentCharNode = CurrentCharNode.Next;
                        }
                        if (CurrentCharNode == null)
                        {
                            MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                        }
                        else
                        {
                            MoveCursorToPosition(CurrentCharNode.Value.Value);
                        }
                    }
                    else if (ki.Key == ConsoleKey.Home)
                    {
                        CurrentCharNode = Confirmed.First;
                        if (CurrentCharNode == null)
                        {
                            MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                        }
                        else
                        {
                            MoveCursorToPosition(CurrentCharNode.Value.Value);
                        }
                    }
                    else if (ki.Key == ConsoleKey.End)
                    {
                        CurrentCharNode = null;
                        MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                        MoveSuggestionToConfirmed();
                        MoveCursorToPosition(ConfirmedLastTop, ConfirmedLastLeft);
                    }
                    else if (ki.Key == ConsoleKey.Backspace)
                    {
                        if (CurrentCharNode == null)
                        {
                            if (Suggested.Count > 0)
                            {
                                ClearSuggestion();
                                RefreshCharsAfterCursor();
                            }
                            else if (Confirmed.Last != null)
                            {
                                MoveCursorToPosition(Confirmed.Last.Value.Value);
                                Confirmed.RemoveLast();
                                RefreshCharsAfterCursor();
                            }
                        }
                        else
                        {
                            if (CurrentCharNode.Previous != null)
                            {
                                MoveCursorToPosition(CurrentCharNode.Previous.Value.Value);
                                Confirmed.Remove(CurrentCharNode.Previous);
                                RefreshSuggestion();
                                RefreshCharsAfterCursor();
                            }
                        }
                    }
                    else if (ki.Key == ConsoleKey.Delete)
                    {
                        if (CurrentCharNode == null)
                        {
                            ClearSuggestion();
                            RefreshCharsAfterCursor();
                        }
                        else
                        {
                            var Next = CurrentCharNode.Next;
                            Confirmed.Remove(CurrentCharNode);
                            CurrentCharNode = Next;
                            RefreshSuggestion();
                            RefreshCharsAfterCursor();
                        }
                    }
                    else if (ki.Key == ConsoleKey.Tab)
                    {
                        if (ki.Modifiers == ConsoleModifiers.Shift)
                        {
                            CycleSuggestion(true);
                            RefreshCharsAfterCursor();
                        }
                        else
                        {
                            CycleSuggestion(false);
                            RefreshCharsAfterCursor();
                        }
                    }
                    else
                    {
                        var c = ki.KeyChar;
                        if (Char.IsControl(c))
                        {
                            continue;
                        }
                        if (CurrentCharNode != null)
                        {
                            Confirmed.AddBefore(CurrentCharNode, new KeyValuePair <Char, KeyValuePair <int, int> >(ki.KeyChar, new KeyValuePair <int, int>(Top, Left)));
                        }
                        else
                        {
                            Confirmed.AddLast(new KeyValuePair <Char, KeyValuePair <int, int> >(ki.KeyChar, new KeyValuePair <int, int>(Top, Left)));
                            CurrentCharNode = null;
                        }
                        Console.Write(c);
                        RefreshSuggestion();
                        RefreshCharsAfterCursor();
                    }
                }
                return(new String(Confirmed.Select(p => p.Key).Concat(Suggested.Select(p => p.Key)).ToArray()));
            }
            finally
            {
                LoadColor();
                LoadCursorAndText(ct);
            }
        }
Exemple #7
0
 public async void FirstLoad()
 {
     await Suggested.RunLoadMoreAsync();
 }