A simple shell with simple goals
https://git.rawtext.club/sloum/slosh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.2 KiB
107 lines
2.2 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"path/filepath" |
|
"strconv" |
|
"strings" |
|
) |
|
|
|
func Dir(in string) error { |
|
p := ExpandedAbsFilepath(in) |
|
return os.Chdir(p) |
|
} |
|
|
|
func Up(in []string) error { |
|
switch len(in) { |
|
case 0: |
|
return nil |
|
case 1: |
|
return os.Chdir("..") |
|
case 2: |
|
if in[1] == "" { |
|
return os.Chdir("..") |
|
} |
|
num, err := strconv.Atoi(in[1]) |
|
if err != nil || num < 0 || num > 999 { |
|
return fmt.Errorf("Invalid argument %q", in[1]) |
|
} |
|
return os.Chdir(strings.Repeat("../", num)) |
|
default: |
|
return fmt.Errorf("Too many arguments. Expected 1, got %d", len(in)-1) |
|
} |
|
} |
|
|
|
// FIXME alias is broken when reloading the slosh file |
|
func MakeAlias(a []string) error { |
|
if len(a) < 3 { |
|
var b strings.Builder |
|
for k, v := range alias { |
|
b.WriteString("alias\t\033[1m") |
|
b.WriteString(k) |
|
b.WriteString("\033[0m\t") |
|
b.WriteString(v.String()) |
|
b.WriteRune('\n') |
|
} |
|
fmt.Print(b.String()) |
|
return nil |
|
} |
|
cl := CommandLine{} |
|
for _, i := range a[2:] { |
|
ce := CommandElement{value: i} |
|
ce.InferKind() |
|
cl.value = append(cl.value, ce) |
|
} |
|
alias[a[1]] = cl |
|
return nil |
|
} |
|
|
|
func RemoveAlias(a string) error { |
|
delete(alias, a); |
|
return nil |
|
} |
|
|
|
func SetEnv(items []string) error { |
|
if len(items) < 3 { |
|
return fmt.Errorf("Incomplete call to set, only %d of minimum 2 arguments received", len(items)) |
|
} |
|
return os.Setenv(items[1], strings.Join(items[2:], " ")) |
|
} |
|
|
|
func UnsetEnv(item string) error { |
|
return os.Unsetenv(item) |
|
} |
|
|
|
func SetLocal(items []string) error { |
|
if len(items) < 3 { |
|
return fmt.Errorf("Incomplete call to let, only %d of minimum 2 arguments received", len(items)) |
|
} |
|
slosh_vars[items[1]] = strings.Join(items[2:], " ") |
|
return nil |
|
} |
|
|
|
func LoadSloshFiles() error { |
|
alias = map[string]CommandLine{} |
|
if IsLoginShell { |
|
sloshSysProfile := filepath.Join("/etc", "slosh") |
|
lines, err := getRcLines(sloshSysProfile) |
|
if err == nil { |
|
parseRcLines(lines) |
|
} |
|
|
|
loginSloshFile := filepath.Join(HomeDir(), ".slosh") |
|
lines, err = getRcLines(loginSloshFile) |
|
if err == nil { |
|
parseRcLines(lines) |
|
} |
|
} |
|
interactiveSloshFile := filepath.Join(HomeDir(), ".sloshrc") |
|
lines, err := getRcLines(interactiveSloshFile) |
|
if err == nil { |
|
parseRcLines(lines) |
|
} |
|
return nil |
|
} |
|
|
|
|
|
|