Ejemplo n.º 1
0
        static void ShowCommands()
        {
            var mode  = 2;
            var store = VesselHost.LogPath[mode];
            var limit = Settings.Default.Limit0;

            var menu = CreateListMenu();

            menu.HelpTopic = My.HelpTopic("command-history");
            menu.Title     = $"Command history ({limit})";
            menu.TypeId    = new Guid("1baa6870-4d49-40e5-8d20-19ff4b8ac5e6");

            menu.AddKey(KeyCode.R, ControlKeyStates.LeftCtrlPressed);
            menu.AddKey(KeyCode.Enter, ControlKeyStates.LeftCtrlPressed);
            if (Far.Api.Window.Kind == WindowKind.Panels)
            {
                menu.AddKey(KeyCode.Delete, ControlKeyStates.ShiftPressed);
            }

            for (; ; menu.Items.Clear())
            {
                var actor     = new Actor(mode, store);
                int lastGroup = -1;
                foreach (var it in actor.GetHistory(DateTime.Now))
                {
                    // separator
                    int nextGroup = it.Group(limit);
                    if (lastGroup != nextGroup)
                    {
                        if (lastGroup > 0)
                        {
                            menu.Add("").IsSeparator = true;
                        }
                        lastGroup = nextGroup;
                    }

                    // item
                    var item = menu.Add(it.Path);
                    item.Data = it;
                    if (it.Evidence > 0)
                    {
                        item.Checked = true;
                    }
                }

show:

                //! show and check the result or after Esc index may be > 0
                //! e.g. ShiftDel the last record + Esc == index out of range
                if (!menu.Show() || menu.Selected < 0)
                {
                    return;
                }

                // update:
                if (menu.Key.IsCtrl(KeyCode.R))
                {
                    var algo = new Actor(mode, store, true);
                    algo.Update();
                    continue;
                }

                // the command
                int    indexSelected = menu.Selected;
                string path          = menu.Items[indexSelected].Text;

                // delete:
                if (menu.Key.IsShift(KeyCode.Delete))
                {
                    if (My.AskDiscard(path))
                    {
                        Store.Remove(store, path, StringComparison.Ordinal);
                        Far.Api.PostMacro($"Keys 'AltF8'; if Menu.Select({Lua.StringLiteral(path)}) > 0 then Keys 'ShiftDel' end; if Area.Menu then Keys 'Esc' end");
                        return;                         //!
                    }
                    goto show;
                }

                // Enter | CtrlEnter:
                if (Far.Api.Window.Kind != WindowKind.Panels && !Far.Api.Window.IsModal)
                {
                    Far.Api.Window.SetCurrentAt(-1);
                }

                // put/post command
                if (Far.Api.Window.Kind == WindowKind.Panels)
                {
                    Far.Api.CommandLine.Text = path;
                    if (!menu.Key.IsCtrl())
                    {
                        Far.Api.PostMacro("Keys'Enter'");
                    }
                }
                else
                {
                    My.BadWindow();
                }

                // if it is not logged yet, log the existing Far record
                if (!actor.IsLoggedPath(path))
                {
                    var info = menu.Items[indexSelected].Data as Info;
                    Store.Append(store, info.Head, Record.OPEN, path);
                }
                // then log the current record
                Store.Append(store, DateTime.Now, Record.OPEN, path);

                UpdatePeriodically(mode);
                return;
            }
        }
