Skip to content

Commit

Permalink
improvements to multiline console entries, mkcc, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Nov 6, 2024
1 parent adda7b9 commit 14279ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
4 changes: 2 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (e RyeCtx) GetState() map[int]Object {

func (e RyeCtx) Print(idxs Idxs) string {
var bu strings.Builder
bu.WriteString("[Context (" + e.Kind.Print(idxs) + "): ")
bu.WriteString("[Context (" + e.Kind.Print(idxs) + ") \"" + e.Doc + "\": ")
for k, v := range e.state {
bu.WriteString(idxs.GetWord(k) + ": " + v.Inspect(idxs) + " ")
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (e *RyeCtx) Get2(word int) (Object, bool, *RyeCtx) {

func (e *RyeCtx) Set(word int, val Object) Object {
if _, exists := e.state[word]; exists {
return NewError("Can't set already set word, try using modword! FIXME !")
return *NewError("Can't set already set word, try using modword! FIXME !")
} else {
e.state[word] = val
return val
Expand Down
40 changes: 38 additions & 2 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,13 +924,15 @@ var builtins = map[string]*env.Builtin{
return *sp
case *env.Block:
return *sp
case *env.RyeCtx:
return *sp
case *env.String:
return *sp
case *env.Native:
sp.Value = env.DereferenceAny(sp.Value)
return *sp
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "thaw")
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType, env.DictType, env.ListType, env.BlockType, env.StringType, env.NativeType, env.CtxType}, "thaw")
}
},
},
Expand Down Expand Up @@ -3181,6 +3183,10 @@ var builtins = map[string]*env.Builtin{
s1.Parent = ps.Ctx // TODO ... this is temporary so ccp works, but some other method must be figured out as changing the parent is not OK
ps.Ctx = &s1
return s1
case *env.RyeCtx:
s1.Parent = ps.Ctx // TODO ... this is temporary so ccp works, but some other method must be figured out as changing the parent is not OK
ps.Ctx = s1
return s1
default:
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "cc")
}
Expand All @@ -3204,7 +3210,11 @@ var builtins = map[string]*env.Builtin{
switch word := arg0.(type) {
case env.Word:
newctx := env.NewEnv(ps.Ctx)
ps.Ctx.Set(word.Index, *newctx)
ret := ps.Ctx.Set(word.Index, newctx)
s, ok := ret.(env.Error)
if ok {
return s
}
ctx := ps.Ctx
ps.Ctx = newctx // make new context with current par
return *ctx
Expand Down Expand Up @@ -7752,6 +7762,32 @@ var builtins = map[string]*env.Builtin{
},
},

"time?": {
Argsn: 1,
Doc: "Returns Time part of a datetime.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Time:
return *env.NewString(s1.Value.Format("15:04:05"))
default:
return MakeArgError(ps, 1, []env.Type{env.TimeType}, "hour?")
}
},
},

"date?": {
Argsn: 1,
Doc: "Returns date part of datetime.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Time:
return *env.NewString(s1.Value.Format("2006-01-02"))
default:
return MakeArgError(ps, 1, []env.Type{env.TimeType}, "hour?")
}
},
},

"day?": {
Argsn: 1,
Doc: "Returns current Time.",
Expand Down
55 changes: 43 additions & 12 deletions term/microliner.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,11 @@ type MLState struct {
lastLineString bool
prevLines int
prevCursorLine int
// killRing *ring.Ring
completer WordCompleter
completer WordCompleter
lines []string // added for the multiline behaviour
currline int // same
// pending []rune
// killRing *ring.Ring
}

// NewLiner initializes a new *State, and sets the terminal into raw mode. To
Expand Down Expand Up @@ -701,22 +703,19 @@ startOfHere:
haveNext:
if next.Ctrl {
switch strings.ToLower(next.Key) {
// next line
case "x":
// TEMP copy of code below ... refactor
historyStale = true
s.lastLineString = false
// trace2("NL")
s.sendBack(fmt.Sprintf("%s⏎\n%s", color_emph, reset))
if s.inString {
s.lastLineString = true
}
s.enterLine(string(line) + " ")
// DONT SEND LINE BACK BUT STORE IT
// s.enterLine(string(line) + " ")
s.lines = append(s.lines, string(line))
pos = 0
//if xx == "next line" {
multiline = true
//} else {
// s.sendBack("") // WW?
//}
line = make([]rune, 0)
trace(line)
goto startOfHere
Expand Down Expand Up @@ -861,7 +860,24 @@ startOfHere:
}
} else {
switch next.Code {
case 13: // Enter
case 13: // Enter Newline
if s.inString {
// This is copy from ctrl+x code above ... deduplicate and systemize TODO
historyStale = true
s.lastLineString = false
s.sendBack(fmt.Sprintf("%s⏎\n%s", color_emph, reset))
if s.inString {
s.lastLineString = true
}
// DONT SEND LINE BACK BUT STORE IT
// s.enterLine(string(line) + " ")
s.lines = append(s.lines, string(line))
pos = 0
multiline = true
line = make([]rune, 0)
trace(line)
goto startOfHere
}
if tabCompletionWasActive {
// TODO --- make it into a function - deduplicate
fmt.Println("")
Expand All @@ -879,7 +895,15 @@ startOfHere:
} else {
s.sendBack("\n")
}
xx := s.enterLine(string(line))
xx := ""
if multiline {
s.lines = append(s.lines, string(line))
xx = s.enterLine(strings.Join(s.lines, " "))

} else {
xx = s.enterLine(string(line))

}
pos = 0
multiline = false
if xx == "next line" {
Expand Down Expand Up @@ -938,7 +962,14 @@ startOfHere:
case 38: // Up
if multiline {
CurUp(1)
fmt.Println("*")
// append the last line -- only when in last line but ok for now
if s.currline == 0 {
s.lines = append(s.lines, string(line))
} else {
s.lines[len(s.lines)-1-s.currline] = string(line)
}
s.currline += 1 // later increment
line = []rune(s.lines[len(s.lines)-1-s.currline])
} else {
histPrev()
}
Expand Down

0 comments on commit 14279ed

Please sign in to comment.