]> git.lizzy.rs Git - micro.git/commitdiff
Adds command "tabmove ±n", for better tab management (#1636)
authorJeff Warner <jwarner112@users.noreply.github.com>
Fri, 15 May 2020 01:51:49 +0000 (18:51 -0700)
committerGitHub <noreply@github.com>
Fri, 15 May 2020 01:51:49 +0000 (21:51 -0400)
* Adds command "tabmove ±n", for better tab management

* Added tabmove to help:commands

* Replace uses of util.Min, util.Max with util.Clamp

Browsing code and discovered `util.Clamp`, ideal for this section of my code

* oops, missed an arg

* Typo, again

internal/action/command.go
runtime/help/commands.md

index 1218bc42822138d5951fe903495f6b46317aabdb..8361e5a10c9252e293e26fc203b9eaea3a85f2b7 100644 (file)
@@ -56,6 +56,7 @@ func InitCommands() {
                "cd":         {(*BufPane).CdCmd, buffer.FileComplete},
                "pwd":        {(*BufPane).PwdCmd, nil},
                "open":       {(*BufPane).OpenCmd, buffer.FileComplete},
+               "tabmove":    {(*BufPane).TabMoveCmd, nil},
                "tabswitch":  {(*BufPane).TabSwitchCmd, nil},
                "term":       {(*BufPane).TermCmd, nil},
                "memusage":   {(*BufPane).MemUsageCmd, nil},
@@ -155,6 +156,56 @@ func (h *BufPane) TextFilterCmd(args []string) {
        h.Buf.Insert(h.Cursor.Loc, bout.String())
 }
 
+// TabMoveCmd moves the current tab to a given index (starts at 1). The
+// displaced tabs are moved up.
+func (h *BufPane) TabMoveCmd(args []string) {
+       if len(args) <= 0 {
+               InfoBar.Error("Not enough arguments: provide an index, starting at 1")
+               return
+       }
+
+       if len(args[0]) <= 0 {
+               InfoBar.Error("Invalid argument: empty string")
+               return
+       }
+
+       num, err := strconv.Atoi(args[0])
+       if err != nil {
+               InfoBar.Error("Invalid argument: ", err)
+               return
+       }
+
+       // Preserve sign for relative move, if one exists
+       var shiftDirection byte
+       if strings.Contains("-+", string([]byte{args[0][0]})) {
+               shiftDirection = args[0][0]
+       }
+
+       // Relative positions -> absolute positions
+       idxFrom := Tabs.Active()
+       idxTo := 0
+       offset := util.Abs(num)
+       if shiftDirection == '-' {
+               idxTo = idxFrom - offset
+       } else if shiftDirection == '+' {
+               idxTo = idxFrom + offset
+       } else {
+               idxTo = offset - 1
+       }
+
+       // Restrain position to within the valid range
+       idxTo = util.Clamp(idxTo, 0, len(Tabs.List)-1)
+
+       activeTab := Tabs.List[idxFrom]
+       Tabs.RemoveTab(activeTab.ID())
+       Tabs.List = append(Tabs.List, nil)
+       copy(Tabs.List[idxTo+1:], Tabs.List[idxTo:])
+       Tabs.List[idxTo] = activeTab
+       Tabs.UpdateNames()
+       Tabs.SetActive(idxTo)
+       // InfoBar.Message(fmt.Sprintf("Moved tab from slot %d to %d", idxFrom+1, idxTo+1))
+}
+
 // TabSwitchCmd switches to a given tab either by name or by number
 func (h *BufPane) TabSwitchCmd(args []string) {
        if len(args) > 0 {
index ff4e999433102c95a359ec8a97ab1518d565fd6b..9f43085cf3bb24a1c550a403679331d9a8592cf0 100644 (file)
@@ -62,6 +62,11 @@ quotes here but these are not necessary when entering the command in micro.
 
 * `tab 'filename'`: opens the given file in a new tab.
 
+* `tabmove '[-+]?n'`: Moves the active tab to another slot. `n` is an integer.
+   If `n` is prefixed with `-` or `+`, then it represents a relative position
+   (e.g. `tabmove +2` moves the tab to the right by `2`). If `n` has no prefix,
+   it represents an absolute position (e.g. `tabmove 2` moves the tab to slot `2`).
+
 * `tabswitch 'tab'`: This command will switch to the specified tab. The `tab`
    can either be a tab number, or a name of a tab.