Skip to content

Commit

Permalink
Add files/dirs built-ins, fix scrollback
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Jan 29, 2025
1 parent 2b485e9 commit 2468896
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
31 changes: 31 additions & 0 deletions mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,37 @@ MainLoop:
}

stack.Push(&MShellDateTime{Time: parsedTime, Token: t})
} else if t.Lexeme == "files" || t.Lexeme == "dirs" {
// Dump all the files in the current directory to the stack. No sub-directories.
files, err := os.ReadDir(".")
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Error reading current directory: %s\n", t.Line, t.Column, err.Error()))
}

newList := &MShellList{
Items: make([]MShellObject, 0, len(files)),
StdinBehavior: STDIN_NONE,
StandardInputContents: "",
StandardInputFile: "",
StandardOutputFile: "",
StandardErrorFile: "",
StdoutBehavior: STDOUT_NONE,
}

if t.Lexeme == "files" {
for _, file := range files {
if !file.IsDir() {
newList.Items = append(newList.Items, &MShellPath{file.Name()})
}
}
} else {
for _, file := range files {
if file.IsDir() {
newList.Items = append(newList.Items, &MShellPath{file.Name()})
}
}
}
stack.Push(newList)
} else if t.Lexeme == "~" || strings.HasPrefix(t.Lexeme, "~/") {
// Only do tilde expansion
homeDir, err := os.UserHomeDir()
Expand Down
33 changes: 23 additions & 10 deletions mshell/Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
// "bufio"
"golang.org/x/term"
// "strings"
"strings"
// "runtime/pprof"
// "runtime/trace"
// "runtime"
Expand Down Expand Up @@ -275,6 +275,7 @@ type TermState struct {
numRows int
numCols int
promptLength int
numPromptLines int
currentCommand []rune
index int // index of cursor, starts at 0
readBuffer []byte
Expand Down Expand Up @@ -408,14 +409,23 @@ func (state *TermState) InteractiveMode() {
fmt.Fprintf(os.Stdout, "\033[K")
state.currentCommand = state.currentCommand[:state.index]
} else if c == 12 { // Ctrl-L
// Clear screen
fmt.Fprintf(os.Stdout, "\033[2J\033[1;1H")
state.printPrompt()
// Print current command
fmt.Fprintf(os.Stdout, "%s", string(state.currentCommand))
// Implement using scroll \e[nS
curRow, curCol, err, _ := getCurrentPos()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting cursor position: %s\n", err)
os.Exit(1)
}

rowsToScroll := curRow - state.numPromptLines
fmt.Fprintf(f, "%d %d %d\n", curRow, state.numPromptLines, rowsToScroll)
if rowsToScroll > 0 {
fmt.Fprintf(os.Stdout, "\033[%dS", rowsToScroll)
} else if rowsToScroll < 0 { // This is for when we have a multi line prompt, and we've used the mouse scroll to go all the way to the bottom.
fmt.Fprintf(os.Stdout, "\033[%dT", -rowsToScroll)
}

// Set cursor to current index, relative to new prompt length
fmt.Fprintf(os.Stdout, "\033[%dG", state.promptLength + 1 + state.index)
// Move cursor
fmt.Fprintf(os.Stdout, "\033[%d;%dH", state.numPromptLines, curCol)
} else if c == 13 { // Enter
// Add command to history
currentCommandStr := string(state.currentCommand)
Expand Down Expand Up @@ -651,11 +661,14 @@ func (state *TermState) printPrompt() {
fmt.Fprintf(os.Stdout, "\033[35m")
// Print PWD
cwd, err := os.Getwd()
var promptText string
if err != nil {
fmt.Fprintf(os.Stdout, "??? >")
promptText = "??? >"
} else {
fmt.Fprintf(os.Stdout, "%s> \n:: ", cwd)
promptText = fmt.Sprintf("%s > \n:: ", cwd)
}
fmt.Fprintf(os.Stdout, promptText)
state.numPromptLines = strings.Count(promptText, "\n") + 1
fmt.Fprintf(os.Stdout, "\033[0m")

// fmt.Fprintf(os.Stdout, "mshell> ")
Expand Down

0 comments on commit 2468896

Please sign in to comment.