tui

package
v0.0.0-...-1459c67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Colors = ColorScheme{
	Border:             tcell.GetColor("#333333"),
	Highlight:          tcell.GetColor("#3f7f3f"),
	SelectedBackground: tcell.GetColor("#333333"),
	SelectedFg:         tcell.GetColor("#f0f0f0"),
	SelectedHighlight:  tcell.GetColor("#3f9f3f"),
}

Colors is the current color scheme

View Source
var FeatureRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		f := e.(*beholder.ClassFeature)

		return []string{
			"{classes}", strings.Join(f.Classes, ", "),
		}
	},

	template: `
[::bu]{name}[-:-:-]
[::d]{classes}[-:-:-]

{text}
`,
}

FeatureRenderer can render a ClassFeature

View Source
var ItemRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		i := e.(beholder.Item)

		rs := []string{
			"{rarity}", i.Rarity,
		}

		value := i.Value
		if value == "" && i.Magic > 0 && i.Rarity != "" {

			switch i.Rarity {
			case "Common":
				value = "50—100 gp"
			case "Uncommon":
				value = "101-500 gp"
			case "Rare":
				value = "501-5000 gp"
			case "Very Rare":
				value = "5001-50000 gp"
			case "Legendary":
				value = "50000+ gp"
			}
		}

		actualSeparator := ""
		if value != "" {
			rs = append(rs, "{value}", fmt.Sprintf(
				"[::d]%s[-:-:-]", value,
			))
		} else {
			rs = append(rs, "{value}", "")
		}

		if i.Magic > 0 {
			rs = append(rs, "{magic}", " (magic)")
			if value != "" {
				actualSeparator = valueSeparator
			}
		} else {
			rs = append(rs, "{magic}", "")
		}

		if i.Rarity != "" {
			if value != "" {
				actualSeparator = valueSeparator
			}

			rs = append(rs, "{text}", strings.Replace(
				strings.Join(i.GetText(), "\n"),
				fmt.Sprintf("Rarity: %s\n", i.Rarity),
				"",
				1,
			))
		}

		rs = append(rs, "{value-sep}", actualSeparator)

		return rs
	},

	template: `
[::bu]{name}[-:-:-]
[::d]{rarity}{magic}[-:-:-]{value-sep}{value}

{text}
`,
}

ItemRenderer can render an Item

View Source
var MonsterRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		m := e.(beholder.Monster)

		var immunities bytes.Buffer
		if m.DamageResistances != "" {
			immunities.WriteString("\n[::b]Damage Resistances[::-]: ")
			immunities.WriteString(m.DamageResistances)
		}
		if m.DamageVulnerabilities != "" {
			immunities.WriteString("\n[::b]Damage Vulnerabilities[::-]: ")
			immunities.WriteString(m.DamageVulnerabilities)
		}
		if m.DamageImmunities != "" {
			immunities.WriteString("\n[::b]Damage Immunities[::-]: ")
			immunities.WriteString(m.DamageImmunities)
		}
		if m.ConditionImmunities != "" {
			immunities.WriteString("\n[::b]Condition Immunities[::-]: ")
			immunities.WriteString(m.ConditionImmunities)
		}

		var savesRow = ""
		if m.SavingThrows != "" {
			savesRow = fmt.Sprintf("\n[::b]Saves[::-]: %s", m.SavingThrows)
		}

		var sensesRow = ""
		if m.Senses != "" {
			sensesRow = fmt.Sprintf("\n[::b]Senses[::-]: %s", m.Senses)
		}

		var skillsRow = ""
		if m.SkillModifiers != "" {
			skillsRow = fmt.Sprintf("\n[::b]Skills[::-]: %s", m.SkillModifiers)
		}

		var languagesRow = ""
		if m.Languages != "" {
			languagesRow = fmt.Sprintf("\n[::b]Languages[::-]: %s", m.Languages)
		}

		var allActions bytes.Buffer
		BuildTraits(&allActions, m.Actions)

		if m.Legendary != nil {
			if allActions.Len() > 0 {
				allActions.WriteString("\n")
			}

			allActions.WriteString("\n[::bu]Legendary Actions\n")
			BuildTraits(&allActions, m.Legendary)
		}

		statFormat := "%2d[::bd]%s[::-]"
		return []string{
			"{size}", sizes[m.Size],
			"{type}", m.Type,
			"{cr}", m.Challenge,
			"{immunities}", immunities.String(),
			"{actions}", allActions.String(),

			"{ac}", m.ArmorClass,
			"{hp}", m.HP,
			"{speed}", m.Speed,
			"{str}", fmt.Sprintf(statFormat, m.Str, formatModifier(m.Str)),
			"{dex}", fmt.Sprintf(statFormat, m.Dex, formatModifier(m.Dex)),
			"{con}", fmt.Sprintf(statFormat, m.Con, formatModifier(m.Con)),
			"{int}", fmt.Sprintf(statFormat, m.Int, formatModifier(m.Int)),
			"{wis}", fmt.Sprintf(statFormat, m.Wis, formatModifier(m.Wis)),
			"{cha}", fmt.Sprintf(statFormat, m.Cha, formatModifier(m.Cha)),
			"{passive}", strconv.Itoa(m.PassivePerception),
			"{saves-row}", savesRow,
			"{skills-row}", skillsRow,
			"{languages-row}", languagesRow,
			"{senses-row}", sensesRow,
		}
	},

	template: `
[::bu]{name}[-:-:-]
[::d]{size} {type}  CR {cr}[-:-:-]

[::b]AC[::-]: {ac}
[::b]HP[::-]: {hp}
[::b]Speed[::-]: {speed}
[::b]Passive Perception[::-]: {passive}

[::b] STR    DEX    CON    INT    WIS    CHA[::-]
[::-]{str}  {dex}  {con}  {int}  {wis}  {cha}[::-]
{immunities}{saves-row}{senses-row}{skills-row}{languages-row}

{traits}

{actions}
`,
}

