github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/quickhelp.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/xyproto/files"
     9  	"github.com/xyproto/vt100"
    10  )
    11  
    12  var quickHelpToggleFilename = filepath.Join(userCacheDir, "o", "quickhelp.txt")
    13  
    14  // DisableQuickHelpScreen saves a file to the cache directory so that the quick help will be disabled the next time the editor starts
    15  func DisableQuickHelpScreen(status *StatusBar) bool {
    16  	// Remove the file, but ignore errors if it was already gone
    17  	_ = os.Remove(quickHelpToggleFilename)
    18  
    19  	// Write a new file
    20  	contents := []byte{'0', '\n'} // 1 for enabled, 0 for disabled
    21  	err := os.WriteFile(quickHelpToggleFilename, contents, 0o644)
    22  	if err != nil {
    23  		return false
    24  	}
    25  
    26  	if status != nil {
    27  		status.SetMessageAfterRedraw("Quick overview at start has been disabled.")
    28  	}
    29  
    30  	return true
    31  }
    32  
    33  // EnableQuickHelpScreen removes the quick help config file
    34  func EnableQuickHelpScreen(status *StatusBar) bool {
    35  	// Ignore any errors. If the file is already removed, that is fine too.
    36  	_ = os.Remove(quickHelpToggleFilename)
    37  	if QuickHelpScreenIsDisabled() {
    38  		return false
    39  	}
    40  	status.SetMessageAfterRedraw("Quick overview at start has been enabled.")
    41  	return true
    42  }
    43  
    44  // QuickHelpScreenIsDisabled checks if the quick help config file exists
    45  func QuickHelpScreenIsDisabled() bool {
    46  	return files.Exists(quickHelpToggleFilename)
    47  }
    48  
    49  // DrawQuickHelp draws the quick help + some help for new users
    50  func (e *Editor) DrawQuickHelp(c *vt100.Canvas, repositionCursorAfterDrawing bool) {
    51  	const (
    52  		maxLines = 8
    53  		title    = "Quick Overview"
    54  	)
    55  
    56  	var (
    57  		minWidth = 55
    58  
    59  		foregroundColor = e.Foreground
    60  		backgroundColor = e.Background
    61  		edgeColor       = e.BoxUpperEdge
    62  	)
    63  
    64  	if QuickHelpScreenIsDisabled() {
    65  		quickHelpText = strings.ReplaceAll(quickHelpText, "Disable this overview", "Enable this overview ")
    66  	} else {
    67  		quickHelpText = strings.ReplaceAll(quickHelpText, "Enable this overview ", "Disable this overview")
    68  	}
    69  
    70  	// Get the last maxLine lines, and create a string slice
    71  	lines := strings.Split(quickHelpText, "\n")
    72  	if l := len(lines); l > maxLines {
    73  		lines = lines[l-maxLines:]
    74  	}
    75  	for _, line := range lines {
    76  		if len(line) > minWidth {
    77  			minWidth = len(line) + 5
    78  		}
    79  	}
    80  
    81  	// First create a box the size of the entire canvas
    82  	canvasBox := NewCanvasBox(c)
    83  
    84  	centerBox := NewBox()
    85  
    86  	centerBox.UpperRightPlacement(canvasBox, minWidth)
    87  	centerBox.H += 2
    88  	centerBox.X -= 4
    89  
    90  	// Then create a list box
    91  	listBox := NewBox()
    92  	listBox.FillWithMargins(centerBox, 2, 2)
    93  
    94  	// Get the current theme for the stdout box
    95  	bt := e.NewBoxTheme()
    96  	bt.Foreground = &foregroundColor
    97  	bt.Background = &backgroundColor
    98  	bt.UpperEdge = &edgeColor
    99  	bt.LowerEdge = bt.UpperEdge
   100  
   101  	leftoverHeight := (canvasBox.Y + canvasBox.H) - (centerBox.Y + centerBox.H)
   102  
   103  	// This is just an attempt at drawing the text, in order to find addedLinesBecauseWordWrap
   104  	const dryRun = true
   105  	if addedLinesBecauseWordWrap := e.DrawText(bt, c, listBox, quickHelpText, dryRun); leftoverHeight > addedLinesBecauseWordWrap {
   106  		centerBox.H += addedLinesBecauseWordWrap + 2
   107  	}
   108  
   109  	e.DrawBox(bt, c, centerBox)
   110  	e.DrawTitle(bt, c, centerBox, "=[ Quick Help ]=", false)
   111  	e.DrawText(bt, c, listBox, quickHelpText, false)
   112  
   113  	// Blit
   114  	c.Draw()
   115  
   116  	// Reposition the cursor
   117  	if repositionCursorAfterDrawing {
   118  		x := e.pos.ScreenX()
   119  		y := e.pos.ScreenY()
   120  		vt100.SetXY(uint(x), uint(y))
   121  	}
   122  }