diff --git a/cmd/rbit/main.go b/cmd/rbit/main.go index bc79a64..f4b03ba 100644 --- a/cmd/rbit/main.go +++ b/cmd/rbit/main.go @@ -143,14 +143,15 @@ func main() { flag.Usage = func() { fmt.Println("╭────────────────────────────────────────────────────────────────────────────────────────────---") - fmt.Println("│ \033[1mrbit - rye builtin info tool") + fmt.Println("│ \033[1mrbit - rye builtin info tool - https://ryelang.org") fmt.Println("╰───────────────────────────────────────────────────────────────────────────────────────---") - fmt.Println("\n Usage: \033[1mparse\033[0m [\033[1moptions\033[0m] [\033[1mfilename\033[0m or \033[1mcommand\033[0m]") + fmt.Println("\n Usage: \033[1mparse\033[0m [\033[1moptions\033[0m] [\033[1mfilename\033[0m]") flag.PrintDefaults() - fmt.Println("\033[33m rbit \033[36m# shows helo") + fmt.Println("\033[33m rbit \033[36m# shows this help") fmt.Println("\033[33m rbit ../../evaldo/builtins.go > ../../info/base.info.rye \033[36m# generates the info file") - fmt.Println("\033[33m rbit -stats ../../evaldo/builtins.go \033[36m# gets bi coverage stats") - fmt.Println("\033[33m rbit -ls ../../evaldo/ \033[36m# lists bi files") + fmt.Println("\033[33m rbit -stats ../../evaldo/builtins.go \033[36m# gets builtins file stats") + fmt.Println("\033[33m rbit -ls ../../evaldo/ \033[36m# lists builtin files") + fmt.Println("\033[33m rbit -help \033[36m# shows this help") fmt.Println("\033[0m\n Thank you for trying out \033[1mRye\033[22m ...") fmt.Println("") } diff --git a/env/object.go b/env/object.go index 960df66..b50e2db 100644 --- a/env/object.go +++ b/env/object.go @@ -3,6 +3,7 @@ package env import ( "fmt" + "math" "strconv" "strings" "time" @@ -168,7 +169,12 @@ func (i Decimal) Equal(o Object) bool { } // fmt.Println(i.Value) // fmt.Println(o.(Decimal).Value) - return i.Value == o.(Decimal).Value + const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64 + if math.Abs(i.Value-o.(Decimal).Value) <= (epsilon) { + return true + } else { + return false + } } func (i Decimal) Dump(e Idxs) string { diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 2327151..cf27b7c 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -252,8 +252,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object { if posMode { idx-- } - if len(s1.Data) >= int(idx)+1 { - v := s1.Data[idx] + if len(s1.Data) > int(idx) && idx >= 0 { + v := s1.Data[idx-1] return env.ToRyeValue(v) } else { ps.FailureFlag = true @@ -267,8 +267,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object { if posMode { idx-- } - if len(s1.Data) >= int(idx)+1 { - v := s1.Data[idx] + if len(s1.Data) > int(idx) && idx >= 0 { + v := s1.Data[idx-1] return env.ToRyeValue(v) } else { ps.FailureFlag = true @@ -410,7 +410,7 @@ var ShowResults bool var builtins = map[string]*env.Builtin{ // Tests: // equal { to-word "test" } 'test - // error { to-word "123" } + // error { to-word 123 } "to-word": { Argsn: 1, Doc: "Tries to change a Rye value to a word with same name.", @@ -490,7 +490,7 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { to-string 'test } "test" // equal { to-string 123 } "123" - // equal { to-string 123.4 } "123.4000" + // equal { to-string 123.4 } "123.400000" // equal { to-string "test" } "test" "to-string": { // *** Argsn: 1, @@ -536,7 +536,7 @@ var builtins = map[string]*env.Builtin{ }, // Tests: - // equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'context + // equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'ctx ; TODO - rename ctx to context in Rye // ; equal { dict [ "a" 1 ] |to-context do\in { a } } '1 "to-context": { // *** Argsn: 1, @@ -642,7 +642,7 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { to-file "example.txt" } %example.txt - // error { to-file 123 } + // equal { to-file 123 } %123 "to-file": { // ** TODO-FIXME: return possible failures Argsn: 1, Doc: "Tries to change Rye value to a file.", @@ -1290,8 +1290,8 @@ var builtins = map[string]*env.Builtin{ }, }, // Tests: - // equal { 3 .odd } 0 - // equal { 2 .odd } 1 + // equal { 3 .even } 0 + // equal { 2 .even } 1 "even": { // *** Argsn: 1, Doc: "Checks if a number is even.", @@ -1345,10 +1345,10 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { 1 + 1 } 2 // equal { 3 + 4 } 7 - // equal { 5.6 + 7.8 } 13.4 + // equal { 5.6 + 7.8 } 13.400000 // equal { "A" + "b" } "Ab" // equal { "A" + 1 } "A1" - // equal { { 1 2 } { 3 4 } } { 1 2 3 4 } + // equal { { 1 2 } + { 3 4 } } { 1 2 3 4 } "_+": { // ** Argsn: 2, Doc: "Adds or joins two values together (Integers, Strings, Uri-s and Blocks)", @@ -1960,7 +1960,28 @@ var builtins = map[string]*env.Builtin{ return obj } } - + return arg0 + }, + }, + "display\\custom": { + Argsn: 2, + Doc: "Work in progress Interactively displays a value.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + // This is temporary implementation for experimenting what it would work like at all + // later it should belong to the object (and the medium of display, terminal, html ..., it's part of the frontend) + term.SaveCurPos() + switch bloc := arg0.(type) { + case env.Spreadsheet: + obj, esc := term.DisplayTable(bloc, ps.Idx) + if !esc { + return obj + } + case *env.Spreadsheet: + obj, esc := term.DisplayTable(*bloc, ps.Idx) + if !esc { + return obj + } + } return arg0 }, }, @@ -6412,7 +6433,7 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { "abcde" .difference "cde" } "ab" // equal { difference { 1 2 3 4 } { 2 4 } } { 1 3 } - // equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" } + // equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } list { "Bob" "Sal" } "difference": { Argsn: 2, Doc: "Finds the difference (values in first but not in second) of two values.", @@ -6732,7 +6753,7 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { split\many "192.0.0.1" "." } { "192" "0" "0" "1" } - // equal { split\many "abcde..e.q|bar" ".|" } { "abcd" "e" "q" "bar" } + // equal { split\many "abcd..e.q|bar" ".|" } { "abcd" "e" "q" "bar" } // Args: // * string // * separator-set @@ -6807,8 +6828,8 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { first { 1 2 3 4 } } 1 - // equal { rest "abcde" } "a" - // equal { rest list { 1 2 3 } } 1 + // equal { first "abcde" } "a" + // equal { first list { 1 2 3 } } 1 "first": { // ** Argsn: 1, Doc: "Accepts a Block, List or String and returns the first item.", @@ -7025,7 +7046,7 @@ var builtins = map[string]*env.Builtin{ // Tests: // equal { last { 1 2 } } 2 // equal { last "abcd" } "d" - // equal { list { 4 5 6 } } 6 + // equal { last list { 4 5 6 } } 6 "last": { // ** Argsn: 1, Doc: "Accepts a Block, List or String and returns the last value in it.", @@ -7152,7 +7173,7 @@ var builtins = map[string]*env.Builtin{ }, // Tests: - // equal { dict { "a" 1 "b" 2 "c" 3 } |values } { 1 2 3 } + // equal { dict { "a" 1 "b" 2 "c" 3 } |values } list { 1 2 3 } "values": { // ** Argsn: 1, Doc: "Accepts a Dict and returns a List of just values.", @@ -8150,7 +8171,8 @@ var builtins = map[string]*env.Builtin{ }, // Tests: // equal { dict { "a" 1 "b" 2 "c" 3 } |keys } { "a" "b" "c" } - // equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c } + // equal { spreadsheet { "a" "b" "c" } { 1 2 3 } |keys } list { "a" "b" "c" } + // ; TODO -- doesn't work yet, .header? also has the same problem -- equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c } "keys": { Argsn: 1, Doc: "Accepts Dict or Spreadsheet and returns a Block of keys or column names.", // TODO -- accept context also @@ -8165,11 +8187,7 @@ var builtins = map[string]*env.Builtin{ } return *env.NewBlock(*env.NewTSeries(keys)) case env.Spreadsheet: - keys := make([]env.Object, len(s1.Cols)) - for i, k := range s1.Cols { - keys[i] = *env.NewString(k) - } - return *env.NewBlock(*env.NewTSeries(keys)) + return s1.GetColumns() default: fmt.Println("Error") } diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index ba95fb2..2f1b869 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -812,44 +812,6 @@ var Builtins_math = map[string]*env.Builtin{ return *env.NewDecimal(fa * float64(math.Pi) / 180.0) }, }, - "is-near": { - Argsn: 2, - Doc: "Returns true if two decimals are close.", - Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { - fa, fb, errPos := assureFloats(arg0, arg1) - if errPos > 0 { - return MakeArgError(ps, errPos, []env.Type{env.IntegerType, env.BlockType}, "is-near") - } - const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64 - if math.Abs(fa-fb) <= (epsilon) { - return env.NewInteger(1) - } else { - return env.NewInteger(0) - } - }, - }, - "near-zero": { - Argsn: 1, - Doc: "Returns true if a decimal is close to zero.", - Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { - var fa float64 - switch a := arg0.(type) { - case env.Decimal: - fa = a.Value - case env.Integer: - fa = float64(a.Value) - default: - return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "near-zero") - } - // const epsilon = math.SmallestNonzeroFloat64 - const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64 - if math.Abs(fa) <= epsilon { - return env.NewInteger(1) - } else { - return env.NewInteger(0) - } - }, - }, "to-eyr": { Argsn: 1, Doc: "Math dialect to Eyr dialect", diff --git a/evaldo/builtins_term.go b/evaldo/builtins_term.go index b8c491f..f61bc2f 100644 --- a/evaldo/builtins_term.go +++ b/evaldo/builtins_term.go @@ -25,6 +25,16 @@ var Builtins_term = map[string]*env.Builtin{ return env.NewInteger(int64(width)) }, }, + + // font colors + "black": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBlack() + return env.NewInteger(1) + }, + }, "red": { Argsn: 0, Doc: "Take input from a user.", @@ -49,11 +59,11 @@ var Builtins_term = map[string]*env.Builtin{ return env.NewInteger(1) }, }, - "orange": { + "yellow": { Argsn: 0, Doc: "Open file.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { - term.ColorOrange() + term.ColorYellow() return env.NewInteger(1) }, }, @@ -73,11 +83,227 @@ var Builtins_term = map[string]*env.Builtin{ return env.NewInteger(1) }, }, + "white": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorWhite() + return env.NewInteger(1) + }, + }, + + // font colors + "str\\black": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorBlack()) + }, + }, + "str\\red": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorRed()) + }, + }, + "str\\blue": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorBlue()) + }, + }, + "str\\green": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorGreen()) + }, + }, + "str\\yellow": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorYellow()) + }, + }, + "str\\magenta": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorMagenta()) + }, + }, + "str\\cyan": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorCyan()) + }, + }, + "str\\white": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorWhite()) + }, + }, + + // bright font colors + "br-black": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrBlack() + return env.NewInteger(1) + }, + }, + "str\\br-black": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrColorBrBlack()) + return env.NewInteger(1) + }, + }, + "br-red": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrRed() + return env.NewInteger(1) + }, + }, + "br-blue": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrBlue() + return env.NewInteger(1) + }, + }, + "br-green": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrGreen() + return env.NewInteger(1) + }, + }, + "br-yellow": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrYellow() + return env.NewInteger(1) + }, + }, + "br-magenta": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrMagenta() + return env.NewInteger(1) + }, + }, + "br-cyan": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrBlue() + return env.NewInteger(1) + }, + }, + "br-white": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBrWhite() + return env.NewInteger(1) + }, + }, + + // background colors + "bg-black": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgBlack() + return env.NewInteger(1) + }, + }, + "bg-red": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgRed() + return env.NewInteger(1) + }, + }, + "bg-blue": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgBlue() + return env.NewInteger(1) + }, + }, + "bg-green": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgGreen() + return env.NewInteger(1) + }, + }, + "bg-yellow": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgYellow() + return env.NewInteger(1) + }, + }, + "bg-magenta": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgMagenta() + return env.NewInteger(1) + }, + }, + "bg-cyan": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgBlue() + return env.NewInteger(1) + }, + }, + "bg-white": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBgWhite() + return env.NewInteger(1) + }, + }, + + // font styles "bold": { Argsn: 0, Doc: "Open file.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { - term.ColorBold() + term.Bold() + return env.NewInteger(1) + }, + }, + "underline": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.Underline() return env.NewInteger(1) }, }, @@ -90,7 +316,15 @@ var Builtins_term = map[string]*env.Builtin{ }, }, - "reset\\all": { + "reset\\all": { // TODO -- remove + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.CloseProps() + return env.NewInteger(1) + }, + }, + "reset": { Argsn: 0, Doc: "Open file.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { @@ -98,4 +332,11 @@ var Builtins_term = map[string]*env.Builtin{ return env.NewInteger(1) }, }, + "str\\reset": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + return *env.NewString(term.StrCloseProps()) + }, + }, } diff --git a/info/base.info.rye b/info/base.info.rye index 382032f..21e8ece 100644 --- a/info/base.info.rye +++ b/info/base.info.rye @@ -7,7 +7,7 @@ section "base" "base text" { { equal { to-word "test" } 'test - error { to-word "123" } + error { to-word 123 } } group "to-integer" @@ -41,7 +41,7 @@ section "base" "base text" { { equal { to-string 'test } "test" equal { to-string 123 } "123" - equal { to-string 123.4 } "123.4000" + equal { to-string 123.4 } "123.400000" equal { to-string "test" } "test" } @@ -71,7 +71,7 @@ section "base" "base text" { } { - equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'context + equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'ctx ; TODO - rename ctx to context in Rye ; equal { dict [ "a" 1 ] |to-context do\in { a } } '1 } @@ -136,7 +136,7 @@ section "base" "base text" { { equal { to-file "example.txt" } %example.txt - error { to-file 123 } + equal { to-file 123 } %123 } group "type?" @@ -305,8 +305,8 @@ section "base" "base text" { } { - equal { 3 .odd } 0 - equal { 2 .odd } 1 + equal { 3 .even } 0 + equal { 2 .even } 1 } group "mod" @@ -328,10 +328,10 @@ section "base" "base text" { { equal { 1 + 1 } 2 equal { 3 + 4 } 7 - equal { 5.6 + 7.8 } 13.4 + equal { 5.6 + 7.8 } 13.400000 equal { "A" + "b" } "Ab" equal { "A" + 1 } "A1" - equal { { 1 2 } { 3 4 } } { 1 2 3 4 } + equal { { 1 2 } + { 3 4 } } { 1 2 3 4 } } group "_-" @@ -361,7 +361,7 @@ section "base" "base text" { { equal { "abcde" .difference "cde" } "ab" equal { difference { 1 2 3 4 } { 2 4 } } { 1 3 } - equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" } + equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } list { "Bob" "Sal" } } group "capitalize" @@ -443,7 +443,7 @@ section "base" "base text" { { equal { split\many "192.0.0.1" "." } { "192" "0" "0" "1" } - equal { split\many "abcde..e.q|bar" ".|" } { "abcd" "e" "q" "bar" } + equal { split\many "abcd..e.q|bar" ".|" } { "abcd" "e" "q" "bar" } } group "split\\every" @@ -466,8 +466,8 @@ section "base" "base text" { { equal { first { 1 2 3 4 } } 1 - equal { rest "abcde" } "a" - equal { rest list { 1 2 3 } } 1 + equal { first "abcde" } "a" + equal { first list { 1 2 3 } } 1 } group "rest" @@ -511,7 +511,7 @@ section "base" "base text" { { equal { last { 1 2 } } 2 equal { last "abcd" } "d" - equal { list { 4 5 6 } } 6 + equal { last list { 4 5 6 } } 6 } group "head" @@ -542,7 +542,7 @@ section "base" "base text" { } { - equal { dict { "a" 1 "b" 2 "c" 3 } |values } { 1 2 3 } + equal { dict { "a" 1 "b" 2 "c" 3 } |values } list { 1 2 3 } } group "range" @@ -586,7 +586,8 @@ section "base" "base text" { { equal { dict { "a" 1 "b" 2 "c" 3 } |keys } { "a" "b" "c" } - equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c } + equal { spreadsheet { "a" "b" "c" } { 1 2 3 } |keys } list { "a" "b" "c" } + ; TODO -- doesn't work yet, .header? also has the same problem -- equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c } } group "cmd" diff --git a/info/main.rye b/info/main.rye index 7c03b72..340d0df 100644 --- a/info/main.rye +++ b/info/main.rye @@ -5,6 +5,8 @@ rye .args\raw |load :args root-ctx: current-ctx +t: ?term + test-framework: context { cnt: 0 @@ -12,7 +14,7 @@ test-framework: context { section: fn { name descr code } { term/magenta term/bold print name term/reset\all print " " + descr , do code print "" } - group: fn { name descr args code } { print "" , inc! 'cnt term/green prns " " + name term/reset\all , do code } + group: fn { name descr args code } { print "" , inc! 'cnt t/yellow prns " " + name t/reset\all , do code } error-: fn { test } { ; try { do\in root-ctx test } @@ -20,13 +22,13 @@ test-framework: context { } error: fn { test } { ; try { do\in root-ctx test } - try test :got |type? |= 'error |either { prns join { "✓" mold got } } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { newline "expected error but got: " mold got } } + try test :got |type? |= 'error |either { t/br-green prns join { "✓" } } { inc! 'failed , t/red prns " ✗ Failed:" t/reset\all prns join { newline "expected error but got: " t/str\br-black inspect got t/str\reset } } } equal: fn { test res } { cc: extends root-ctx { } do\in cc test :got = res - |either { term/green prns "✓" term/reset\all } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { "expected " mold res ", got " mold got } } + |either { t/green prns "✓" t/reset\all } { inc! 'failed , t/red prns " ✗ Failed:" t/reset\all prns join { "expected " t/str\br-black inspect res t/str\reset ", got " t/str\br-black inspect got t/str\reset } } } equal\todo: fn { test res } { @@ -35,7 +37,7 @@ test-framework: context { stdout: fn { test res } { cc: extends root-ctx { } capture-stdout { do\in cc test } :got = res - |either { term/green prns "✓" term/reset\all } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { "expected " mold res ", got " mold got } } + |either { term/green prns "✓" term/reset\all } { inc! 'failed , term/br-red prns " ✗ Failed:" term/reset\all prns join { "expected " t/str\br-black inspect res t/str\reset ", got " t/str\br-black inspect got t/str\reset } } } comment: fn { str } { diff --git a/term/term.go b/term/term.go index ba8e090..536b9f8 100644 --- a/term/term.go +++ b/term/term.go @@ -30,7 +30,7 @@ DODO: ClearLine() if i == curr { ColorBrGreen() - ColorBold() + Bold() fmt.Print("\u00bb ") } else { fmt.Print(" ") @@ -120,7 +120,7 @@ DODO1: CurRight(right) if i/2 == curr { ColorMagenta() - ColorBold() + Bold() fmt.Print("\u00bb ") } else { fmt.Print(" ") @@ -268,12 +268,12 @@ DODO: ClearLine() if ii == curr { ColorBrGreen() - ColorBold() + Bold() fmt.Print("\u00bb ") } else { fmt.Print(" ") } - ColorBold() + Bold() fmt.Print(k + ": ") ResetBold() switch ob := v.(type) { @@ -380,12 +380,12 @@ DODO: ClearLine() if ii == curr { ColorBrGreen() - ColorBold() + Bold() fmt.Print("\u00bb ") } else { fmt.Print(" ") } - ColorBold() + Bold() fmt.Print(k + ": ") ResetBold() switch ob := v.(type) { @@ -510,7 +510,147 @@ DODO: } SaveCurPos() for ic, cn := range bloc.Cols { - ColorBold() + Bold() + fmt.Printf("| %-"+strconv.Itoa(widths[ic])+"s", cn) + CloseProps() + } + fmt.Println("|") + fmt.Println("+" + strings.Repeat("-", fulwidth-1) + "+") + + for range bloc.Rows { + ClearLine() + } + for i, r := range bloc.Rows { + if i == curr { + ColorBrGreen() + fmt.Print("") + } else { + fmt.Print("") + } + for ic, v := range r.Values { + if ic < len(widths) { + // fmt.Println(v) + switch ob := v.(type) { + case env.Object: + if mode == 0 { + fmt.Printf("| %-"+strconv.Itoa(widths[ic])+"s", util.TruncateString(ob.Print(*idx), widths[ic])) + //fmt.Print("| " + + "\t") + } else { + fmt.Printf("| %-"+strconv.Itoa(widths[ic])+"s", ob.Inspect(*idx)) + //fmt.Print("| " + + "\t") + } + default: + fmt.Printf("| %-"+strconv.Itoa(widths[ic])+"s", fmt.Sprint(ob)) + ///fmt.Print("| " + +"\t") + } + } + // term.CurUp(1) + } + CloseProps() + fmt.Println("|") + } + + moveUp = len(bloc.Rows) + 2 + + defer func() { + // Show cursor. + fmt.Printf("\033[?25h") + }() + + // RestoreCurPos() + + for { + ascii, keyCode, err := GetChar() + + if (ascii == 3 || ascii == 27) || err != nil { + fmt.Println() + ShowCur() + return nil, true + } + + if ascii == 13 { + fmt.Println() + return bloc.GetRowNew(curr), false // bloc.Series.Get(curr), false + } + + if ascii == 77 || ascii == 109 { + if mode == 0 { + mode = 1 + } else { + mode = 0 + } + goto DODO + } + + if keyCode == 40 { + curr++ + goto DODO + } else if keyCode == 38 { + curr-- + goto DODO + } + } +} + +// ideation: +// .display\custom fn { x } { -> 'subject .elipsis 20 .red .prn , spacer 2 , -> 'score .align-right 10 .print } +func DisplayTableCustom(bloc env.Spreadsheet, code env.Block, idx *env.Idxs) (env.Object, bool) { + HideCur() + curr := 0 + moveUp := 0 + mode := 0 // 0 - human, 1 - dev + // get the ideal widths of columns + widths := make([]int, len(bloc.Cols)) + // check all col names + for ic, col := range bloc.Cols { + widths[ic] = len(col) + 1 + } + // check all data + for _, r := range bloc.Rows { + for ic, v := range r.Values { + ww := 5 + switch val := v.(type) { + case string: + ww = len(val) + 2 + if ww > 52 { + ww = 52 + } + case int64: + ww = len(strconv.Itoa(int(val))) + 1 + case env.Integer: + ww = len(strconv.Itoa(int(val.Value))) + 1 + case float64: + ww = len(strconv.FormatFloat(val, 'f', 2, 64)) + 1 + case env.Decimal: + ww = len(strconv.FormatFloat(val.Value, 'f', 2, 64)) + 1 + case env.String: + ww = len(val.Print(*idx)) + if ww > 52 { + ww = 52 + } + //if ww > 60 { + // ww = 60 + //} + case env.Vector: + ww = len(val.Print(*idx)) + } + if len(widths) > ic && widths[ic] < ww { + widths[ic] = ww + 1 + } + } + } + fulwidth := 0 + for _, w := range widths { + fulwidth += w + 2 + } + +DODO: + if moveUp > 0 { + CurUp(moveUp) + } + SaveCurPos() + for ic, cn := range bloc.Cols { + Bold() fmt.Printf("| %-"+strconv.Itoa(widths[ic])+"s", cn) CloseProps() } @@ -615,13 +755,17 @@ func RestoreCurPos() { fmt.Print("\x1b8") } +// Standard colors +func ColorBlack() { + fmt.Printf("\x1b[30m") +} func ColorRed() { fmt.Printf("\x1b[31m") } func ColorGreen() { fmt.Printf("\x1b[32m") } -func ColorOrange() { +func ColorYellow() { fmt.Printf("\x1b[33m") } func ColorBlue() { @@ -636,18 +780,108 @@ func ColorCyan() { func ColorWhite() { fmt.Printf("\x1b[37m") } + +// Standard colors returned +func StrColorBlack() string { + return "\x1b[30m" +} +func StrColorRed() string { + return "\x1b[31m" +} +func StrColorGreen() string { + return "\x1b[32m" +} +func StrColorYellow() string { + return "\x1b[33m" +} +func StrColorBlue() string { + return "\x1b[34m" +} +func StrColorMagenta() string { + return "\x1b[35m" +} +func StrColorCyan() string { + return "\x1b[36m" +} +func StrColorWhite() string { + return "\x1b[37m" +} + +func StrColorBrBlack() string { + return "\x1b[30;1m" +} + +// Bright colors +func ColorBrBlack() { + fmt.Printf("\x1b[30;1m") +} +func ColorBrRed() { + fmt.Printf("\x1b[31;1m") +} func ColorBrGreen() { fmt.Printf("\x1b[32;1m") } -func ColorBold() { +func ColorBrYellow() { + fmt.Printf("\x1b[33;1m") +} +func ColorBrBlue() { + fmt.Printf("\x1b[34;1m") +} +func ColorBrMagenta() { + fmt.Printf("\x1b[36;1m") +} +func ColorBrCyan() { + fmt.Printf("\x1b[37;1m") +} +func ColorBrWhite() { + fmt.Printf("\x1b[37;1m") +} + +// Background +func ColorBgBlack() { + fmt.Printf("\x1b[40m") +} +func ColorBgRed() { + fmt.Printf("\x1b[41m") +} +func ColorBgGreen() { + fmt.Printf("\x1b[42m") +} +func ColorBgYellow() { + fmt.Printf("\x1b[43m") +} +func ColorBgBlue() { + fmt.Printf("\x1b[44m") +} +func ColorBgMagenta() { + fmt.Printf("\x1b[45m") +} +func ColorBgCyan() { + fmt.Printf("\x1b[46m") +} +func ColorBgWhite() { + fmt.Printf("\x1b[47m") +} + +// Font style +func Bold() { fmt.Printf("\x1b[1m") } +func Italic() { + fmt.Printf("\x1b[3m") +} +func Underline() { + fmt.Printf("\x1b[4m") +} func ResetBold() { fmt.Printf("\x1b[22m") } func CloseProps() { fmt.Printf("\x1b[0m") } +func StrCloseProps() string { + return "\x1b[0m" +} func CurUp(n int) { fmt.Printf("\x1b[%dA", n) }