MonsterRenderer can render an Monster

View Source
var ReferenceListRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		r := e.(*beholder.ReferenceList)

		var text bytes.Buffer
		lastCategory := ""
		hasAnyCategory := false

		for _, ref := range r.References {
			if categorized, ok := ref.(beholder.CategorizedEntity); ok {
				hasAnyCategory = true
				category := categorized.GetCategory()
				if category != lastCategory {
					lastCategory = category

					if text.Len() > 0 {
						text.WriteString("\n")
					}

					text.WriteString(`[::bu]`)
					text.WriteString(category)
					text.WriteString(`[-:-:-]`)
					text.WriteString(":\n\n")
				}
			}

			if hasAnyCategory {

				text.WriteString("  ")
			}

			text.WriteString(formatString(ref.GetName()))
			text.WriteString("\n")
		}

		return []string{
			"{text}", text.String(),
		}
	},

	template: `
[::bu]{name}[-:-:-]

{text}
`,
}

ReferenceListRenderer can render a ReferenceListEntity

View Source
var SpellRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		s := e.(beholder.Spell)

		var level string
		if s.Level >= 1 {
			level = strconv.Itoa(s.Level)
		} else {
			level = "Cantrip"
		}

		ritualTag := ""
		if s.Ritual != nil {
			ritualTag = " (Ritual)"
		}

		return []string{
			"{level}", level,
			"{school}", schools[s.School],
			"{cast-time}", s.Time,
			"{range}", s.Range,
			"{ritual?}", ritualTag,
			"{components}", s.Components,
			"{duration}", s.Duration,
			"{classes}", s.Classes,
		}
	},

	template: `
[::bu]{name}[-:-:-]
[::d]{level} {school}{ritual?}[-:-:-]

[::b]Casting Time[::-]: {cast-time}
[::b]Range[::-]: {range}
[::b]Components[::-]: {components}
[::b]Duration[::-]: {duration}
[::b]Classes[::-]: {classes}

{text}
`,
}

SpellRenderer can render a Spell

View Source
var TabSize = 4

TabSize is the number of spaces with which a tab character will be replaced.

View Source
var TraitRenderer = &EntityRenderer{
	replacements: func(e beholder.Entity) []string {
		t := e.(*beholder.RaceTrait)

		return []string{
			"{races}", strings.Join(t.Races, ", "),
		}
	},

	template: `
[::bu]{name}[-:-:-]
[::d]{races}[-:-:-]

{text}
`,
}

