Skip to content

Commit

Permalink
Add a block mode status bar + some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Sep 2, 2024
1 parent aca88f4 commit e11b335
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
2 changes: 1 addition & 1 deletion v2/cmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (e *Editor) CommandMenu(c *vt100.Canvas, tty *vt100.TTY, status *StatusBar,
status.ClearAll(c)
e.statusMode = !e.statusMode
if e.statusMode {
status.ShowLineColWordCount(c, e, e.filename)
status.ShowFilenameLineColWordCount(c, e)
e.showColumnLimit = e.wrapWhenTyping
}
})
Expand Down
39 changes: 0 additions & 39 deletions v2/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1814,45 +1814,6 @@ func (e *Editor) ColIndex() ColIndex {
return ColIndex(x)
}

// PositionAndModeInfo returns a status message, intended for being displayed at the bottom, containing:
// * the current line number (counting from 1)
// * the current column number (counting from 1)
// * the current rune unicode value
// * the current word count
// * the currently detected file mode
// * the current indentation mode (tabs or spaces)
func (e *Editor) PositionAndModeInfo() string {
indentation := "spaces"
if !e.indentation.Spaces {
indentation = "tabs"
}
return fmt.Sprintf("line %d col %d rune %U words %d, [%s] %s", e.LineNumber(), e.ColNumber(), e.Rune(), e.WordCount(), e.mode, indentation)
}

// PositionPercentageAndModeInfo returns a status message, intended for being displayed at the bottom, containing:
// * the current line number (counting from 1)
// * the current number of lines
// * the current line percentage
// * the current column number (counting from 1)
// * the current rune unicode value
// * the current word count
// * the currently detected file mode
// * the current indentation mode (tabs or spaces)
func (e *Editor) PositionPercentageAndModeInfo() string {
indentation := "spaces"
if !e.indentation.Spaces {
indentation = "tabs"
}
lineNumber := e.LineNumber()
allLines := e.Len()
percentage := 0
if allLines > 0 {
percentage = int(100.0 * (float64(lineNumber) / float64(allLines)))
}

return fmt.Sprintf("line %d/%d (%d%%) col %d rune %U words %d, [%s] %s", lineNumber, allLines, percentage, e.ColNumber(), e.Rune(), e.WordCount(), e.mode, indentation)
}

// GoToPosition can go to the given position struct and use it as the new position
func (e *Editor) GoToPosition(c *vt100.Canvas, status *StatusBar, pos Position) {
e.pos = pos
Expand Down
13 changes: 5 additions & 8 deletions v2/keyloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -1571,12 +1571,9 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
})
}

e.blockMode = !e.blockMode // let typing affect all the lines in this block
if e.blockMode {
status.SetMessage("Block mode")
} else {
status.SetMessage("Block mode off")
}
// Toggle block editing mode
e.blockMode = !e.blockMode
e.redraw = true
status.Show(c, e)

case "c:21": // ctrl-u to undo
Expand Down Expand Up @@ -2046,8 +2043,8 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
clearKeyHistory = true
}

// Clear status, if needed
if e.statusMode && e.redrawCursor {
// Clear status line, if needed
if (e.statusMode || e.blockMode) && e.redrawCursor {
status.ClearAll(c)
}

Expand Down
8 changes: 6 additions & 2 deletions v2/redraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func (e *Editor) InitialRedraw(c *vt100.Canvas, status *StatusBar) {
if e.nanoMode {
status.Show(c, e)
} else if e.statusMode {
status.ShowLineColWordCount(c, e, e.filename)
status.ShowFilenameLineColWordCount(c, e)
} else if e.blockMode {
status.ShowBlockModeStatusLine(c, e)
} else if status.IsError() {
status.Show(c, e)
}
Expand Down Expand Up @@ -182,7 +184,9 @@ func (e *Editor) RedrawAtEndOfKeyLoop(c *vt100.Canvas, status *StatusBar) {
if e.nanoMode {
status.Show(c, e)
} else if e.statusMode {
status.ShowLineColWordCount(c, e, e.filename)
status.ShowFilenameLineColWordCount(c, e)
} else if e.blockMode {
status.ShowBlockModeStatusLine(c, e)
} else if status.IsError() {
// Show the status message, if *statusMessage is not set
if status.messageAfterRedraw == "" {
Expand Down
35 changes: 31 additions & 4 deletions v2/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,37 @@ func (sb *StatusBar) ShowNoTimeout(c *vt100.Canvas, e *Editor) {
c.Draw()
}

// ShowLineColWordCount shows a status message with the current filename, line, column and word count
func (sb *StatusBar) ShowLineColWordCount(c *vt100.Canvas, e *Editor, filename string) {
statusString := filename + ": " + e.PositionPercentageAndModeInfo()
sb.SetMessage(statusString)
// ShowFilenameLineColWordCount sets a status message at the bottom, containing:
// * the current filename
// * the current line number (counting from 1)
// * the current number of lines
// * the current line percentage
// * the current column number (counting from 1)
// * the current rune unicode value
// * the current word count
// * the currently detected file mode
// * the current indentation mode (tabs or spaces)
// func FilenamePositionPercentageAndModeInfo(e *Editor) string {
func (sb *StatusBar) ShowFilenameLineColWordCount(c *vt100.Canvas, e *Editor) {
indentation := "spaces"
if !e.indentation.Spaces {
indentation = "tabs"
}
lineNumber := e.LineNumber()
allLines := e.Len()
percentage := 0
if allLines > 0 {
percentage = int(100.0 * (float64(lineNumber) / float64(allLines)))
}
statusLine := fmt.Sprintf("%s: line %d/%d (%d%%) col %d rune %U words %d, [%s] %s", e.filename, lineNumber, allLines, percentage, e.ColNumber(), e.Rune(), e.WordCount(), e.mode, indentation)
sb.SetMessage(statusLine)
sb.ShowNoTimeout(c, e)
}

// ShowBlockModeStatusLine shows a status message for when block mode is enabled
func (sb *StatusBar) ShowBlockModeStatusLine(c *vt100.Canvas, e *Editor) {
statusLine := fmt.Sprintf("Block Editing Mode [line %d, column %d]", e.LineNumber(), e.ColNumber())
sb.SetMessage(statusLine)
sb.ShowNoTimeout(c, e)
}

Expand Down

0 comments on commit e11b335

Please sign in to comment.