]> git.lizzy.rs Git - micro.git/blobdiff - internal/action/actions.go
More style improvements
[micro.git] / internal / action / actions.go
index 17875c691ff1ba2fd9d1e756948ebe8df557ea12..08bd4c7294d9911a8b30c939f60935c154610644 100644 (file)
@@ -1,6 +1,9 @@
 package action
 
 import (
+       "errors"
+       "fmt"
+       "io/fs"
        "regexp"
        "runtime"
        "strings"
@@ -31,8 +34,8 @@ func (h *BufPane) ScrollDown(n int) {
        h.SetView(v)
 }
 
-// If the user has scrolled past the last line, ScrollAdjust can be used
-// to shift the view so that the last line is at the bottom
+// ScrollAdjust can be used to shift the view so that the last line is at the
+// bottom if the user has scrolled past the last line.
 func (h *BufPane) ScrollAdjust() {
        v := h.GetView()
        end := h.SLocFromLoc(h.Buf.End())
@@ -807,7 +810,7 @@ func (h *BufPane) SaveAs() bool {
 func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
        err := h.Buf.SaveAs(filename)
        if err != nil {
-               if strings.HasSuffix(err.Error(), "permission denied") {
+               if errors.Is(err, fs.ErrPermission) {
                        saveWithSudo := func() {
                                err = h.Buf.SaveAsWithSudo(filename)
                                if err != nil {
@@ -824,12 +827,15 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
                        if h.Buf.Settings["autosu"].(bool) {
                                saveWithSudo()
                        } else {
-                               InfoBar.YNPrompt("Permission denied. Do you want to save this file using sudo? (y,n)", func(yes, canceled bool) {
-                                       if yes && !canceled {
-                                               saveWithSudo()
-                                               h.completeAction(action)
-                                       }
-                               })
+                               InfoBar.YNPrompt(
+                                       fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)),
+                                       func(yes, canceled bool) {
+                                               if yes && !canceled {
+                                                       saveWithSudo()
+                                                       h.completeAction(action)
+                                               }
+                                       },
+                               )
                                return false
                        }
                } else {
@@ -1019,16 +1025,16 @@ func (h *BufPane) Copy() bool {
        return true
 }
 
-// Copy the current line to the clipboard
+// CopyLine copies the current line to the clipboard
 func (h *BufPane) CopyLine() bool {
        if h.Cursor.HasSelection() {
                return false
-       } else {
-               h.Cursor.SelectLine()
-               h.Cursor.CopySelection(clipboard.ClipboardReg)
-               h.freshClip = true
-               InfoBar.Message("Copied line")
        }
+       h.Cursor.SelectLine()
+       h.Cursor.CopySelection(clipboard.ClipboardReg)
+       h.freshClip = true
+       InfoBar.Message("Copied line")
+
        h.Cursor.Deselect(true)
        h.Relocate()
        return true
@@ -1071,9 +1077,8 @@ func (h *BufPane) Cut() bool {
 
                h.Relocate()
                return true
-       } else {
-               return h.CutLine()
        }
+       return h.CutLine()
 }
 
 // DuplicateLine duplicates the current line or selection
@@ -1632,12 +1637,12 @@ func (h *BufPane) PreviousSplit() bool {
 }
 
 var curmacro []interface{}
-var recording_macro bool
+var recordingMacro bool
 
 // ToggleMacro toggles recording of a macro
 func (h *BufPane) ToggleMacro() bool {
-       recording_macro = !recording_macro
-       if recording_macro {
+       recordingMacro = !recordingMacro
+       if recordingMacro {
                curmacro = []interface{}{}
                InfoBar.Message("Recording")
        } else {
@@ -1649,7 +1654,7 @@ func (h *BufPane) ToggleMacro() bool {
 
 // PlayMacro plays back the most recently recorded macro
 func (h *BufPane) PlayMacro() bool {
-       if recording_macro {
+       if recordingMacro {
                return false
        }
        for _, action := range curmacro {
@@ -1709,10 +1714,9 @@ func (h *BufPane) SpawnMultiCursor() bool {
 func (h *BufPane) SpawnMultiCursorUp() bool {
        if h.Cursor.Y == 0 {
                return false
-       } else {
-               h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y - 1})
-               h.Cursor.Relocate()
        }
+       h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y - 1})
+       h.Cursor.Relocate()
 
        c := buffer.NewCursor(h.Buf, buffer.Loc{h.Cursor.X, h.Cursor.Y + 1})
        h.Buf.AddCursor(c)
@@ -1727,10 +1731,9 @@ func (h *BufPane) SpawnMultiCursorUp() bool {
 func (h *BufPane) SpawnMultiCursorDown() bool {
        if h.Cursor.Y+1 == h.Buf.LinesNum() {
                return false
-       } else {
-               h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y + 1})
-               h.Cursor.Relocate()
        }
+       h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y + 1})
+       h.Cursor.Relocate()
 
        c := buffer.NewCursor(h.Buf, buffer.Loc{h.Cursor.X, h.Cursor.Y - 1})
        h.Buf.AddCursor(c)