TraitRenderer can render a RaceTrait

Functions

func BuildTraits

func BuildTraits(traitsBuilder *bytes.Buffer, traits []*beholder.Trait)

BuildTraits .

func BuildTraitsString

func BuildTraitsString(traits []*beholder.Trait) string

BuildTraitsString is a convenience wrapper for BuildTraits when you have a single source of Traits and want a string

Types

type AsyncTextView

type AsyncTextView struct {
	sync.Mutex
	*tview.Box
	// contains filtered or unexported fields
}

AsyncTextView is based on tview.TextView. See package comments for more information, and the documentation for tview.TextView for usage.

func NewAsyncTextView

func NewAsyncTextView() *AsyncTextView

NewAsyncTextView returns a new text view.

func (*AsyncTextView) Clear

func (t *AsyncTextView) Clear() *AsyncTextView

Clear removes all text from the buffer.

func (*AsyncTextView) Draw

func (t *AsyncTextView) Draw(screen tcell.Screen)

Draw draws this primitive onto the screen.

func (*AsyncTextView) GetHighlights

func (t *AsyncTextView) GetHighlights() (regionIDs []string)

GetHighlights returns the IDs of all currently highlighted regions.

func (*AsyncTextView) GetRegionText

func (t *AsyncTextView) GetRegionText(regionID string) string

GetRegionText returns the text of the region with the given ID. If dynamic colors are enabled, color tags are stripped from the text. Newlines are always returned as '\n' runes.

If the region does not exist or if regions are turned off, an empty string is returned.

func (*AsyncTextView) Highlight

func (t *AsyncTextView) Highlight(regionIDs ...string) *AsyncTextView

Highlight specifies which regions should be highlighted. See class description for details on regions. Empty region strings are ignored.

Text in highlighted regions will be drawn inverted, i.e. with their background and foreground colors swapped.

Calling this function will remove any previous highlights. To remove all highlights, call this function without any arguments.

func (*AsyncTextView) InputHandler

func (t *AsyncTextView) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive))

InputHandler returns the handler for this primitive.

func (*AsyncTextView) ScrollPageBackward

func (t *AsyncTextView) ScrollPageBackward()

ScrollPageBackward scrolls backward a page

func (*AsyncTextView) ScrollPageForward

func (t *AsyncTextView) ScrollPageForward()

ScrollPageForward scrolls forward a page

func (*AsyncTextView) ScrollToBeginning

func (t *AsyncTextView) ScrollToBeginning() *AsyncTextView

ScrollToBeginning scrolls to the top left corner of the text if the text view is scrollable.

func (*AsyncTextView) ScrollToEnd

func (t *AsyncTextView) ScrollToEnd() *AsyncTextView

ScrollToEnd scrolls to the bottom left corner of the text if the text view is scrollable. Adding new rows to the end of the text view will cause it to scroll with the new data.

func (*AsyncTextView) ScrollToHighlight

func (t *AsyncTextView) ScrollToHighlight() *AsyncTextView

ScrollToHighlight will cause the visible area to be scrolled so that the highlighted regions appear in the visible area of the text view. This repositioning happens the next time the text view is drawn. It happens only once so you will need to call this function repeatedly to always keep highlighted regions in view.

Nothing happens if there are no highlighted regions or if the text view is not scrollable.

func (*AsyncTextView) SetChangedFunc

func (t *AsyncTextView) SetChangedFunc(handler func()) *AsyncTextView

SetChangedFunc sets a handler function which is called when the text of the text view has changed. This is typically used to cause the application to redraw the screen.

func (*AsyncTextView) SetDoneFunc

func (t *AsyncTextView) SetDoneFunc(handler func(key tcell.Key)) *AsyncTextView

SetDoneFunc sets a handler which is called when the user presses on the following keys: Escape, Enter, Tab, Backtab. The key is passed to the handler.

func (*AsyncTextView) SetDynamicColors

func (t *AsyncTextView) SetDynamicColors(dynamic bool) *AsyncTextView

SetDynamicColors sets the flag that allows the text color to be changed dynamically. See class description for details.