Ejemplo n.º 2
0
        static void ShowHistory()
        {
            var mode  = 0;
            var store = VesselHost.LogPath[mode];
            var limit = Settings.Default.Limit0;

            var menu = CreateListMenu();

            menu.HelpTopic = My.HelpTopic("file-history");
            menu.Title     = $"File history ({limit})";
            menu.TypeId    = new Guid("23b390e8-d91d-4ff1-a9ab-de0ceffdc0ac");

            menu.AddKey(KeyCode.Enter, ControlKeyStates.LeftCtrlPressed);
            menu.AddKey(KeyCode.Enter, ControlKeyStates.ShiftPressed);
            menu.AddKey(KeyCode.F3);
            menu.AddKey(KeyCode.F3, ControlKeyStates.LeftCtrlPressed);
            menu.AddKey(KeyCode.F4);
            menu.AddKey(KeyCode.F4, ControlKeyStates.LeftCtrlPressed);
            menu.AddKey(KeyCode.R, ControlKeyStates.LeftCtrlPressed);
            var area = Far.Api.Window.Kind;

            if (area == WindowKind.Panels || area == WindowKind.Editor || area == WindowKind.Viewer)
            {
                menu.AddKey(KeyCode.Delete, ControlKeyStates.ShiftPressed);
            }

            for (; ; menu.Items.Clear())
            {
                var actor     = new Actor(mode, store);
                int lastGroup = -1;
                foreach (var it in actor.GetHistory(DateTime.Now))
                {
                    // separator
                    int nextGroup = it.Group(limit);
                    if (lastGroup != nextGroup)
                    {
                        if (lastGroup > 0)
                        {
                            menu.Add("").IsSeparator = true;
                        }

                        lastGroup = nextGroup;
                    }

                    // item
                    var item = menu.Add(it.Path);
                    item.Data = it;
                    if (it.Evidence > 0)
                    {
                        item.Checked = true;
                    }
                }

show:

                //! show and check the result or after Esc index may be > 0
                //! e.g. ShiftDel the last record + Esc == index out of range
                if (!menu.Show() || menu.Selected < 0)
                {
                    return;
                }

                // update:
                if (menu.Key.IsCtrl(KeyCode.R))
                {
                    var algo = new Actor(mode, store, true);
                    algo.Update();
                    continue;
                }

                // the file
                int    indexSelected = menu.Selected;
                string path          = menu.Items[indexSelected].Text;

                // delete:
                if (menu.Key.IsShift(KeyCode.Delete))
                {
                    if (My.AskDiscard(path))
                    {
                        Store.Remove(store, path, StringComparison.OrdinalIgnoreCase);

                        // Known far history items: Edit: PATH | Edit:-PATH | View: PATH | Ext.: ...
                        // Remove 1-3 and 4 if 4 ends with PATH (note, proper commands use "PATH", i.e. do not end with PATH)
                        Far.Api.PostMacro($"Keys 'AltF11'; while Menu.Select({Lua.StringLiteral(path)}, 2) > 0 do Keys 'ShiftDel' end; if Area.Menu then Keys 'Esc' end");
                        return;                         //!
                    }

                    goto show;
                }

                // missing?
                if (!File.Exists(path))
                {
                    Far.Api.Message("File does not exist.");
                    goto show;
                }

                // if it is not logged yet, log the existing Far record(s)
                if (!actor.IsLoggedPath(path))
                {
                    var info = menu.Items[indexSelected].Data as Info;
                    Store.Append(store, info.Head, Record.GOTO, path);
                    if (info.Head != info.Tail)
                    {
                        Store.Append(store, info.Tail, Record.GOTO, path);
                    }
                }

                // go to:
                if (menu.Key.IsCtrl(KeyCode.Enter))
                {
                    Far.Api.Panel.GoToPath(path);
                    Store.Append(store, DateTime.Now, Record.GOTO, path);
                }
                // view:
                else if (menu.Key.VirtualKeyCode == KeyCode.F3)
                {
                    IViewer viewer = Far.Api.CreateViewer();
                    viewer.FileName = path;

                    viewer.Closed += delegate
                    {
                        Store.Append(store, DateTime.Now, Record.VIEW, path);
                    };

                    if (menu.Key.IsCtrl())
                    {
                        viewer.Open(OpenMode.Modal);
                        goto show;
                    }

                    viewer.Open();
                }
                // edit:
                else
                {
                    IEditor editor = Far.Api.CreateEditor();
                    editor.FileName = path;

                    editor.Closed += delegate
                    {
                        Store.Append(store, DateTime.Now, Record.EDIT, path);
                    };

                    if (menu.Key.IsCtrl(KeyCode.F4))
                    {
                        editor.Open(OpenMode.Modal);
                        goto show;
                    }

                    editor.Open();

                    if (menu.Key.IsShift(KeyCode.Enter))
                    {
                        goto show;
                    }
                }

                UpdatePeriodically(mode);
                return;
            }
        }