| 1 |
package tui |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
"unicode" |
| 8 |
|
| 9 |
"github.com/junegunn/fzf/src/util" |
| 10 |
"github.com/rivo/uniseg" |
| 11 |
) |
| 12 |
|
| 13 |
type Attr int32 |
| 14 |
|
| 15 |
const ( |
| 16 |
AttrUndefined = Attr(0) |
| 17 |
AttrRegular = Attr(1 << 8) |
| 18 |
AttrClear = Attr(1 << 9) |
| 19 |
BoldForce = Attr(1 << 10) |
| 20 |
FullBg = Attr(1 << 11) |
| 21 |
Strip = Attr(1 << 12) |
| 22 |
|
| 23 |
// Underline style stored in bits 13-15 (3 bits, values 0-4) |
| 24 |
// Only meaningful when the Underline attribute bit is also set. |
| 25 |
// 0 = solid (default) |
| 26 |
UnderlineStyleShift = 13 |
| 27 |
UnderlineStyleMask = Attr(0b111 << UnderlineStyleShift) |
| 28 |
UlStyleDouble = Attr(0b001 << UnderlineStyleShift) |
| 29 |
UlStyleCurly = Attr(0b010 << UnderlineStyleShift) |
| 30 |
UlStyleDotted = Attr(0b011 << UnderlineStyleShift) |
| 31 |
UlStyleDashed = Attr(0b100 << UnderlineStyleShift) |
| 32 |
) |
| 33 |
|
| 34 |
func (a Attr) UnderlineStyle() Attr { |
| 35 |
return a & UnderlineStyleMask |
| 36 |
} |
| 37 |
|
| 38 |
func (a Attr) Merge(b Attr) Attr { |
| 39 |
if b&AttrRegular > 0 { |
| 40 |
// Only keep bold attribute set by the system |
| 41 |
return (b &^ AttrRegular) | (a & BoldForce) |
| 42 |
} |
| 43 |
|
| 44 |
merged := (a &^ AttrRegular) | b |
| 45 |
// When b sets Underline, use b's underline style instead of OR'ing |
| 46 |
if b&Underline > 0 { |
| 47 |
merged = (merged &^ UnderlineStyleMask) | (b & UnderlineStyleMask) |
| 48 |
} |
| 49 |
return merged |
| 50 |
} |
| 51 |
|
| 52 |
// Types of user action |
| 53 |
// |
| 54 |
//go:generate stringer -type=EventType |
| 55 |
type EventType int |
| 56 |
|
| 57 |
const ( |
| 58 |
Rune EventType = iota |
| 59 |
|
| 60 |
CtrlA |
| 61 |
CtrlB |
| 62 |
CtrlC |
| 63 |
CtrlD |
| 64 |
CtrlE |
| 65 |
CtrlF |
| 66 |
CtrlG |
| 67 |
CtrlH |
| 68 |
Tab |
| 69 |
CtrlJ |
| 70 |
CtrlK |
| 71 |
CtrlL |
| 72 |
Enter |
| 73 |
CtrlN |
| 74 |
CtrlO |
| 75 |
CtrlP |
| 76 |
CtrlQ |
| 77 |
CtrlR |
| 78 |
CtrlS |
| 79 |
CtrlT |
| 80 |
CtrlU |
| 81 |
CtrlV |
| 82 |
CtrlW |
| 83 |
CtrlX |
| 84 |
CtrlY |
| 85 |
CtrlZ |
| 86 |
Esc |
| 87 |
CtrlSpace |
| 88 |
|
| 89 |
// https://apple.stackexchange.com/questions/24261/how-do-i-send-c-that-is-control-slash-to-the-terminal |
| 90 |
CtrlBackSlash |
| 91 |
CtrlRightBracket |
| 92 |
CtrlCaret |
| 93 |
CtrlSlash |
| 94 |
|
| 95 |
ShiftTab |
| 96 |
Backspace |
| 97 |
|
| 98 |
Delete |
| 99 |
PageUp |
| 100 |
PageDown |
| 101 |
|
| 102 |
Up |
| 103 |
Down |
| 104 |
Left |
| 105 |
Right |
| 106 |
Home |
| 107 |
End |
| 108 |
Insert |
| 109 |
|
| 110 |
ShiftUp |
| 111 |
ShiftDown |
| 112 |
ShiftLeft |
| 113 |
ShiftRight |
| 114 |
ShiftDelete |
| 115 |
ShiftHome |
| 116 |
ShiftEnd |
| 117 |
ShiftPageUp |
| 118 |
ShiftPageDown |
| 119 |
|
| 120 |
F1 |
| 121 |
F2 |
| 122 |
F3 |
| 123 |
F4 |
| 124 |
F5 |
| 125 |
F6 |
| 126 |
F7 |
| 127 |
F8 |
| 128 |
F9 |
| 129 |
F10 |
| 130 |
F11 |
| 131 |
F12 |
| 132 |
|
| 133 |
AltBackspace |
| 134 |
|
| 135 |
AltUp |
| 136 |
AltDown |
| 137 |
AltLeft |
| 138 |
AltRight |
| 139 |
AltDelete |
| 140 |
AltHome |
| 141 |
AltEnd |
| 142 |
AltPageUp |
| 143 |
AltPageDown |
| 144 |
|
| 145 |
AltShiftUp |
| 146 |
AltShiftDown |
| 147 |
AltShiftLeft |
| 148 |
AltShiftRight |
| 149 |
AltShiftDelete |
| 150 |
AltShiftHome |
| 151 |
AltShiftEnd |
| 152 |
AltShiftPageUp |
| 153 |
AltShiftPageDown |
| 154 |
|
| 155 |
CtrlUp |
| 156 |
CtrlDown |
| 157 |
CtrlLeft |
| 158 |
CtrlRight |
| 159 |
CtrlHome |
| 160 |
CtrlEnd |
| 161 |
CtrlBackspace |
| 162 |
CtrlDelete |
| 163 |
CtrlPageUp |
| 164 |
CtrlPageDown |
| 165 |
|
| 166 |
Alt |
| 167 |
CtrlAlt |
| 168 |
|
| 169 |
CtrlAltUp |
| 170 |
CtrlAltDown |
| 171 |
CtrlAltLeft |
| 172 |
CtrlAltRight |
| 173 |
CtrlAltHome |
| 174 |
CtrlAltEnd |
| 175 |
CtrlAltBackspace |
| 176 |
CtrlAltDelete |
| 177 |
CtrlAltPageUp |
| 178 |
CtrlAltPageDown |
| 179 |
|
| 180 |
CtrlShiftUp |
| 181 |
CtrlShiftDown |
| 182 |
CtrlShiftLeft |
| 183 |
CtrlShiftRight |
| 184 |
CtrlShiftHome |
| 185 |
CtrlShiftEnd |
| 186 |
CtrlShiftDelete |
| 187 |
CtrlShiftPageUp |
| 188 |
CtrlShiftPageDown |
| 189 |
|
| 190 |
CtrlAltShiftUp |
| 191 |
CtrlAltShiftDown |
| 192 |
CtrlAltShiftLeft |
| 193 |
CtrlAltShiftRight |
| 194 |
CtrlAltShiftHome |
| 195 |
CtrlAltShiftEnd |
| 196 |
CtrlAltShiftDelete |
| 197 |
CtrlAltShiftPageUp |
| 198 |
CtrlAltShiftPageDown |
| 199 |
|
| 200 |
Mouse |
| 201 |
DoubleClick |
| 202 |
LeftClick |
| 203 |
RightClick |
| 204 |
SLeftClick |
| 205 |
SRightClick |
| 206 |
ScrollUp |
| 207 |
ScrollDown |
| 208 |
SScrollUp |
| 209 |
SScrollDown |
| 210 |
PreviewScrollUp |
| 211 |
PreviewScrollDown |
| 212 |
|
| 213 |
// Synthetic / non-user events. Everything from Invalid onward is |
| 214 |
// either internally generated or a state-change notification, not |
| 215 |
// direct user input. Use `>= Invalid` to gate activity tracking. |
| 216 |
// BracketedPasteBegin/End sit here too: they enclose user input |
| 217 |
// (which arrives as Rune events) and should not appear in FZF_KEY. |
| 218 |
Invalid |
| 219 |
Fatal |
| 220 |
BracketedPasteBegin |
| 221 |
BracketedPasteEnd |
| 222 |
Resize |
| 223 |
Change |
| 224 |
BackwardEOF |
| 225 |
Start |
| 226 |
Load |
| 227 |
Focus |
| 228 |
One |
| 229 |
Zero |
| 230 |
Result |
| 231 |
Jump |
| 232 |
JumpCancel |
| 233 |
ClickHeader |
| 234 |
ClickFooter |
| 235 |
Multi |
| 236 |
Every |
| 237 |
ResultFinal |
| 238 |
) |
| 239 |
|
| 240 |
func (t EventType) AsEvent() Event { |
| 241 |
return Event{t, 0, nil} |
| 242 |
} |
| 243 |
|
| 244 |
func (t EventType) Int() int { |
| 245 |
return int(t) |
| 246 |
} |
| 247 |
|
| 248 |
func (t EventType) Byte() byte { |
| 249 |
return byte(t) |
| 250 |
} |
| 251 |
|
| 252 |
func (e Event) Comparable() Event { |
| 253 |
// Ignore MouseEvent pointer |
| 254 |
return Event{e.Type, e.Char, nil} |
| 255 |
} |
| 256 |
|
| 257 |
// Printable returns true if the event is a printable character that can be |
| 258 |
// inserted into the query (e.g. via the 'put' action). |
| 259 |
func (e Event) Printable() bool { |
| 260 |
return e.Type == Rune && unicode.IsGraphic(e.Char) |
| 261 |
} |
| 262 |
|
| 263 |
func (e Event) KeyName() string { |
| 264 |
if me := e.MouseEvent; me != nil { |
| 265 |
return me.Name() |
| 266 |
} |
| 267 |
|
| 268 |
if e.Type >= Invalid { |
| 269 |
return "" |
| 270 |
} |
| 271 |
|
| 272 |
switch e.Type { |
| 273 |
case Rune: |
| 274 |
if e.Char == ' ' { |
| 275 |
return "space" |
| 276 |
} |
| 277 |
return string(e.Char) |
| 278 |
case Alt: |
| 279 |
return "alt-" + string(e.Char) |
| 280 |
case CtrlAlt: |
| 281 |
return "ctrl-alt-" + string(e.Char) |
| 282 |
case CtrlBackSlash: |
| 283 |
return "ctrl-\\" |
| 284 |
case CtrlRightBracket: |
| 285 |
return "ctrl-]" |
| 286 |
case CtrlCaret: |
| 287 |
return "ctrl-^" |
| 288 |
case CtrlSlash: |
| 289 |
return "ctrl-/" |
| 290 |
} |
| 291 |
|
| 292 |
return util.ToKebabCase(e.Type.String()) |
| 293 |
} |
| 294 |
|
| 295 |
func Key(r rune) Event { |
| 296 |
return Event{Rune, r, nil} |
| 297 |
} |
| 298 |
|
| 299 |
func AltKey(r rune) Event { |
| 300 |
return Event{Alt, r, nil} |
| 301 |
} |
| 302 |
|
| 303 |
func CtrlAltKey(r rune) Event { |
| 304 |
return Event{CtrlAlt, r, nil} |
| 305 |
} |
| 306 |
|
| 307 |
const ( |
| 308 |
doubleClickDuration = 500 * time.Millisecond |
| 309 |
) |
| 310 |
|
| 311 |
type Color int32 |
| 312 |
|
| 313 |
func (c Color) IsDefault() bool { |
| 314 |
return c == colDefault |
| 315 |
} |
| 316 |
|
| 317 |
func (c Color) is24() bool { |
| 318 |
return c > 0 && (c&(1<<24)) > 0 |
| 319 |
} |
| 320 |
|
| 321 |
type ColorAttr struct { |
| 322 |
Color Color |
| 323 |
Attr Attr |
| 324 |
} |
| 325 |
|
| 326 |
func (a ColorAttr) IsColorDefined() bool { |
| 327 |
return a.Color != colUndefined |
| 328 |
} |
| 329 |
|
| 330 |
func (a ColorAttr) IsAttrDefined() bool { |
| 331 |
return a.Attr&^BoldForce != AttrUndefined |
| 332 |
} |
| 333 |
|
| 334 |
func (a ColorAttr) IsUndefined() bool { |
| 335 |
return !a.IsColorDefined() && !a.IsAttrDefined() |
| 336 |
} |
| 337 |
|
| 338 |
func NewColorAttr() ColorAttr { |
| 339 |
return ColorAttr{Color: colUndefined, Attr: AttrUndefined} |
| 340 |
} |
| 341 |
|
| 342 |
func (a ColorAttr) Merge(other ColorAttr) ColorAttr { |
| 343 |
if other.Color != colUndefined { |
| 344 |
a.Color = other.Color |
| 345 |
} |
| 346 |
if other.Attr != AttrUndefined { |
| 347 |
a.Attr = a.Attr.Merge(other.Attr) |
| 348 |
} |
| 349 |
return a |
| 350 |
} |
| 351 |
|
| 352 |
const ( |
| 353 |
colUndefined Color = -2 |
| 354 |
colDefault Color = -1 |
| 355 |
) |
| 356 |
|
| 357 |
const ( |
| 358 |
colBlack Color = iota |
| 359 |
colRed |
| 360 |
colGreen |
| 361 |
colYellow |
| 362 |
colBlue |
| 363 |
colMagenta |
| 364 |
colCyan |
| 365 |
colWhite |
| 366 |
colGrey |
| 367 |
colBrightRed |
| 368 |
colBrightGreen |
| 369 |
colBrightYellow |
| 370 |
colBrightBlue |
| 371 |
colBrightMagenta |
| 372 |
colBrightCyan |
| 373 |
colBrightWhite |
| 374 |
) |
| 375 |
|
| 376 |
type FillReturn int |
| 377 |
|
| 378 |
const ( |
| 379 |
FillContinue FillReturn = iota |
| 380 |
FillNextLine |
| 381 |
FillSuspend |
| 382 |
) |
| 383 |
|
| 384 |
type ColorPair struct { |
| 385 |
fg Color |
| 386 |
bg Color |
| 387 |
ul Color |
| 388 |
attr Attr |
| 389 |
} |
| 390 |
|
| 391 |
func HexToColor(rrggbb string) Color { |
| 392 |
r, _ := strconv.ParseInt(rrggbb[1:3], 16, 0) |
| 393 |
g, _ := strconv.ParseInt(rrggbb[3:5], 16, 0) |
| 394 |
b, _ := strconv.ParseInt(rrggbb[5:7], 16, 0) |
| 395 |
return Color((1 << 24) + (r << 16) + (g << 8) + b) |
| 396 |
} |
| 397 |
|
| 398 |
func NewColorPair(fg Color, bg Color, attr Attr) ColorPair { |
| 399 |
return ColorPair{fg, bg, colDefault, attr} |
| 400 |
} |
| 401 |
|
| 402 |
func NoColorPair() ColorPair { |
| 403 |
return ColorPair{-1, -1, -1, 0} |
| 404 |
} |
| 405 |
|
| 406 |
func (p ColorPair) Fg() Color { |
| 407 |
return p.fg |
| 408 |
} |
| 409 |
|
| 410 |
func (p ColorPair) Bg() Color { |
| 411 |
return p.bg |
| 412 |
} |
| 413 |
|
| 414 |
func (p ColorPair) Ul() Color { |
| 415 |
return p.ul |
| 416 |
} |
| 417 |
|
| 418 |
func (p ColorPair) WithUl(ul Color) ColorPair { |
| 419 |
dup := p |
| 420 |
dup.ul = ul |
| 421 |
return dup |
| 422 |
} |
| 423 |
|
| 424 |
func (p ColorPair) Attr() Attr { |
| 425 |
return p.attr |
| 426 |
} |
| 427 |
|
| 428 |
func (p ColorPair) IsFullBgMarker() bool { |
| 429 |
return p.attr&FullBg > 0 |
| 430 |
} |
| 431 |
|
| 432 |
func (p ColorPair) ShouldStripColors() bool { |
| 433 |
return p.attr&Strip > 0 |
| 434 |
} |
| 435 |
|
| 436 |
func (p ColorPair) HasBg() bool { |
| 437 |
return p.attr&Reverse == 0 && p.bg != colDefault || |
| 438 |
p.attr&Reverse > 0 && p.fg != colDefault |
| 439 |
} |
| 440 |
|
| 441 |
func (p ColorPair) merge(other ColorPair, except Color) ColorPair { |
| 442 |
dup := p |
| 443 |
dup.attr = dup.attr.Merge(other.attr) |
| 444 |
if other.fg != except { |
| 445 |
dup.fg = other.fg |
| 446 |
} |
| 447 |
if other.bg != except { |
| 448 |
dup.bg = other.bg |
| 449 |
} |
| 450 |
if other.ul != except { |
| 451 |
dup.ul = other.ul |
| 452 |
} |
| 453 |
return dup |
| 454 |
} |
| 455 |
|
| 456 |
func (p ColorPair) WithAttr(attr Attr) ColorPair { |
| 457 |
dup := p |
| 458 |
dup.attr = dup.attr.Merge(attr) |
| 459 |
return dup |
| 460 |
} |
| 461 |
|
| 462 |
func (p ColorPair) WithNewAttr(attr Attr) ColorPair { |
| 463 |
dup := p |
| 464 |
dup.attr = attr |
| 465 |
return dup |
| 466 |
} |
| 467 |
|
| 468 |
func (p ColorPair) WithFg(fg ColorAttr) ColorPair { |
| 469 |
dup := p |
| 470 |
fgPair := ColorPair{fg.Color, colUndefined, colUndefined, fg.Attr} |
| 471 |
return dup.Merge(fgPair) |
| 472 |
} |
| 473 |
|
| 474 |
func (p ColorPair) WithBg(bg ColorAttr) ColorPair { |
| 475 |
dup := p |
| 476 |
bgPair := ColorPair{colUndefined, bg.Color, colUndefined, bg.Attr} |
| 477 |
return dup.Merge(bgPair) |
| 478 |
} |
| 479 |
|
| 480 |
func (p ColorPair) MergeAttr(other ColorPair) ColorPair { |
| 481 |
return p.WithAttr(other.attr) |
| 482 |
} |
| 483 |
|
| 484 |
func (p ColorPair) Merge(other ColorPair) ColorPair { |
| 485 |
return p.merge(other, colUndefined) |
| 486 |
} |
| 487 |
|
| 488 |
func (p ColorPair) MergeNonDefault(other ColorPair) ColorPair { |
| 489 |
return p.merge(other, colDefault) |
| 490 |
} |
| 491 |
|
| 492 |
type ColorTheme struct { |
| 493 |
Colored bool |
| 494 |
Input ColorAttr |
| 495 |
Ghost ColorAttr |
| 496 |
Disabled ColorAttr |
| 497 |
Fg ColorAttr |
| 498 |
Bg ColorAttr |
| 499 |
ListFg ColorAttr |
| 500 |
ListBg ColorAttr |
| 501 |
AltBg ColorAttr |
| 502 |
Nth ColorAttr |
| 503 |
Nomatch ColorAttr |
| 504 |
SelectedFg ColorAttr |
| 505 |
SelectedBg ColorAttr |
| 506 |
SelectedMatch ColorAttr |
| 507 |
PreviewFg ColorAttr |
| 508 |
PreviewBg ColorAttr |
| 509 |
DarkBg ColorAttr |
| 510 |
Gutter ColorAttr |
| 511 |
AltGutter ColorAttr |
| 512 |
Prompt ColorAttr |
| 513 |
InputBg ColorAttr |
| 514 |
InputBorder ColorAttr |
| 515 |
InputLabel ColorAttr |
| 516 |
Match ColorAttr |
| 517 |
Current ColorAttr |
| 518 |
CurrentMatch ColorAttr |
| 519 |
Spinner ColorAttr |
| 520 |
Info ColorAttr |
| 521 |
Pointer ColorAttr |
| 522 |
Marker ColorAttr |
| 523 |
Header ColorAttr |
| 524 |
HeaderBg ColorAttr |
| 525 |
HeaderBorder ColorAttr |
| 526 |
HeaderLabel ColorAttr |
| 527 |
Footer ColorAttr |
| 528 |
FooterBg ColorAttr |
| 529 |
FooterBorder ColorAttr |
| 530 |
FooterLabel ColorAttr |
| 531 |
Separator ColorAttr |
| 532 |
Scrollbar ColorAttr |
| 533 |
Border ColorAttr |
| 534 |
PreviewBorder ColorAttr |
| 535 |
PreviewLabel ColorAttr |
| 536 |
PreviewScrollbar ColorAttr |
| 537 |
BorderLabel ColorAttr |
| 538 |
ListLabel ColorAttr |
| 539 |
ListBorder ColorAttr |
| 540 |
GapLine ColorAttr |
| 541 |
NthCurrentAttr Attr // raw current-fg attr (before fg merge) for nth overlay |
| 542 |
NthSelectedAttr Attr // raw selected-fg attr (before ListFg inherit) for nth overlay |
| 543 |
} |
| 544 |
|
| 545 |
type Event struct { |
| 546 |
Type EventType |
| 547 |
Char rune |
| 548 |
MouseEvent *MouseEvent |
| 549 |
} |
| 550 |
|
| 551 |
type MouseEvent struct { |
| 552 |
Y int |
| 553 |
X int |
| 554 |
S int |
| 555 |
Left bool |
| 556 |
Down bool |
| 557 |
Double bool |
| 558 |
Ctrl bool |
| 559 |
Alt bool |
| 560 |
Shift bool |
| 561 |
} |
| 562 |
|
| 563 |
func (e MouseEvent) Mod() bool { |
| 564 |
return e.Ctrl || e.Alt || e.Shift |
| 565 |
} |
| 566 |
|
| 567 |
func (e MouseEvent) Name() string { |
| 568 |
name := "" |
| 569 |
if e.Down { |
| 570 |
return name |
| 571 |
} |
| 572 |
|
| 573 |
if e.Ctrl { |
| 574 |
name += "ctrl-" |
| 575 |
} |
| 576 |
if e.Alt { |
| 577 |
name += "alt-" |
| 578 |
} |
| 579 |
if e.Shift { |
| 580 |
name += "shift-" |
| 581 |
} |
| 582 |
if e.Double { |
| 583 |
name += "double-" |
| 584 |
} |
| 585 |
if !e.Left { |
| 586 |
name += "right-" |
| 587 |
} |
| 588 |
return name + "click" |
| 589 |
} |
| 590 |
|
| 591 |
type BorderShape int |
| 592 |
|
| 593 |
const ( |
| 594 |
BorderUndefined BorderShape = iota |
| 595 |
BorderLine |
| 596 |
BorderNone |
| 597 |
BorderPhantom |
| 598 |
BorderRounded |
| 599 |
BorderSharp |
| 600 |
BorderBold |
| 601 |
BorderBlock |
| 602 |
BorderThinBlock |
| 603 |
BorderDouble |
| 604 |
BorderHorizontal |
| 605 |
BorderVertical |
| 606 |
BorderTop |
| 607 |
BorderBottom |
| 608 |
BorderLeft |
| 609 |
BorderRight |
| 610 |
BorderInline |
| 611 |
BorderDashed |
| 612 |
) |
| 613 |
|
| 614 |
func (s BorderShape) HasLeft() bool { |
| 615 |
switch s { |
| 616 |
case BorderNone, BorderPhantom, BorderLine, BorderInline, BorderRight, BorderTop, BorderBottom, BorderHorizontal: // No Left |
| 617 |
return false |
| 618 |
} |
| 619 |
return true |
| 620 |
} |
| 621 |
|
| 622 |
func (s BorderShape) HasRight() bool { |
| 623 |
switch s { |
| 624 |
case BorderNone, BorderPhantom, BorderLine, BorderInline, BorderLeft, BorderTop, BorderBottom, BorderHorizontal: // No right |
| 625 |
return false |
| 626 |
} |
| 627 |
return true |
| 628 |
} |
| 629 |
|
| 630 |
func (s BorderShape) HasTop() bool { |
| 631 |
switch s { |
| 632 |
case BorderNone, BorderPhantom, BorderLine, BorderInline, BorderLeft, BorderRight, BorderBottom, BorderVertical: // No top |
| 633 |
return false |
| 634 |
} |
| 635 |
return true |
| 636 |
} |
| 637 |
|
| 638 |
func (s BorderShape) HasBottom() bool { |
| 639 |
switch s { |
| 640 |
case BorderNone, BorderPhantom, BorderLine, BorderInline, BorderLeft, BorderRight, BorderTop, BorderVertical: // No bottom |
| 641 |
return false |
| 642 |
} |
| 643 |
return true |
| 644 |
} |
| 645 |
|
| 646 |
func (s BorderShape) Visible() bool { |
| 647 |
return s != BorderNone |
| 648 |
} |
| 649 |
|
| 650 |
type BorderStyle struct { |
| 651 |
shape BorderShape |
| 652 |
top rune |
| 653 |
bottom rune |
| 654 |
left rune |
| 655 |
right rune |
| 656 |
topLeft rune |
| 657 |
topRight rune |
| 658 |
bottomLeft rune |
| 659 |
bottomRight rune |
| 660 |
leftMid rune |
| 661 |
rightMid rune |
| 662 |
} |
| 663 |
|
| 664 |
type BorderCharacter int |
| 665 |
|
| 666 |
func MakeBorderStyle(shape BorderShape, unicode bool) BorderStyle { |
| 667 |
if shape == BorderNone || shape == BorderPhantom { |
| 668 |
return BorderStyle{ |
| 669 |
shape: BorderNone, |
| 670 |
top: ' ', |
| 671 |
bottom: ' ', |
| 672 |
left: ' ', |
| 673 |
right: ' ', |
| 674 |
topLeft: ' ', |
| 675 |
topRight: ' ', |
| 676 |
bottomLeft: ' ', |
| 677 |
bottomRight: ' ', |
| 678 |
leftMid: ' ', |
| 679 |
rightMid: ' '} |
| 680 |
} |
| 681 |
if !unicode { |
| 682 |
return BorderStyle{ |
| 683 |
shape: shape, |
| 684 |
top: '-', |
| 685 |
bottom: '-', |
| 686 |
left: '|', |
| 687 |
right: '|', |
| 688 |
topLeft: '+', |
| 689 |
topRight: '+', |
| 690 |
bottomLeft: '+', |
| 691 |
bottomRight: '+', |
| 692 |
leftMid: '+', |
| 693 |
rightMid: '+', |
| 694 |
} |
| 695 |
} |
| 696 |
switch shape { |
| 697 |
case BorderSharp: |
| 698 |
return BorderStyle{ |
| 699 |
shape: shape, |
| 700 |
top: '─', |
| 701 |
bottom: '─', |
| 702 |
left: '│', |
| 703 |
right: '│', |
| 704 |
topLeft: '┌', |
| 705 |
topRight: '┐', |
| 706 |
bottomLeft: '└', |
| 707 |
bottomRight: '┘', |
| 708 |
leftMid: '├', |
| 709 |
rightMid: '┤', |
| 710 |
} |
| 711 |
case BorderBold: |
| 712 |
return BorderStyle{ |
| 713 |
shape: shape, |
| 714 |
top: '━', |
| 715 |
bottom: '━', |
| 716 |
left: '┃', |
| 717 |
right: '┃', |
| 718 |
topLeft: '┏', |
| 719 |
topRight: '┓', |
| 720 |
bottomLeft: '┗', |
| 721 |
bottomRight: '┛', |
| 722 |
leftMid: '┣', |
| 723 |
rightMid: '┫', |
| 724 |
} |
| 725 |
case BorderBlock: |
| 726 |
// ▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜ |
| 727 |
// ▌ ▐ |
| 728 |
// ▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟ |
| 729 |
return BorderStyle{ |
| 730 |
shape: shape, |
| 731 |
top: '▀', |
| 732 |
bottom: '▄', |
| 733 |
left: '▌', |
| 734 |
right: '▐', |
| 735 |
topLeft: '▛', |
| 736 |
topRight: '▜', |
| 737 |
bottomLeft: '▙', |
| 738 |
bottomRight: '▟', |
| 739 |
leftMid: '▌', |
| 740 |
rightMid: '▐', |
| 741 |
} |
| 742 |
|
| 743 |
case BorderThinBlock: |
| 744 |
// 🭽▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔🭾 |
| 745 |
// ▏ ▕ |
| 746 |
// 🭼▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁🭿 |
| 747 |
return BorderStyle{ |
| 748 |
shape: shape, |
| 749 |
top: '▔', |
| 750 |
bottom: '▁', |
| 751 |
left: '▏', |
| 752 |
right: '▕', |
| 753 |
topLeft: '🭽', |
| 754 |
topRight: '🭾', |
| 755 |
bottomLeft: '🭼', |
| 756 |
bottomRight: '🭿', |
| 757 |
leftMid: '▏', |
| 758 |
rightMid: '▕', |
| 759 |
} |
| 760 |
|
| 761 |
case BorderDouble: |
| 762 |
return BorderStyle{ |
| 763 |
shape: shape, |
| 764 |
top: '═', |
| 765 |
bottom: '═', |
| 766 |
left: '║', |
| 767 |
right: '║', |
| 768 |
topLeft: '╔', |
| 769 |
topRight: '╗', |
| 770 |
bottomLeft: '╚', |
| 771 |
bottomRight: '╝', |
| 772 |
leftMid: '╠', |
| 773 |
rightMid: '╣', |
| 774 |
} |
| 775 |
case BorderDashed: |
| 776 |
// Terminal cells are taller than wide (~2:1), so horizontals can use a |
| 777 |
// sparse stub per cell while verticals need more dashes per cell to look |
| 778 |
// evenly dashed. Rounded corners and sharp T-junction mids. |
| 779 |
return BorderStyle{ |
| 780 |
shape: shape, |
| 781 |
top: '╶', |
| 782 |
bottom: '╶', |
| 783 |
left: '┆', |
| 784 |
right: '┆', |
| 785 |
topLeft: '╭', |
| 786 |
topRight: '╮', |
| 787 |
bottomLeft: '╰', |
| 788 |
bottomRight: '╯', |
| 789 |
leftMid: '├', |
| 790 |
rightMid: '┤', |
| 791 |
} |
| 792 |
} |
| 793 |
return BorderStyle{ |
| 794 |
shape: shape, |
| 795 |
top: '─', |
| 796 |
bottom: '─', |
| 797 |
left: '│', |
| 798 |
right: '│', |
| 799 |
topLeft: '╭', |
| 800 |
topRight: '╮', |
| 801 |
bottomLeft: '╰', |
| 802 |
bottomRight: '╯', |
| 803 |
leftMid: '├', |
| 804 |
rightMid: '┤', |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
type TermSize struct { |
| 809 |
Lines int |
| 810 |
Columns int |
| 811 |
PxWidth int |
| 812 |
PxHeight int |
| 813 |
} |
| 814 |
|
| 815 |
type WindowType int |
| 816 |
|
| 817 |
const ( |
| 818 |
WindowBase WindowType = iota |
| 819 |
WindowList |
| 820 |
WindowPreview |
| 821 |
WindowInput |
| 822 |
WindowHeader |
| 823 |
WindowFooter |
| 824 |
) |
| 825 |
|
| 826 |
// BorderColor returns the ColorPair used to draw borders for the given WindowType. |
| 827 |
func BorderColor(wt WindowType) ColorPair { |
| 828 |
switch wt { |
| 829 |
case WindowList: |
| 830 |
return ColListBorder |
| 831 |
case WindowInput: |
| 832 |
return ColInputBorder |
| 833 |
case WindowHeader: |
| 834 |
return ColHeaderBorder |
| 835 |
case WindowFooter: |
| 836 |
return ColFooterBorder |
| 837 |
case WindowPreview: |
| 838 |
return ColPreviewBorder |
| 839 |
} |
| 840 |
return ColBorder |
| 841 |
} |
| 842 |
|
| 843 |
// SectionEdge selects which outer edge of the frame an inline section |
| 844 |
// should claim when PaintSectionFrame overpaints its adjacent border. |
| 845 |
// SectionEdgeNone paints only the inner verticals (for sections that |
| 846 |
// don't touch the outer top or bottom). |
| 847 |
type SectionEdge int |
| 848 |
|
| 849 |
const ( |
| 850 |
SectionEdgeNone SectionEdge = iota |
| 851 |
SectionEdgeTop |
| 852 |
SectionEdgeBottom |
| 853 |
) |
| 854 |
|
| 855 |
type Renderer interface { |
| 856 |
DefaultTheme() *ColorTheme |
| 857 |
Init() error |
| 858 |
Resize(maxHeightFunc func(int) int) |
| 859 |
Pause(clear bool) |
| 860 |
Resume(clear bool, sigcont bool) |
| 861 |
Clear() |
| 862 |
RefreshWindows(windows []Window) |
| 863 |
Refresh() |
| 864 |
Close() |
| 865 |
PassThrough(string) |
| 866 |
NeedScrollbarRedraw() bool |
| 867 |
ShouldEmitResizeEvent() bool |
| 868 |
Bell() |
| 869 |
HideCursor() |
| 870 |
ShowCursor() |
| 871 |
|
| 872 |
GetChar(cancellable bool) Event |
| 873 |
CancelGetChar() |
| 874 |
|
| 875 |
Top() int |
| 876 |
MaxX() int |
| 877 |
MaxY() int |
| 878 |
|
| 879 |
Size() TermSize |
| 880 |
|
| 881 |
NewWindow(top int, left int, width int, height int, windowType WindowType, borderStyle BorderStyle, erase bool) Window |
| 882 |
} |
| 883 |
|
| 884 |
type Window interface { |
| 885 |
Top() int |
| 886 |
Left() int |
| 887 |
Width() int |
| 888 |
Height() int |
| 889 |
|
| 890 |
DrawBorder() |
| 891 |
DrawHBorder() |
| 892 |
// DrawHSeparator draws an inline horizontal separator at `row` (relative to the |
| 893 |
// window's top) using the color for `windowType`. The separator is conceptually |
| 894 |
// the section's inner edge (e.g. the bottom border of an inline header), so the |
| 895 |
// whole row including junctions carries the section's fg + bg. When useBottom is |
| 896 |
// true the `bottom` horizontal char is used instead of `top`; for thinblock/block |
| 897 |
// styles this keeps the thin line bonded to the list content on the opposite side. |
| 898 |
DrawHSeparator(row int, windowType WindowType, useBottom bool) |
| 899 |
// PaintSectionFrame overpaints the border cells around the rows [topContent, |
| 900 |
// bottomContent] (inclusive, relative to the window's top) with the color for |
| 901 |
// `windowType`. When edge is SectionEdgeTop / SectionEdgeBottom, the |
| 902 |
// corresponding outer horizontal (+ corners) is also painted, letting the |
| 903 |
// inline section claim that edge of the outer frame. |
| 904 |
PaintSectionFrame(topContent, bottomContent int, windowType WindowType, edge SectionEdge) |
| 905 |
Refresh() |
| 906 |
FinishFill() |
| 907 |
|
| 908 |
X() int |
| 909 |
Y() int |
| 910 |
EncloseX(x int) bool |
| 911 |
EncloseY(y int) bool |
| 912 |
Enclose(y int, x int) bool |
| 913 |
|
| 914 |
Move(y int, x int) |
| 915 |
MoveAndClear(y int, x int) |
| 916 |
Print(text string) |
| 917 |
CPrint(color ColorPair, text string) |
| 918 |
Fill(text string) FillReturn |
| 919 |
CFill(fg Color, bg Color, ul Color, attr Attr, text string) FillReturn |
| 920 |
LinkBegin(uri string, params string) |
| 921 |
LinkEnd() |
| 922 |
Erase() |
| 923 |
EraseMaybe() bool |
| 924 |
|
| 925 |
SetWrapSign(string, int) |
| 926 |
} |
| 927 |
|
| 928 |
type FullscreenRenderer struct { |
| 929 |
theme *ColorTheme |
| 930 |
mouse bool |
| 931 |
forceBlack bool |
| 932 |
tabstop int |
| 933 |
prevDownTime time.Time |
| 934 |
clicks [][2]int |
| 935 |
showCursor bool |
| 936 |
} |
| 937 |
|
| 938 |
func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool, tabstop int) Renderer { |
| 939 |
r := &FullscreenRenderer{ |
| 940 |
theme: theme, |
| 941 |
mouse: mouse, |
| 942 |
forceBlack: forceBlack, |
| 943 |
tabstop: tabstop, |
| 944 |
prevDownTime: time.Unix(0, 0), |
| 945 |
clicks: [][2]int{}, |
| 946 |
showCursor: true} |
| 947 |
return r |
| 948 |
} |
| 949 |
|
| 950 |
var ( |
| 951 |
NoColorTheme *ColorTheme |
| 952 |
EmptyTheme *ColorTheme |
| 953 |
Default16 *ColorTheme |
| 954 |
Dark256 *ColorTheme |
| 955 |
Light256 *ColorTheme |
| 956 |
|
| 957 |
ColPrompt ColorPair |
| 958 |
ColNormal ColorPair |
| 959 |
ColInput ColorPair |
| 960 |
ColDisabled ColorPair |
| 961 |
ColGhost ColorPair |
| 962 |
ColMatch ColorPair |
| 963 |
ColPointer ColorPair |
| 964 |
ColPointerEmpty ColorPair |
| 965 |
ColPointerEmptyChar ColorPair |
| 966 |
ColAltPointerEmpty ColorPair |
| 967 |
ColAltPointerEmptyChar ColorPair |
| 968 |
ColMarker ColorPair |
| 969 |
ColSelected ColorPair |
| 970 |
ColSelectedMatch ColorPair |
| 971 |
ColCurrent ColorPair |
| 972 |
ColCurrentMatch ColorPair |
| 973 |
ColCurrentPointer ColorPair |
| 974 |
ColCurrentPointerEmpty ColorPair |
| 975 |
ColCurrentMarker ColorPair |
| 976 |
ColCurrentSelectedEmpty ColorPair |
| 977 |
ColSpinner ColorPair |
| 978 |
ColInfo ColorPair |
| 979 |
ColHeader ColorPair |
| 980 |
ColHeaderBorder ColorPair |
| 981 |
ColHeaderLabel ColorPair |
| 982 |
ColFooter ColorPair |
| 983 |
ColFooterBorder ColorPair |
| 984 |
ColFooterLabel ColorPair |
| 985 |
ColSeparator ColorPair |
| 986 |
ColScrollbar ColorPair |
| 987 |
ColGapLine ColorPair |
| 988 |
ColBorder ColorPair |
| 989 |
ColPreview ColorPair |
| 990 |
ColPreviewBorder ColorPair |
| 991 |
ColBorderLabel ColorPair |
| 992 |
ColPreviewLabel ColorPair |
| 993 |
ColPreviewScrollbar ColorPair |
| 994 |
ColPreviewSpinner ColorPair |
| 995 |
ColListBorder ColorPair |
| 996 |
ColListLabel ColorPair |
| 997 |
ColInputBorder ColorPair |
| 998 |
ColInputLabel ColorPair |
| 999 |
) |
| 1000 |
|
| 1001 |
func init() { |
| 1002 |
defaultColor := ColorAttr{colDefault, AttrUndefined} |
| 1003 |
undefined := ColorAttr{colUndefined, AttrUndefined} |
| 1004 |
|
| 1005 |
NoColorTheme = &ColorTheme{ |
| 1006 |
Colored: false, |
| 1007 |
// Root colors. Everything else is left undefined so that overriding a |
| 1008 |
// root (e.g. --color bw,bg:blue) propagates to the derived colors, |
| 1009 |
// just like in the colored base themes. |
| 1010 |
Input: defaultColor, |
| 1011 |
Fg: defaultColor, |
| 1012 |
Bg: defaultColor, |
| 1013 |
DarkBg: defaultColor, |
| 1014 |
Prompt: defaultColor, |
| 1015 |
Match: defaultColor, |
| 1016 |
Spinner: defaultColor, |
| 1017 |
Info: defaultColor, |
| 1018 |
Pointer: defaultColor, |
| 1019 |
Marker: defaultColor, |
| 1020 |
Header: defaultColor, |
| 1021 |
Footer: defaultColor, |
| 1022 |
BorderLabel: defaultColor, |
| 1023 |
// Derived colors. Left undefined so they inherit from a root. |
| 1024 |
ListFg: undefined, |
| 1025 |
ListBg: undefined, |
| 1026 |
AltBg: undefined, |
| 1027 |
SelectedFg: undefined, |
| 1028 |
SelectedBg: undefined, |
| 1029 |
SelectedMatch: undefined, |
| 1030 |
Current: undefined, |
| 1031 |
CurrentMatch: undefined, |
| 1032 |
Border: undefined, |
| 1033 |
Ghost: undefined, |
| 1034 |
Disabled: undefined, |
| 1035 |
PreviewFg: undefined, |
| 1036 |
PreviewBg: undefined, |
| 1037 |
Gutter: undefined, |
| 1038 |
AltGutter: undefined, |
| 1039 |
PreviewBorder: undefined, |
| 1040 |
PreviewScrollbar: undefined, |
| 1041 |
PreviewLabel: undefined, |
| 1042 |
ListLabel: undefined, |
| 1043 |
ListBorder: undefined, |
| 1044 |
Separator: undefined, |
| 1045 |
Scrollbar: undefined, |
| 1046 |
InputBg: undefined, |
| 1047 |
InputBorder: undefined, |
| 1048 |
InputLabel: undefined, |
| 1049 |
HeaderBg: undefined, |
| 1050 |
HeaderBorder: undefined, |
| 1051 |
HeaderLabel: undefined, |
| 1052 |
FooterBg: undefined, |
| 1053 |
FooterBorder: undefined, |
| 1054 |
FooterLabel: undefined, |
| 1055 |
GapLine: undefined, |
| 1056 |
Nth: undefined, |
| 1057 |
Nomatch: undefined, |
| 1058 |
} |
| 1059 |
|
| 1060 |
EmptyTheme = &ColorTheme{ |
| 1061 |
Colored: true, |
| 1062 |
Input: undefined, |
| 1063 |
Fg: undefined, |
| 1064 |
Bg: undefined, |
| 1065 |
ListFg: undefined, |
| 1066 |
ListBg: undefined, |
| 1067 |
AltBg: undefined, |
| 1068 |
SelectedFg: undefined, |
| 1069 |
SelectedBg: undefined, |
| 1070 |
SelectedMatch: undefined, |
| 1071 |
DarkBg: undefined, |
| 1072 |
Prompt: undefined, |
| 1073 |
Match: undefined, |
| 1074 |
Current: undefined, |
| 1075 |
CurrentMatch: undefined, |
| 1076 |
Spinner: undefined, |
| 1077 |
Info: undefined, |
| 1078 |
Pointer: undefined, |
| 1079 |
Marker: undefined, |
| 1080 |
Header: undefined, |
| 1081 |
Footer: undefined, |
| 1082 |
Border: undefined, |
| 1083 |
BorderLabel: undefined, |
| 1084 |
ListLabel: undefined, |
| 1085 |
ListBorder: undefined, |
| 1086 |
Ghost: undefined, |
| 1087 |
Disabled: undefined, |
| 1088 |
PreviewFg: undefined, |
| 1089 |
PreviewBg: undefined, |
| 1090 |
Gutter: undefined, |
| 1091 |
AltGutter: undefined, |
| 1092 |
PreviewBorder: undefined, |
| 1093 |
PreviewScrollbar: undefined, |
| 1094 |
PreviewLabel: undefined, |
| 1095 |
Separator: undefined, |
| 1096 |
Scrollbar: undefined, |
| 1097 |
InputBg: undefined, |
| 1098 |
InputBorder: undefined, |
| 1099 |
InputLabel: undefined, |
| 1100 |
HeaderBg: undefined, |
| 1101 |
HeaderBorder: undefined, |
| 1102 |
HeaderLabel: undefined, |
| 1103 |
FooterBg: undefined, |
| 1104 |
FooterBorder: undefined, |
| 1105 |
FooterLabel: undefined, |
| 1106 |
GapLine: undefined, |
| 1107 |
Nth: undefined, |
| 1108 |
Nomatch: undefined, |
| 1109 |
} |
| 1110 |
|
| 1111 |
Default16 = &ColorTheme{ |
| 1112 |
Colored: true, |
| 1113 |
Input: defaultColor, |
| 1114 |
Fg: defaultColor, |
| 1115 |
Bg: defaultColor, |
| 1116 |
ListFg: undefined, |
| 1117 |
ListBg: undefined, |
| 1118 |
AltBg: undefined, |
| 1119 |
SelectedFg: undefined, |
| 1120 |
SelectedBg: undefined, |
| 1121 |
SelectedMatch: undefined, |
| 1122 |
DarkBg: ColorAttr{colGrey, AttrUndefined}, |
| 1123 |
Prompt: ColorAttr{colBlue, AttrUndefined}, |
| 1124 |
Match: ColorAttr{colGreen, AttrUndefined}, |
| 1125 |
Current: ColorAttr{colBrightWhite, AttrUndefined}, |
| 1126 |
CurrentMatch: ColorAttr{colBrightGreen, AttrUndefined}, |
| 1127 |
Spinner: ColorAttr{colGreen, AttrUndefined}, |
| 1128 |
Info: ColorAttr{colYellow, AttrUndefined}, |
| 1129 |
Pointer: ColorAttr{colRed, AttrUndefined}, |
| 1130 |
Marker: ColorAttr{colMagenta, AttrUndefined}, |
| 1131 |
Header: ColorAttr{colCyan, AttrUndefined}, |
| 1132 |
Footer: ColorAttr{colCyan, AttrUndefined}, |
| 1133 |
Border: undefined, |
| 1134 |
BorderLabel: defaultColor, |
| 1135 |
Ghost: undefined, |
| 1136 |
Disabled: undefined, |
| 1137 |
PreviewFg: undefined, |
| 1138 |
PreviewBg: undefined, |
| 1139 |
Gutter: undefined, |
| 1140 |
AltGutter: undefined, |
| 1141 |
PreviewBorder: undefined, |
| 1142 |
PreviewScrollbar: undefined, |
| 1143 |
PreviewLabel: undefined, |
| 1144 |
ListLabel: undefined, |
| 1145 |
ListBorder: undefined, |
| 1146 |
Separator: undefined, |
| 1147 |
Scrollbar: undefined, |
| 1148 |
InputBg: undefined, |
| 1149 |
InputBorder: undefined, |
| 1150 |
InputLabel: undefined, |
| 1151 |
HeaderBg: undefined, |
| 1152 |
HeaderBorder: undefined, |
| 1153 |
HeaderLabel: undefined, |
| 1154 |
FooterBg: undefined, |
| 1155 |
FooterBorder: undefined, |
| 1156 |
FooterLabel: undefined, |
| 1157 |
GapLine: undefined, |
| 1158 |
Nth: undefined, |
| 1159 |
Nomatch: undefined, |
| 1160 |
} |
| 1161 |
|
| 1162 |
Dark256 = &ColorTheme{ |
| 1163 |
Colored: true, |
| 1164 |
Input: defaultColor, |
| 1165 |
Fg: defaultColor, |
| 1166 |
Bg: defaultColor, |
| 1167 |
ListFg: undefined, |
| 1168 |
ListBg: undefined, |
| 1169 |
AltBg: undefined, |
| 1170 |
SelectedFg: undefined, |
| 1171 |
SelectedBg: undefined, |
| 1172 |
SelectedMatch: undefined, |
| 1173 |
DarkBg: ColorAttr{236, AttrUndefined}, |
| 1174 |
Prompt: ColorAttr{110, AttrUndefined}, |
| 1175 |
Match: ColorAttr{108, AttrUndefined}, |
| 1176 |
Current: ColorAttr{254, AttrUndefined}, |
| 1177 |
CurrentMatch: ColorAttr{151, AttrUndefined}, |
| 1178 |
Spinner: ColorAttr{148, AttrUndefined}, |
| 1179 |
Info: ColorAttr{144, AttrUndefined}, |
| 1180 |
Pointer: ColorAttr{161, AttrUndefined}, |
| 1181 |
Marker: ColorAttr{168, AttrUndefined}, |
| 1182 |
Header: ColorAttr{109, AttrUndefined}, |
| 1183 |
Footer: ColorAttr{109, AttrUndefined}, |
| 1184 |
Border: ColorAttr{59, AttrUndefined}, |
| 1185 |
BorderLabel: ColorAttr{145, AttrUndefined}, |
| 1186 |
Ghost: undefined, |
| 1187 |
Disabled: undefined, |
| 1188 |
PreviewFg: undefined, |
| 1189 |
PreviewBg: undefined, |
| 1190 |
Gutter: undefined, |
| 1191 |
AltGutter: undefined, |
| 1192 |
PreviewBorder: undefined, |
| 1193 |
PreviewScrollbar: undefined, |
| 1194 |
PreviewLabel: undefined, |
| 1195 |
ListLabel: undefined, |
| 1196 |
ListBorder: undefined, |
| 1197 |
Separator: undefined, |
| 1198 |
Scrollbar: undefined, |
| 1199 |
InputBg: undefined, |
| 1200 |
InputBorder: undefined, |
| 1201 |
InputLabel: undefined, |
| 1202 |
HeaderBg: undefined, |
| 1203 |
HeaderBorder: undefined, |
| 1204 |
HeaderLabel: undefined, |
| 1205 |
FooterBg: undefined, |
| 1206 |
FooterBorder: undefined, |
| 1207 |
FooterLabel: undefined, |
| 1208 |
GapLine: undefined, |
| 1209 |
Nth: undefined, |
| 1210 |
Nomatch: undefined, |
| 1211 |
} |
| 1212 |
|
| 1213 |
Light256 = &ColorTheme{ |
| 1214 |
Colored: true, |
| 1215 |
Input: defaultColor, |
| 1216 |
Fg: defaultColor, |
| 1217 |
Bg: defaultColor, |
| 1218 |
ListFg: undefined, |
| 1219 |
ListBg: undefined, |
| 1220 |
AltBg: undefined, |
| 1221 |
SelectedFg: undefined, |
| 1222 |
SelectedBg: undefined, |
| 1223 |
SelectedMatch: undefined, |
| 1224 |
DarkBg: ColorAttr{251, AttrUndefined}, |
| 1225 |
Prompt: ColorAttr{25, AttrUndefined}, |
| 1226 |
Match: ColorAttr{66, AttrUndefined}, |
| 1227 |
Current: ColorAttr{237, AttrUndefined}, |
| 1228 |
CurrentMatch: ColorAttr{23, AttrUndefined}, |
| 1229 |
Spinner: ColorAttr{65, AttrUndefined}, |
| 1230 |
Info: ColorAttr{101, AttrUndefined}, |
| 1231 |
Pointer: ColorAttr{161, AttrUndefined}, |
| 1232 |
Marker: ColorAttr{168, AttrUndefined}, |
| 1233 |
Header: ColorAttr{31, AttrUndefined}, |
| 1234 |
Footer: ColorAttr{31, AttrUndefined}, |
| 1235 |
Border: ColorAttr{145, AttrUndefined}, |
| 1236 |
BorderLabel: ColorAttr{59, AttrUndefined}, |
| 1237 |
Ghost: undefined, |
| 1238 |
Disabled: undefined, |
| 1239 |
PreviewFg: undefined, |
| 1240 |
PreviewBg: undefined, |
| 1241 |
Gutter: undefined, |
| 1242 |
AltGutter: undefined, |
| 1243 |
PreviewBorder: undefined, |
| 1244 |
PreviewScrollbar: undefined, |
| 1245 |
PreviewLabel: undefined, |
| 1246 |
ListLabel: undefined, |
| 1247 |
ListBorder: undefined, |
| 1248 |
Separator: undefined, |
| 1249 |
Scrollbar: undefined, |
| 1250 |
InputBg: undefined, |
| 1251 |
InputBorder: undefined, |
| 1252 |
InputLabel: undefined, |
| 1253 |
HeaderBg: undefined, |
| 1254 |
HeaderBorder: undefined, |
| 1255 |
HeaderLabel: undefined, |
| 1256 |
FooterBg: undefined, |
| 1257 |
FooterBorder: undefined, |
| 1258 |
FooterLabel: undefined, |
| 1259 |
GapLine: undefined, |
| 1260 |
Nth: undefined, |
| 1261 |
Nomatch: undefined, |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
func InitTheme(theme *ColorTheme, baseTheme *ColorTheme, boldify bool, forceBlack bool, hasInputWindow bool, hasHeaderWindow bool, headerInline bool, footerInline bool) { |
| 1266 |
if forceBlack { |
| 1267 |
theme.Bg = ColorAttr{colBlack, AttrUndefined} |
| 1268 |
} |
| 1269 |
|
| 1270 |
if boldify { |
| 1271 |
boldify := func(c ColorAttr) ColorAttr { |
| 1272 |
dup := c |
| 1273 |
if (c.Attr & AttrRegular) == 0 { |
| 1274 |
dup.Attr |= BoldForce |
| 1275 |
} |
| 1276 |
return dup |
| 1277 |
} |
| 1278 |
theme.Current = boldify(theme.Current) |
| 1279 |
theme.CurrentMatch = boldify(theme.CurrentMatch) |
| 1280 |
theme.Prompt = boldify(theme.Prompt) |
| 1281 |
theme.Input = boldify(theme.Input) |
| 1282 |
theme.Pointer = boldify(theme.Pointer) |
| 1283 |
theme.Spinner = boldify(theme.Spinner) |
| 1284 |
} |
| 1285 |
|
| 1286 |
o := func(a ColorAttr, b ColorAttr) ColorAttr { |
| 1287 |
c := a |
| 1288 |
if b.Color != colUndefined { |
| 1289 |
c.Color = b.Color |
| 1290 |
} |
| 1291 |
if b.Attr != AttrUndefined { |
| 1292 |
c.Attr = b.Attr |
| 1293 |
} |
| 1294 |
return c |
| 1295 |
} |
| 1296 |
theme.Input = o(baseTheme.Input, theme.Input) |
| 1297 |
theme.Fg = o(baseTheme.Fg, theme.Fg) |
| 1298 |
theme.Bg = o(baseTheme.Bg, theme.Bg) |
| 1299 |
theme.DarkBg = o(baseTheme.DarkBg, theme.DarkBg) |
| 1300 |
theme.Prompt = o(baseTheme.Prompt, theme.Prompt) |
| 1301 |
match := theme.Match |
| 1302 |
if !baseTheme.Colored && match.IsUndefined() { |
| 1303 |
match.Attr = Underline |
| 1304 |
} |
| 1305 |
theme.Match = o(baseTheme.Match, match) |
| 1306 |
// These colors are not defined in the base themes. |
| 1307 |
// Resolve ListFg/ListBg early so Current and Selected can inherit from them. |
| 1308 |
theme.ListFg = o(theme.Fg, theme.ListFg) |
| 1309 |
theme.ListBg = o(theme.Bg, theme.ListBg) |
| 1310 |
// Inherit from 'list-fg', so that we don't have to write 'current-fg:dim' |
| 1311 |
// e.g. fzf --delimiter / --nth -1 --color fg:dim,nth:regular |
| 1312 |
current := theme.Current |
| 1313 |
if !baseTheme.Colored && current.IsUndefined() { |
| 1314 |
current.Attr |= Reverse |
| 1315 |
} |
| 1316 |
resolvedCurrent := o(baseTheme.Current, current) |
| 1317 |
theme.NthCurrentAttr = resolvedCurrent.Attr |
| 1318 |
theme.Current = theme.ListFg.Merge(resolvedCurrent) |
| 1319 |
currentMatch := theme.CurrentMatch |
| 1320 |
if !baseTheme.Colored && currentMatch.IsUndefined() { |
| 1321 |
currentMatch.Attr |= Reverse | Underline |
| 1322 |
} |
| 1323 |
theme.CurrentMatch = o(baseTheme.CurrentMatch, currentMatch) |
| 1324 |
theme.Spinner = o(baseTheme.Spinner, theme.Spinner) |
| 1325 |
theme.Info = o(baseTheme.Info, theme.Info) |
| 1326 |
theme.Pointer = o(baseTheme.Pointer, theme.Pointer) |
| 1327 |
theme.Marker = o(baseTheme.Marker, theme.Marker) |
| 1328 |
theme.Header = o(baseTheme.Header, theme.Header) |
| 1329 |
theme.Footer = o(baseTheme.Footer, theme.Footer) |
| 1330 |
|
| 1331 |
// If border color is undefined, set it to default color with dim attribute. |
| 1332 |
border := theme.Border |
| 1333 |
if baseTheme.Border.IsUndefined() && border.IsUndefined() { |
| 1334 |
border.Attr = Dim |
| 1335 |
} |
| 1336 |
theme.Border = o(baseTheme.Border, border) |
| 1337 |
theme.BorderLabel = o(baseTheme.BorderLabel, theme.BorderLabel) |
| 1338 |
|
| 1339 |
undefined := NewColorAttr() |
| 1340 |
scrollbarDefined := theme.Scrollbar != undefined |
| 1341 |
previewBorderDefined := theme.PreviewBorder != undefined |
| 1342 |
|
| 1343 |
theme.NthSelectedAttr = theme.SelectedFg.Attr |
| 1344 |
theme.SelectedFg = theme.ListFg.Merge(theme.SelectedFg) |
| 1345 |
theme.SelectedBg = o(theme.ListBg, theme.SelectedBg) |
| 1346 |
theme.SelectedMatch = o(theme.Match, theme.SelectedMatch) |
| 1347 |
|
| 1348 |
ghost := theme.Ghost |
| 1349 |
if ghost.IsUndefined() { |
| 1350 |
ghost.Attr = Dim |
| 1351 |
} else if ghost.IsColorDefined() && !ghost.IsAttrDefined() { |
| 1352 |
// Don't want to inherit 'bold' from 'input' |
| 1353 |
ghost.Attr = AttrRegular |
| 1354 |
} |
| 1355 |
theme.Ghost = o(theme.Input, ghost) |
| 1356 |
theme.Disabled = o(theme.Input, theme.Disabled) |
| 1357 |
|
| 1358 |
// Use dim gutter on non-colored themes if undefined |
| 1359 |
gutter := theme.Gutter |
| 1360 |
if !baseTheme.Colored && gutter.IsUndefined() { |
| 1361 |
gutter.Attr = Dim |
| 1362 |
} |
| 1363 |
theme.Gutter = o(theme.DarkBg, gutter) |
| 1364 |
theme.AltGutter = o(theme.Gutter, theme.AltGutter) |
| 1365 |
theme.PreviewFg = o(theme.Fg, theme.PreviewFg) |
| 1366 |
theme.PreviewBg = o(theme.Bg, theme.PreviewBg) |
| 1367 |
theme.PreviewLabel = o(theme.BorderLabel, theme.PreviewLabel) |
| 1368 |
theme.PreviewBorder = o(theme.Border, theme.PreviewBorder) |
| 1369 |
theme.ListLabel = o(theme.BorderLabel, theme.ListLabel) |
| 1370 |
theme.ListBorder = o(theme.Border, theme.ListBorder) |
| 1371 |
theme.Separator = o(theme.ListBorder, theme.Separator) |
| 1372 |
theme.Scrollbar = o(theme.ListBorder, theme.Scrollbar) |
| 1373 |
theme.GapLine = o(theme.ListBorder, theme.GapLine) |
| 1374 |
/* |
| 1375 |
--color list-border:green |
| 1376 |
--color scrollbar:red |
| 1377 |
--color scrollbar:red,list-border:green |
| 1378 |
--color scrollbar:red,preview-border:green |
| 1379 |
*/ |
| 1380 |
if scrollbarDefined && !previewBorderDefined { |
| 1381 |
theme.PreviewScrollbar = o(theme.Scrollbar, theme.PreviewScrollbar) |
| 1382 |
} else { |
| 1383 |
theme.PreviewScrollbar = o(theme.PreviewBorder, theme.PreviewScrollbar) |
| 1384 |
} |
| 1385 |
if hasInputWindow { |
| 1386 |
theme.InputBg = o(theme.Bg, theme.InputBg) |
| 1387 |
} else { |
| 1388 |
// We shouldn't use input-bg if there's no separate input window |
| 1389 |
// e.g. fzf --color 'list-bg:green,input-bg:red' --no-input-border |
| 1390 |
theme.InputBg = o(theme.Bg, theme.ListBg) |
| 1391 |
} |
| 1392 |
theme.InputBorder = o(theme.Border, theme.InputBorder) |
| 1393 |
theme.InputLabel = o(theme.BorderLabel, theme.InputLabel) |
| 1394 |
if hasHeaderWindow { |
| 1395 |
theme.HeaderBg = o(theme.Bg, theme.HeaderBg) |
| 1396 |
} else { |
| 1397 |
theme.HeaderBg = o(theme.Bg, theme.ListBg) |
| 1398 |
} |
| 1399 |
// Inline header/footer borders sit inside the list frame, so default their color |
| 1400 |
// to the list-border color when the user has not explicitly set it. The inline |
| 1401 |
// separator then matches the surrounding frame. |
| 1402 |
headerBorderFallback := theme.Border |
| 1403 |
if headerInline { |
| 1404 |
headerBorderFallback = theme.ListBorder |
| 1405 |
} |
| 1406 |
theme.HeaderBorder = o(headerBorderFallback, theme.HeaderBorder) |
| 1407 |
theme.HeaderLabel = o(theme.BorderLabel, theme.HeaderLabel) |
| 1408 |
|
| 1409 |
theme.FooterBg = o(theme.Bg, theme.FooterBg) |
| 1410 |
footerBorderFallback := theme.Border |
| 1411 |
if footerInline { |
| 1412 |
footerBorderFallback = theme.ListBorder |
| 1413 |
} |
| 1414 |
theme.FooterBorder = o(footerBorderFallback, theme.FooterBorder) |
| 1415 |
theme.FooterLabel = o(theme.BorderLabel, theme.FooterLabel) |
| 1416 |
|
| 1417 |
if theme.Nomatch.IsUndefined() { |
| 1418 |
theme.Nomatch.Attr = Dim |
| 1419 |
} |
| 1420 |
|
| 1421 |
initPalette(theme) |
| 1422 |
} |
| 1423 |
|
| 1424 |
func initPalette(theme *ColorTheme) { |
| 1425 |
pair := func(fg, bg ColorAttr) ColorPair { |
| 1426 |
if fg.Color == colDefault && (fg.Attr&Reverse) > 0 { |
| 1427 |
bg.Color = colDefault |
| 1428 |
} |
| 1429 |
return ColorPair{fg.Color, bg.Color, colDefault, fg.Attr} |
| 1430 |
} |
| 1431 |
blank := theme.ListFg |
| 1432 |
blank.Attr = AttrRegular |
| 1433 |
|
| 1434 |
ColPrompt = pair(theme.Prompt, theme.InputBg) |
| 1435 |
ColNormal = pair(theme.ListFg, theme.ListBg) |
| 1436 |
ColSelected = pair(theme.SelectedFg, theme.SelectedBg) |
| 1437 |
ColInput = pair(theme.Input, theme.InputBg) |
| 1438 |
ColGhost = pair(theme.Ghost, theme.InputBg) |
| 1439 |
ColDisabled = pair(theme.Disabled, theme.InputBg) |
| 1440 |
ColMatch = pair(theme.Match, theme.ListBg) |
| 1441 |
ColSelectedMatch = pair(theme.SelectedMatch, theme.SelectedBg) |
| 1442 |
ColPointer = pair(theme.Pointer, theme.Gutter) |
| 1443 |
ColPointerEmpty = pair(blank, theme.Gutter) |
| 1444 |
ColPointerEmptyChar = pair(theme.Gutter, theme.ListBg) |
| 1445 |
ColAltPointerEmpty = pair(blank, theme.AltGutter) |
| 1446 |
ColAltPointerEmptyChar = pair(theme.AltGutter, theme.ListBg) |
| 1447 |
if theme.SelectedBg.Color != theme.ListBg.Color { |
| 1448 |
ColMarker = pair(theme.Marker, theme.SelectedBg) |
| 1449 |
} else { |
| 1450 |
ColMarker = pair(theme.Marker, theme.ListBg) |
| 1451 |
} |
| 1452 |
ColCurrent = pair(theme.Current, theme.DarkBg) |
| 1453 |
ColCurrentMatch = pair(theme.CurrentMatch, theme.DarkBg) |
| 1454 |
ColCurrentPointer = pair(theme.Pointer, theme.DarkBg) |
| 1455 |
ColCurrentPointerEmpty = pair(blank, theme.DarkBg) |
| 1456 |
ColCurrentMarker = pair(theme.Marker, theme.DarkBg) |
| 1457 |
ColCurrentSelectedEmpty = pair(blank, theme.DarkBg) |
| 1458 |
ColSpinner = pair(theme.Spinner, theme.InputBg) |
| 1459 |
ColInfo = pair(theme.Info, theme.InputBg) |
| 1460 |
ColSeparator = pair(theme.Separator, theme.InputBg) |
| 1461 |
ColScrollbar = pair(theme.Scrollbar, theme.ListBg) |
| 1462 |
ColGapLine = pair(theme.GapLine, theme.ListBg) |
| 1463 |
ColBorder = pair(theme.Border, theme.Bg) |
| 1464 |
ColBorderLabel = pair(theme.BorderLabel, theme.Bg) |
| 1465 |
ColPreviewLabel = pair(theme.PreviewLabel, theme.PreviewBg) |
| 1466 |
ColPreview = pair(theme.PreviewFg, theme.PreviewBg) |
| 1467 |
ColPreviewBorder = pair(theme.PreviewBorder, theme.PreviewBg) |
| 1468 |
ColPreviewScrollbar = pair(theme.PreviewScrollbar, theme.PreviewBg) |
| 1469 |
ColPreviewSpinner = pair(theme.Spinner, theme.PreviewBg) |
| 1470 |
ColListLabel = pair(theme.ListLabel, theme.ListBg) |
| 1471 |
ColListBorder = pair(theme.ListBorder, theme.ListBg) |
| 1472 |
ColInputBorder = pair(theme.InputBorder, theme.InputBg) |
| 1473 |
ColInputLabel = pair(theme.InputLabel, theme.InputBg) |
| 1474 |
ColHeader = pair(theme.Header, theme.HeaderBg) |
| 1475 |
ColHeaderBorder = pair(theme.HeaderBorder, theme.HeaderBg) |
| 1476 |
ColHeaderLabel = pair(theme.HeaderLabel, theme.HeaderBg) |
| 1477 |
ColFooter = pair(theme.Footer, theme.FooterBg) |
| 1478 |
ColFooterBorder = pair(theme.FooterBorder, theme.FooterBg) |
| 1479 |
ColFooterLabel = pair(theme.FooterLabel, theme.FooterBg) |
| 1480 |
} |
| 1481 |
|
| 1482 |
func runeWidth(r rune) int { |
| 1483 |
return uniseg.StringWidth(string(r)) |
| 1484 |
} |
| 1485 |
|
| 1486 |
// WrappedLine represents a single visual line after character-level wrapping. |
| 1487 |
type WrappedLine struct { |
| 1488 |
Text string |
| 1489 |
DisplayWidth int |
| 1490 |
} |
| 1491 |
|
| 1492 |
// WrapLine splits a single line (no embedded \n) into visual lines |
| 1493 |
// that fit within initialMax columns. Character-level wrapping only. |
| 1494 |
func WrapLine(input string, prefixLength int, initialMax int, tabstop int, wrapSignWidth int) []WrappedLine { |
| 1495 |
lines := []WrappedLine{} |
| 1496 |
width := 0 |
| 1497 |
line := "" |
| 1498 |
gr := uniseg.NewGraphemes(input) |
| 1499 |
maxWidth := initialMax |
| 1500 |
contMax := max(1, initialMax-wrapSignWidth) |
| 1501 |
for gr.Next() { |
| 1502 |
rs := gr.Runes() |
| 1503 |
str := string(rs) |
| 1504 |
var w int |
| 1505 |
if len(rs) == 1 && rs[0] == '\t' { |
| 1506 |
w = tabstop - (prefixLength+width)%tabstop |
| 1507 |
str = strings.Repeat(" ", w) |
| 1508 |
} else if rs[0] == '\r' { |
| 1509 |
w++ |
| 1510 |
} else { |
| 1511 |
w = uniseg.StringWidth(str) |
| 1512 |
} |
| 1513 |
width += w |
| 1514 |
|
| 1515 |
if prefixLength+width <= maxWidth { |
| 1516 |
line += str |
| 1517 |
} else { |
| 1518 |
lines = append(lines, WrappedLine{string(line), width - w}) |
| 1519 |
line = str |
| 1520 |
prefixLength = 0 |
| 1521 |
width = w |
| 1522 |
maxWidth = contMax |
| 1523 |
} |
| 1524 |
} |
| 1525 |
lines = append(lines, WrappedLine{string(line), width}) |
| 1526 |
return lines |
| 1527 |
} |