func (*AsyncTextView) SetRegions

func (t *AsyncTextView) SetRegions(regions bool) *AsyncTextView

SetRegions sets the flag that allows to define regions in the text. See class description for details.

func (*AsyncTextView) SetScrollable

func (t *AsyncTextView) SetScrollable(scrollable bool) *AsyncTextView

SetScrollable sets the flag that decides whether or not the text view is scrollable. If true, text is kept in a buffer and can be navigated.

func (*AsyncTextView) SetText

func (t *AsyncTextView) SetText(text string) *AsyncTextView

SetText sets the text of this text view to the provided string. Previously contained text will be removed.

func (*AsyncTextView) SetTextAlign

func (t *AsyncTextView) SetTextAlign(align int) *AsyncTextView

SetTextAlign sets the text alignment within the text view. This must be either AlignLeft, AlignCenter, or AlignRight.

func (*AsyncTextView) SetTextColor

func (t *AsyncTextView) SetTextColor(color tcell.Color) *AsyncTextView

SetTextColor sets the initial color of the text (which can be changed dynamically by sending color strings in square brackets to the text view if dynamic colors are enabled).

func (*AsyncTextView) SetWordWrap

func (t *AsyncTextView) SetWordWrap(wrapOnWords bool) *AsyncTextView

SetWordWrap sets the flag that, if true and if the "wrap" flag is also true (see SetWrap()), wraps the line at spaces or after punctuation marks. Note that trailing spaces will not be printed.

This flag is ignored if the "wrap" flag is false.

func (*AsyncTextView) SetWrap

func (t *AsyncTextView) SetWrap(wrap bool) *AsyncTextView

SetWrap sets the flag that, if true, leads to lines that are longer than the available width being wrapped onto the next line. If false, any characters beyond the available width are not displayed.

func (*AsyncTextView) Write

func (t *AsyncTextView) Write(p []byte) (n int, err error)

Write lets us implement the io.Writer interface. Tab characters will be replaced with TabSize space characters. A "\n" or "\r\n" will be interpreted as a new line.

type ColorScheme

type ColorScheme struct {
	Border             tcell.Color
	Highlight          tcell.Color
	SelectedBackground tcell.Color
	SelectedFg         tcell.Color
	SelectedHighlight  tcell.Color
}

ColorScheme is a struct containing... well, you can guess

type EntityList

type EntityList struct {
	*tview.Box
	// contains filtered or unexported fields
}

EntityList .

func NewList

func NewList() *EntityList

NewList returns a new form.

func (*EntityList) AddItem

func (l *EntityList) AddItem(entity beholder.SearchResult)

AddItem .

func (*EntityList) Clear

func (l *EntityList) Clear()

Clear all entities from this List

func (*EntityList) Draw

func (l *EntityList) Draw(screen tcell.Screen)

Draw .

func (*EntityList) GetCurrentEntity

func (l *EntityList) GetCurrentEntity() beholder.Entity

GetCurrentEntity returns the currently selected entity.

func (*EntityList) GetCurrentItem

func (l *EntityList) GetCurrentItem() int

GetCurrentItem returns the index of the currently selected list item.

func (*EntityList) GetItemCount

func (l *EntityList) GetItemCount() int

GetItemCount .

func (*EntityList) SetChangedFunc

func (l *EntityList) SetChangedFunc(changed func(entity beholder.Entity))

SetChangedFunc .

func (*EntityList) SetCurrentItem

func (l *EntityList) SetCurrentItem(item int)

SetCurrentItem sets the index of the currently selected list item.

func (*EntityList) SetEntities

func (l *EntityList) SetEntities(entities []beholder.SearchResult)

SetEntities sets the entities

type EntityRenderer

type EntityRenderer struct {
	// contains filtered or unexported fields
}

EntityRenderer can render an Entity type

func NewSimpleRenderer

func NewSimpleRenderer(label string) *EntityRenderer

NewSimpleRenderer will create a Renderer that can render anything that's just a name and text. The label will be rendered immediately after the name.

func (*EntityRenderer) Render

func (r *EntityRenderer) Render(entity beholder.Entity) string

Render the given entity to a TUI string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL