File: main.go

package info (click to toggle)
kitty 0.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,540 kB
  • sloc: ansic: 84,052; python: 57,643; objc: 5,365; sh: 1,319; xml: 364; makefile: 144; javascript: 78
file content (396 lines) | stat: -rw-r--r-- 9,802 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package hints

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
	"unicode"

	"github.com/kovidgoyal/kitty/tools/cli"
	"github.com/kovidgoyal/kitty/tools/tty"
	"github.com/kovidgoyal/kitty/tools/tui"
	"github.com/kovidgoyal/kitty/tools/tui/loop"
	"github.com/kovidgoyal/kitty/tools/utils"
	"github.com/kovidgoyal/kitty/tools/utils/style"
	"github.com/kovidgoyal/kitty/tools/wcswidth"
)

var _ = fmt.Print

func convert_text(text string, cols int) string {
	lines := make([]string, 0, 64)
	empty_line := strings.Repeat("\x00", cols) + "\n"
	s1 := utils.NewLineScanner(text)
	for s1.Scan() {
		full_line := s1.Text()
		if full_line == "" {
			lines = append(lines, empty_line)
			continue
		}
		if strings.TrimRight(full_line, "\r") == "" {
			for range len(full_line) {
				lines = append(lines, empty_line)
			}
			continue
		}
		appended := false
		s2 := utils.NewSeparatorScanner(full_line, "\r")
		for s2.Scan() {
			line := s2.Text()
			if line != "" {
				line_sz := wcswidth.Stringwidth(line)
				extra := cols - line_sz
				if extra > 0 {
					line += strings.Repeat("\x00", extra)
				}
				lines = append(lines, line)
				lines = append(lines, "\r")
				appended = true
			}
		}
		if appended {
			lines[len(lines)-1] = "\n"
		}
	}
	ans := strings.Join(lines, "")
	return strings.TrimRight(ans, "\r\n")
}

func parse_input(text string) string {
	cols, err := strconv.Atoi(os.Getenv("OVERLAID_WINDOW_COLS"))
	if err == nil {
		return convert_text(text, cols)
	}
	term, err := tty.OpenControllingTerm()
	if err == nil {
		sz, err := term.GetSize()
		term.Close()
		if err == nil {
			return convert_text(text, int(sz.Col))
		}
	}
	return convert_text(text, 80)
}

type Result struct {
	Match                []string         `json:"match"`
	Programs             []string         `json:"programs"`
	Multiple_joiner      string           `json:"multiple_joiner"`
	Customize_processing string           `json:"customize_processing"`
	Type                 string           `json:"type"`
	Groupdicts           []map[string]any `json:"groupdicts"`
	Extra_cli_args       []string         `json:"extra_cli_args"`
	Linenum_action       string           `json:"linenum_action"`
	Cwd                  string           `json:"cwd"`
}

func encode_hint(num int, alphabet string) (res string) {
	runes := []rune(alphabet)
	d := len(runes)
	for res == "" || num > 0 {
		res = string(runes[num%d]) + res
		num /= d
	}
	return
}

func decode_hint(x string, alphabet string) (ans int) {
	base := len(alphabet)
	index_map := make(map[rune]int, len(alphabet))
	for i, c := range alphabet {
		index_map[c] = i
	}
	for _, char := range x {
		ans = ans*base + index_map[char]
	}
	return
}

func as_rgb(c uint32) [3]float32 {
	return [3]float32{float32((c>>16)&255) / 255.0, float32((c>>8)&255) / 255.0, float32(c&255) / 255.0}
}

func hints_text_color(confval string) (ans string) {
	ans = confval
	if ans == "auto" {
		ans = "bright-gray"
		if bc, err := tui.ReadBasicColors(); err == nil {
			bg := as_rgb(bc.Background)
			c15 := as_rgb(bc.Color15)
			c8 := as_rgb(bc.Color8)
			if utils.RGBContrast(bg[0], bg[1], bg[2], c8[0], c8[1], c8[2]) > utils.RGBContrast(bg[0], bg[1], bg[2], c15[0], c15[1], c15[2]) {
				ans = "bright-black"
			}
		}
	}
	return
}

