Skip to content

Commit

Permalink
Add clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Oct 23, 2024
1 parent dad42b2 commit 81a05a7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
45 changes: 45 additions & 0 deletions internal/server/commands/clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package commands

import (
"io"

"github.com/NHAS/reverse_ssh/internal/server/users"
"github.com/NHAS/reverse_ssh/internal/terminal"
)

type clear struct {
}

func (e *clear) ValidArgs() map[string]string {
return map[string]string{}
}

func (e *clear) Run(user *users.User, tty io.ReadWriter, line terminal.ParsedLine) error {

term, ok := tty.(*terminal.Terminal)
if !ok {
return nil
}

term.Clear()

return nil
}

func (e *clear) Expect(line terminal.ParsedLine) []string {
return nil
}

func (e *clear) Help(explain bool) string {

const description = "Clear server console"

if explain {
return description
}

return terminal.MakeHelpText(e.ValidArgs(),
"clear",
description,
)
}
2 changes: 2 additions & 0 deletions internal/server/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var allCommands = map[string]terminal.Command{
"access": &access{},
"autocomplete": &shellAutocomplete{},
"log": &logCommand{},
"clear": &clear{},
}

func CreateCommands(session string, user *users.User, log logger.Logger, datadir string) map[string]terminal.Command {
Expand All @@ -46,6 +47,7 @@ func CreateCommands(session string, user *users.User, log logger.Logger, datadir
"access": &access{},
"autocomplete": &shellAutocomplete{},
"log": Log(log),
"clear": &clear{},
}

return o
Expand Down
13 changes: 13 additions & 0 deletions internal/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,19 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
return
}

func (t *Terminal) Clear() {

t.lock.Lock()
defer t.lock.Unlock()

// Erases the screen and moves the cursor to the home position.
t.queue([]rune("\x1b[2J\x1b[H"))
t.queue(t.prompt)
t.cursorX, t.cursorY = 0, 0
t.advanceCursor(visualLength(t.prompt))
t.setLine(t.line, t.pos)
}

// addKeyToLine inserts the given key at the current position in the current
// line.
func (t *Terminal) addKeyToLine(key rune) {
Expand Down

0 comments on commit 81a05a7

Please sign in to comment.