File: main.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (77 lines) | stat: -rw-r--r-- 1,729 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Command e3bc ("Elvish-editor-enhanced bc") is a wrapper for the bc command
// that uses Elvish's cli library for an enhanced CLI experience.
package main

import (
	"fmt"
	"io"
	"unicode"

	"src.elv.sh/pkg/cli"
	"src.elv.sh/pkg/cli/examples/e3bc/bc"
	"src.elv.sh/pkg/cli/modes"
	"src.elv.sh/pkg/cli/term"
	"src.elv.sh/pkg/cli/tk"
	"src.elv.sh/pkg/diag"
	"src.elv.sh/pkg/ui"
)

// A highlighter for bc code. Currently this just makes all digits green.
//
// TODO: Highlight more syntax of bc.
type highlighter struct{}

func (highlighter) Get(code string) (ui.Text, []ui.Text) {
	t := ui.Text{}
	for _, r := range code {
		var style ui.Styling
		if unicode.IsDigit(r) {
			style = ui.FgGreen
		}
		t = append(t, ui.T(string(r), style)...)
	}
	return t, nil
}

func (highlighter) LateUpdates() <-chan struct{} { return nil }

func main() {
	var app cli.App
	app = cli.NewApp(cli.AppSpec{
		Prompt:      cli.NewConstPrompt(ui.T("bc> ")),
		Highlighter: highlighter{},
		CodeAreaBindings: tk.MapBindings{
			term.K('D', ui.Ctrl): func(tk.Widget) { app.CommitEOF() },
			term.K(ui.Tab): func(w tk.Widget) {
				codearea := w.(tk.CodeArea)
				if codearea.CopyState().Buffer.Content != "" {
					// Only complete with an empty buffer
					return
				}
				w, err := modes.NewCompletion(app, modes.CompletionSpec{
					Replace: diag.Ranging{From: 0, To: 0}, Items: candidates(),
				})
				if err == nil {
					app.PushAddon(w)
				}
			},
		},
		GlobalBindings: tk.MapBindings{
			term.K('[', ui.Ctrl): func(tk.Widget) { app.PopAddon() },
		},
	})

	bc := bc.Start()
	defer bc.Quit()

	for {
		code, err := app.ReadCode()
		if err != nil {
			if err != io.EOF {
				fmt.Println("error:", err)
			}
			break
		}
		bc.Exec(code)
	}
}