func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
	o.HintsTextColor = hints_text_color(o.HintsTextColor)
	output := tui.KittenOutputSerializer()
	if tty.IsTerminal(os.Stdin.Fd()) {
		return 1, fmt.Errorf("You must pass the text to be hinted on STDIN")
	}
	stdin, err := io.ReadAll(os.Stdin)
	if err != nil {
		return 1, fmt.Errorf("Failed to read from STDIN with error: %w", err)
	}
	if len(args) > 0 && o.CustomizeProcessing == "" && o.Type != "linenum" {
		return 1, fmt.Errorf("Extra command line arguments present: %s", strings.Join(args, " "))
	}
	input_text := parse_input(utils.UnsafeBytesToString(stdin))
	text, all_marks, index_map, err := find_marks(input_text, o, os.Args[2:]...)
	if err != nil {
		return 1, err
	}

	result := Result{
		Programs: o.Program, Multiple_joiner: o.MultipleJoiner, Customize_processing: o.CustomizeProcessing, Type: o.Type,
		Extra_cli_args: args, Linenum_action: o.LinenumAction,
	}
	result.Cwd, _ = os.Getwd()
	alphabet := o.Alphabet
	if alphabet == "" {
		alphabet = DEFAULT_HINT_ALPHABET
	}
	ignore_mark_indices := utils.NewSet[int](8)
	window_title := o.WindowTitle
	if window_title == "" {
		switch o.Type {
		case "url":
			window_title = "Choose URL"
		default:
			window_title = "Choose text"
		}
	}
	current_text := ""
	current_input := ""
	match_suffix := ""
	switch o.AddTrailingSpace {
	case "always":
		match_suffix = " "
	case "never":
	default:
		if o.Multiple {
			match_suffix = " "
		}
	}
	chosen := []*Mark{}
	lp, err := loop.New(loop.NoAlternateScreen) // no alternate screen reduces flicker on exit
	if err != nil {
		return
	}
	fctx := style.Context{AllowEscapeCodes: true}
	faint := fctx.SprintFunc("dim")
	hint_style := fctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s bold", o.HintsForegroundColor, o.HintsBackgroundColor))
	text_style := fctx.SprintFunc(fmt.Sprintf("fg=%s bold", o.HintsTextColor))

	highlight_mark := func(m *Mark, mark_text string) string {
		hint := encode_hint(m.Index, alphabet)
		if current_input != "" && !strings.HasPrefix(hint, current_input) {
			return faint(mark_text)
		}
		hint = hint[len(current_input):]
		if hint == "" {
			hint = " "
		}
		if len(mark_text) <= len(hint) {
			mark_text = ""
		} else {
			replaced_text := mark_text[:len(hint)]
			replaced_text = strings.ReplaceAll(replaced_text, "\r", "\n")
			if strings.Contains(replaced_text, "\n") {
				buf := strings.Builder{}
				buf.Grow(2 * len(hint))
				h := hint
				parts := strings.Split(replaced_text, "\n")
				for i, x := range parts {
					if x != "" {
						buf.WriteString(h[:len(x)])
						h = h[len(x):]
					}
					if i != len(parts)-1 {
						buf.WriteString("\n")
					}
				}
				if h != "" {
					buf.WriteString(h)
				}
				hint = buf.String()
			}
			mark_text = mark_text[len(hint):]
		}
		ans := hint_style(hint) + text_style(mark_text)
		return fmt.Sprintf("\x1b]8;;mark:%d\a%s\x1b]8;;\a", m.Index, ans)
	}

	render := func() string {
		ans := text
		for i := len(all_marks) - 1; i >= 0; i-- {
			mark := &all_marks[i]
			if ignore_mark_indices.Has(mark.Index) {
				continue
			}
			mtext := highlight_mark(mark, ans[mark.Start:mark.End])
			ans = ans[:mark.Start] + mtext + ans[mark.End:]
		}
		ans = strings.ReplaceAll(ans, "\x00", "")
		return strings.TrimRightFunc(strings.NewReplacer("\r", "\r\n", "\n", "\r\n").Replace(ans), unicode.IsSpace)
	}

	draw_screen := func() {
		lp.StartAtomicUpdate()
		defer lp.EndAtomicUpdate()
		if current_text == "" {
			current_text = render()
		}
		lp.ClearScreen()
		lp.QueueWriteString(current_text)
	}
	reset := func() {
		current_input = ""
		current_text = ""
	}

	lp.OnInitialize = func() (string, error) {
		lp.SetCursorVisible(false)
		lp.SetWindowTitle(window_title)
		lp.AllowLineWrapping(false)
		draw_screen()
		lp.SendOverlayReady()
		return "", nil
	}
	lp.OnFinalize = func() string {
		lp.SetCursorVisible(true)
		return ""
	}
	lp.OnResize = func(old_size, new_size loop.ScreenSize) error {
		draw_screen()
		return nil
	}
	lp.OnRCResponse = func(data []byte) error {
		var r struct {
			Type string
			Mark int
		}
		if err := json.Unmarshal(data, &r); err != nil {
			return err
		}
		if r.Type == "mark_activated" {
			if m, ok := index_map[r.Mark]; ok {
				chosen = append(chosen, m)
				if o.Multiple {
					ignore_mark_indices.Add(m.Index)
					reset()
				} else {
					lp.Quit(0)
					return nil
				}

			}
		}
		return nil
	}

	lp.OnText = func(text string, _, _ bool) error {
		changed := false
		for _, ch := range text {
			if strings.ContainsRune(alphabet, ch) {
				current_input += string(ch)
				changed = true
			}
		}
		if changed {
			matches := []*Mark{}
			for idx, m := range index_map {
				if eh := encode_hint(idx, alphabet); strings.HasPrefix(eh, current_input) {
					matches = append(matches, m)
				}
			}
			if len(matches) == 1 {
				chosen = append(chosen, matches[0])
				if o.Multiple {
					ignore_mark_indices.Add(matches[0].Index)
					reset()
				} else {
					lp.Quit(0)
					return nil
				}
			}
			current_text = ""
			draw_screen()
		}
		return nil
	}

	lp.OnKeyEvent = func(ev *loop.KeyEvent) error {
		if ev.MatchesPressOrRepeat("backspace") {
			ev.Handled = true
			r := []rune(current_input)
			if len(r) > 0 {
				r = r[:len(r)-1]
				current_input = string(r)
				current_text = ""
			}
			draw_screen()
		} else if ev.MatchesPressOrRepeat("enter") || ev.MatchesPressOrRepeat("space") {
			ev.Handled = true
			if current_input != "" {
				idx := decode_hint(current_input, alphabet)
				if m := index_map[idx]; m != nil {
					chosen = append(chosen, m)
					ignore_mark_indices.Add(idx)
					if o.Multiple {
						reset()
						draw_screen()
					} else {
						lp.Quit(0)
					}
				} else {
					current_input = ""
					current_text = ""
					draw_screen()
				}
			}
		} else if ev.MatchesPressOrRepeat("esc") {
			if o.Multiple {
				lp.Quit(0)
			} else {
				lp.Quit(1)
			}
		}
		return nil
	}

	err = lp.Run()
	if err != nil {
		return 1, err
	}
	ds := lp.DeathSignalName()
	if ds != "" {
		fmt.Println("Killed by signal: ", ds)
		lp.KillIfSignalled()
		return 1, nil
	}
	if lp.ExitCode() != 0 {
		return lp.ExitCode(), nil
	}
	result.Match = make([]string, len(chosen))
	result.Groupdicts = make([]map[string]any, len(chosen))
	for i, m := range chosen {
		result.Match[i] = m.Text + match_suffix
		result.Groupdicts[i] = m.Groupdict
	}
	fmt.Println(output(result))
	return
}

func EntryPoint(parent *cli.Command) {
	create_cmd(parent, main)
}