-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hof/tui: add support for outputting playground content, update help t…
…ext and some styling (#324)
- Loading branch information
Showing
11 changed files
with
374 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package playground | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
|
||
"github.com/hofstadter-io/hof/lib/tui/tview" | ||
) | ||
func (C *Playground) setupKeybinds() { | ||
// events (hotkeys) | ||
C.SetInputCapture(func(evt *tcell.EventKey) *tcell.EventKey { | ||
switch evt.Key() { | ||
case tcell.KeyRune: | ||
if (evt.Modifiers() & tcell.ModAlt) == tcell.ModAlt { | ||
switch evt.Rune() { | ||
case 'f': | ||
flexDir := C.GetDirection() | ||
if flexDir == tview.FlexRow { | ||
C.SetDirection(tview.FlexColumn) | ||
} else { | ||
C.SetDirection(tview.FlexRow) | ||
} | ||
|
||
case 'S': | ||
C.useScope = !C.useScope | ||
C.Rebuild(false) | ||
|
||
case 'R': | ||
C.Rebuild(true) | ||
|
||
default: | ||
return evt | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return evt | ||
|
||
default: | ||
return evt | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package playground | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/parnurzeal/gorequest" | ||
) | ||
|
||
const HTTP2_GOAWAY_CHECK = "http2: server sent GOAWAY and closed the connection" | ||
|
||
func (C *Playground) PushToPlayground() (string, error) { | ||
src := C.edit.GetText() | ||
|
||
url := "https://cuelang.org/.netlify/functions/snippets" | ||
req := gorequest.New().Post(url) | ||
req.Set("Content-Type", "text/plain") | ||
req.Send(src) | ||
|
||
resp, body, errs := req.End() | ||
|
||
if len(errs) != 0 && !strings.Contains(errs[0].Error(), HTTP2_GOAWAY_CHECK) { | ||
fmt.Println("errs:", errs) | ||
fmt.Println("resp:", resp) | ||
fmt.Println("body:", body) | ||
return body, errs[0] | ||
} | ||
|
||
if len(errs) != 0 || resp.StatusCode >= 500 { | ||
return body, fmt.Errorf("Internal Error: " + body) | ||
} | ||
if resp.StatusCode >= 400 { | ||
return body, fmt.Errorf("Bad Request: " + body) | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func (C *Playground) WriteEditToFile(filename string) (error) { | ||
src := C.edit.GetText() | ||
|
||
return os.WriteFile(filename, []byte(src), 0644) | ||
} | ||
|
||
func (C *Playground) ExportFinalToFile(filename string) (error) { | ||
ext := filepath.Ext(filename) | ||
ext = strings.TrimPrefix(ext, ".") | ||
src, err := C.final.viewer.GetValueText(ext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(filename, []byte(src), 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.