Skip to content

Commit

Permalink
small fixes for better contextplay rye cont command loader url fixes,…
Browse files Browse the repository at this point in the history
… os\open
  • Loading branch information
refaktor committed Mar 6, 2024
1 parent 40e87a3 commit d7b9faf
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
9 changes: 4 additions & 5 deletions contrib/bleve/builtins_bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package bleve

import (
"encoding/json"
"strings"

"github.com/refaktor/rye/env"
"github.com/refaktor/rye/evaldo"
Expand All @@ -28,8 +27,8 @@ var Builtins_bleve = map[string]*env.Builtin{
case env.Native:
switch s := arg1.(type) {
case env.Uri:
path := strings.Split(s.Path, "://")
iindex, err := bleve.New(path[1], mpi.Value.(mapping.IndexMapping))
path := s.Path
iindex, err := bleve.New(path, mpi.Value.(mapping.IndexMapping))
if err != nil {
return evaldo.MakeError(ps, err.Error())
}
Expand All @@ -48,8 +47,8 @@ var Builtins_bleve = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s := arg0.(type) {
case env.Uri:
path := strings.Split(s.Path, "://")
iindex, err := bleve.Open(path[1])
path := s.Path
iindex, err := bleve.Open(path)
if err != nil {
return evaldo.MakeError(ps, err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (i Uri) GetPath() string {
return i.Path
}

func (i Uri) GetFullUri(e Idxs) string {
return e.GetWord(i.Scheme.Index) + "://" + i.Path
}

func (i Uri) GetProtocol() Word {
return i.Scheme
}
Expand Down
32 changes: 32 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"crypto/rand"
"fmt"
"io"
"log"
"math"
"math/big"
"os"
"os/exec"
"reflect"
"runtime"
"sort"

"github.com/refaktor/rye/env"
Expand Down Expand Up @@ -6934,6 +6936,36 @@ var builtins = map[string]*env.Builtin{
},
},

"os\\open": { // todo -- variation is not meant for grouping inside context ... just for function variations ... just temp to test
Argsn: 1,
Doc: "",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Uri:

var err error
url := s0.GetFullUri(*ps.Idx)

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
default:
return makeError(ps, "Arg 1 should be String") // TODO - make propper error

Check failure on line 6963 in evaldo/builtins.go

View workflow job for this annotation

GitHub Actions / lint

`propper` is a misspelling of `proper` (misspell)
}
return nil
},
},

"rye": {
Argsn: 0,
Doc: "",
Expand Down
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<="
LETTER <- < [a-zA-Z^(` + "`" + `] >
LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] >
URIPATH <- < [a-zA-Z0-9-?=.:@/\\!_> ()] >
URIPATH <- < [a-zA-Z0-9-?&=.,:@/\\!_> ()] >
UCLETTER <- < [A-Z] >
LCLETTERORNUM <- < [a-z0-9] >
NUMBER <- < ("-"?[0-9]+) >
Expand Down
50 changes: 45 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package main

import (
"io/ioutil"

Check failure on line 7 in main.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"regexp"
"sort"

"github.com/refaktor/rye/contrib"

Expand Down Expand Up @@ -59,28 +61,60 @@ func main() {
} else if os.Args[1] == "--hr" {
evaldo.ShowResults = false
main_rye_repl(os.Stdin, os.Stdout, true, false)
} else if os.Args[1] == "cont" {
ryeFile := findLastConsoleSave()
main_rye_file(ryeFile, false, true, true)
} else {
ryeFile := dotsToMainRye(os.Args[1])
main_rye_file(ryeFile, false, true)
main_rye_file(ryeFile, false, true, false)
}
} else if len(os.Args) >= 3 {
if os.Args[1] == "ryk" {
main_ryk()
} else if os.Args[1] == "--hr" {
ryeFile := dotsToMainRye(os.Args[2])
evaldo.ShowResults = false
main_rye_file(ryeFile, false, true)
main_rye_file(ryeFile, false, true, false)
} else if os.Args[1] == "cgi" {
main_cgi_file(os.Args[2], false)
} else if os.Args[1] == "sig" {
main_rye_file(os.Args[2], true, true)
main_rye_file(os.Args[2], true, true, false)
} else {
ryeFile := dotsToMainRye(os.Args[1])
main_rye_file(ryeFile, false, true)
main_rye_file(ryeFile, false, true, false)
}
}
}

func findLastConsoleSave() string {
// Read directory entries
entries, err := ioutil.ReadDir(".")
if err != nil {
fmt.Println("Error reading directory:", err) // TODO --- report better
return ""
}

files := make([]string, 0)

// Filter files starting with "shell_"
for _, entry := range entries {
if entry.IsDir() {
continue // Skip directories
}
if strings.HasPrefix(entry.Name(), "shell_") {
files = append(files, entry.Name())
}
}

if len(files) == 0 {
return ""
}

sort.Strings(files)

return files[len(files)-1]
}

func dotsToMainRye(ryeFile string) string {
re := regexp.MustCompile(`^\.$|/\.$`)
if re.MatchString(ryeFile) {
Expand Down Expand Up @@ -247,7 +281,7 @@ func main_ryeco() {

}

func main_rye_file(file string, sig bool, subc bool) {
func main_rye_file(file string, sig bool, subc bool, interactive bool) {
info := true
//util.PrintHeader()
//defer profile.Start(profile.CPUProfile).Stop()
Expand Down Expand Up @@ -285,9 +319,15 @@ func main_rye_file(file string, sig bool, subc bool) {

evaldo.EvalBlock(es)
evaldo.MaybeDisplayFailureOrError(es, genv)

if interactive {
evaldo.DoRyeRepl(es, evaldo.ShowResults)
}

case env.Error:
fmt.Println(val.Message)
}

Check failure on line 330 in main.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

func main_cgi_file(file string, sig bool) {
Expand Down

0 comments on commit d7b9faf

Please sign in to comment.