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

Streamline command prompt string generation #6

Merged
merged 1 commit into from
Jan 12, 2019
Merged
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
24 changes: 0 additions & 24 deletions builtin/promptitems/commandprompt.go

This file was deleted.

26 changes: 0 additions & 26 deletions builtin/promptitems/hostname.go

This file was deleted.

26 changes: 0 additions & 26 deletions builtin/promptitems/user.go

This file was deleted.

34 changes: 0 additions & 34 deletions builtin/promptitems/workdir.go

This file was deleted.

15 changes: 6 additions & 9 deletions command/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package command

import (
"fmt"
"os"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -30,15 +30,11 @@ func GetCommand(text string) Command {

// HasArgs returns true if the command has arguments or false if it does not have arguments
func (c Command) HasArgs() bool {
if len(c.Args) == 0 {
return false
}
return true
return len(c.Args) > 0
}

// Execute executes the command
func (c Command) Execute() {

func (c Command) Execute() error {
var cmd *exec.Cmd

if c.HasArgs() {
Expand All @@ -47,7 +43,8 @@ func (c Command) Execute() {
cmd = exec.Command(c.Exec)
}

out, _ := cmd.CombinedOutput()
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

fmt.Printf("%s\n", out)
return cmd.Run()
}
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ import (
)

func main() {

shell.Run()

}
70 changes: 70 additions & 0 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package prompt

import (
"fmt"
"os"
"os/user"
"strings"
)

// Cmdprompt holds all key information for the prompt.
type Cmdprompt struct {
username string
hostname string
prompt string
}

// New returns a Cmdprompt pre-populated with various system-level information
// such as the username and hostname
// the string representation of it returns a prompt with the current
// working directory as well as the cached username and host.
func New(promptChar string) *Cmdprompt {
if len(promptChar) == 0 {
promptChar = ":"
}
return &Cmdprompt{
username: username(),
hostname: hostname(),
prompt: promptChar,
}
}

func (i *Cmdprompt) String() string {
cwd := workdir()
return fmt.Sprintf("%s@%s %s %s ", i.username, i.hostname, cwd, i.prompt)
}

func username() string {
fallbackUsername := "unknown" // this should be rare.
user, err := user.Current()
if err != nil {
return fallbackUsername
}
return user.Username
}

func hostname() string {
fallbackHostname := "localhost"
hn, err := os.Hostname()
if err != nil {
return fallbackHostname
}
return hn
}

func workdir() string {
fallbackWorkdir := "?" // this should be rare.
wd, err := os.Getwd()
if err != nil {
wd = fallbackWorkdir
}

homedir := os.Getenv("HOME")
if homedir == wd {
return "~"
} else if strings.HasPrefix(wd, homedir) {
return "~" + strings.TrimPrefix(wd, homedir)
} else {
return wd
}
}
31 changes: 0 additions & 31 deletions shell/initialize.go

This file was deleted.

26 changes: 9 additions & 17 deletions shell/interpret.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package shell

import (
"fmt"
"strings"

"github.com/cezarmathe/gosh/builtin"
"github.com/cezarmathe/gosh/command"
)

func interpret() {
shellPrompt.Print()

text, err := readInput()

if err != nil {
fmt.Println(err)
return
func interpret(cmd string) error {
cmd = strings.TrimRight(cmd, "\r\n")
// no command was actually issued.
if cmd == "" {
return nil
}

com := command.GetCommand(text)
com := command.GetCommand(cmd)

if builtin.BuiltinCommand(com) {
return
return nil
}

com.Execute()

}

func readInput() (string, error) {
return reader.ReadString('\n')
return com.Execute()
}
8 changes: 0 additions & 8 deletions shell/prompt/item.go

This file was deleted.

23 changes: 0 additions & 23 deletions shell/prompt/prompt.go

This file was deleted.

29 changes: 18 additions & 11 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ package shell

import (
"bufio"
"fmt"
"io"
"os"

"github.com/cezarmathe/gosh/shell/prompt"
)

var (
shellPrompt prompt.Prompt
reader *bufio.Reader
"github.com/cezarmathe/gosh/prompt"
)

// Run contains the entire shell lifecycle
func Run() {
reader := bufio.NewReader(os.Stdin)
shellPrompt := prompt.New(":")
for {
fmt.Print(shellPrompt)
input, err := reader.ReadString('\n')

initialize()

defer terminate()
if err != nil {
if err == io.EOF {
break
}
fmt.Fprintln(os.Stderr, err)
}

for {
interpret()
if err = interpret(input); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
11 changes: 0 additions & 11 deletions shell/terminate.go

This file was deleted.