Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set the shells starting directory #432

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ Set the shell with the `Set Shell <shell>` command
Set Shell fish
```

#### Set CWD

Sets the directory the terminal will start in. Must be quoted.

```elixir
Set CWD "/tmp"
```

#### Set Font Size

Set the font size with the `Set FontSize <number>` command.
Expand Down
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ var Settings = map[string]CommandFunc{
"TypingSpeed": ExecuteSetTypingSpeed,
"Width": ExecuteSetWidth,
"Shell": ExecuteSetShell,
"CWD": ExecuteSetCWD,
"LoopOffset": ExecuteLoopOffset,
"MarginFill": ExecuteSetMarginFill,
"Margin": ExecuteSetMargin,
Expand Down Expand Up @@ -330,6 +331,11 @@ func ExecuteSetShell(c parser.Command, v *VHS) {
}
}

// ExecuteSetCWD applies the current working directory on the vhs.
func ExecuteSetCWD(c parser.Command, v *VHS) {
v.Options.CWD = c.Args
}

const (
bitSize = 64
base = 10
Expand Down
2 changes: 1 addition & 1 deletion evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...Evaluator

v := New()
for _, cmd := range cmds {
if cmd.Type == token.SET && cmd.Options == "Shell" {
if cmd.Type == token.SET && (cmd.Options == "Shell" || cmd.Options == "CWD") {
Execute(cmd, &v)
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/fixtures/all.tape
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Output examples/fixtures/all.webm

# Settings:
Set Shell "fish"
Set CWD ".."
Set FontSize 22
Set FontFamily "DejaVu Sans Mono"
Set Height 600
Expand Down
23 changes: 23 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ func (p *Parser) parseSet() Command {
)
}

case token.CWD:
cmd.Args = p.peek.Literal
p.nextToken()

cwd := p.cur.Literal

if !isDirectory(cwd) {
p.errors = append(
p.errors,
NewError(p.cur, cwd+" is not a valid directory."),
)
}
default:
cmd.Args = p.peek.Literal
p.nextToken()
Expand All @@ -461,6 +473,17 @@ func (p *Parser) parseSet() Command {
return cmd
}

func isDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if os.IsNotExist(err){
return false
}else if fileInfo.IsDir() {
return true
} else {
return false
}
}

// parseSleep parses a sleep command.
// A sleep command takes a time for how long to sleep.
//
Expand Down
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestParseTapeFile(t *testing.T) {
{Type: token.OUTPUT, Options: ".mp4", Args: "examples/fixtures/all.mp4"},
{Type: token.OUTPUT, Options: ".webm", Args: "examples/fixtures/all.webm"},
{Type: token.SET, Options: "Shell", Args: "fish"},
{Type: token.SET, Options: "CWD", Args: ".."},
{Type: token.SET, Options: "FontSize", Args: "22"},
{Type: token.SET, Options: "FontFamily", Args: "DejaVu Sans Mono"},
{Type: token.SET, Options: "Height", Args: "600"},
Expand Down
4 changes: 3 additions & 1 deletion token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
COPY = "COPY"
PASTE = "PASTE"
SHELL = "SHELL"
CWD = "CWD"
FONT_FAMILY = "FONT_FAMILY" //nolint:revive
FONT_SIZE = "FONT_SIZE" //nolint:revive
FRAMERATE = "FRAMERATE"
Expand Down Expand Up @@ -124,6 +125,7 @@ var Keywords = map[string]Type{
"Show": SHOW,
"Output": OUTPUT,
"Shell": SHELL,
"CWD": CWD,
"FontFamily": FONT_FAMILY,
"MarginFill": MARGIN_FILL,
"Margin": MARGIN,
Expand Down Expand Up @@ -153,7 +155,7 @@ var Keywords = map[string]Type{
// IsSetting returns whether a token is a setting.
func IsSetting(t Type) bool {
switch t {
case SHELL, FONT_FAMILY, FONT_SIZE, LETTER_SPACING, LINE_HEIGHT,
case SHELL, CWD, FONT_FAMILY, FONT_SIZE, LETTER_SPACING, LINE_HEIGHT,
FRAMERATE, TYPING_SPEED, THEME, PLAYBACK_SPEED, HEIGHT, WIDTH,
PADDING, LOOP_OFFSET, MARGIN_FILL, MARGIN, WINDOW_BAR,
WINDOW_BAR_SIZE, BORDER_RADIUS, CURSOR_BLINK:
Expand Down
4 changes: 3 additions & 1 deletion tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func randomPort() int {
}

// buildTtyCmd builds the ttyd exec.Command on the given port.
func buildTtyCmd(port int, shell Shell) *exec.Cmd {
func buildTtyCmd(port int, shell Shell, cwd string) *exec.Cmd {
args := []string{
fmt.Sprintf("--port=%d", port),
"--interface", "127.0.0.1",
Expand All @@ -34,6 +34,8 @@ func buildTtyCmd(port int, shell Shell) *exec.Cmd {
"-t", "customGlyphs=true",
"--once", // will allow one connection and exit
"--writable",
"--cwd",
cwd,
}

args = append(args, shell.Command...)
Expand Down
5 changes: 4 additions & 1 deletion vhs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type VHS struct {
// Options is the set of options for the setup.
type Options struct {
Shell Shell
CWD string
FontFamily string
FontSize int
LetterSpacing float64
Expand All @@ -52,6 +53,7 @@ type Options struct {
}

const (
defaultCWD = "."
defaultFontSize = 22
defaultTypingSpeed = 50 * time.Millisecond
defaultLineHeight = 1.0
Expand Down Expand Up @@ -89,6 +91,7 @@ func DefaultVHSOptions() Options {
screenshot := NewScreenshotOptions(video.Input, style)

return Options{
CWD: defaultCWD,
FontFamily: defaultFontFamily,
FontSize: defaultFontSize,
LetterSpacing: defaultLetterSpacing,
Expand Down Expand Up @@ -123,7 +126,7 @@ func (vhs *VHS) Start() error {
}

port := randomPort()
vhs.tty = buildTtyCmd(port, vhs.Options.Shell)
vhs.tty = buildTtyCmd(port, vhs.Options.Shell, vhs.Options.CWD)
if err := vhs.tty.Start(); err != nil {
return fmt.Errorf("could not start tty: %w", err)
}
Expand Down