1// termbox is a library for creating cross-platform text-based interfaces
  2package termbox
  3
  4// public API, common OS agnostic part
  5
  6type (
  7	InputMode  int
  8	OutputMode int
  9	EventType  uint8
 10	Modifier   uint8
 11	Key        uint16
 12	Attribute  uint64
 13)
 14
 15// This type represents a termbox event. The 'Mod', 'Key' and 'Ch' fields are
 16// valid if 'Type' is EventKey. The 'Width' and 'Height' fields are valid if
 17// 'Type' is EventResize. The 'Err' field is valid if 'Type' is EventError.
 18type Event struct {
 19	Type   EventType // one of Event* constants
 20	Mod    Modifier  // one of Mod* constants or 0
 21	Key    Key       // one of Key* constants, invalid if 'Ch' is not 0
 22	Ch     rune      // a unicode character
 23	Width  int       // width of the screen
 24	Height int       // height of the screen
 25	Err    error     // error in case if input failed
 26	MouseX int       // x coord of mouse
 27	MouseY int       // y coord of mouse
 28	N      int       // number of bytes written when getting a raw event
 29}
 30
 31// A cell, single conceptual entity on the screen. The screen is basically a 2d
 32// array of cells. 'Ch' is a unicode character, 'Fg' and 'Bg' are foreground
 33// and background attributes respectively.
 34type Cell struct {
 35	Ch rune
 36	Fg Attribute
 37	Bg Attribute
 38}
 39
 40// To know if termbox has been initialized or not
 41var (
 42	IsInit bool = false
 43)
 44
 45// Key constants, see Event.Key field.
 46const (
 47	KeyF1 Key = 0xFFFF - iota
 48	KeyF2
 49	KeyF3
 50	KeyF4
 51	KeyF5
 52	KeyF6
 53	KeyF7
 54	KeyF8
 55	KeyF9
 56	KeyF10
 57	KeyF11
 58	KeyF12
 59	KeyInsert
 60	KeyDelete
 61	KeyHome
 62	KeyEnd
 63	KeyPgup
 64	KeyPgdn
 65	KeyArrowUp
 66	KeyArrowDown
 67	KeyArrowLeft
 68	KeyArrowRight
 69	key_min // see terminfo
 70	MouseLeft
 71	MouseMiddle
 72	MouseRight
 73	MouseRelease
 74	MouseWheelUp
 75	MouseWheelDown
 76)
 77
 78const (
 79	KeyCtrlTilde      Key = 0x00
 80	KeyCtrl2          Key = 0x00
 81	KeyCtrlSpace      Key = 0x00
 82	KeyCtrlA          Key = 0x01
 83	KeyCtrlB          Key = 0x02
 84	KeyCtrlC          Key = 0x03
 85	KeyCtrlD          Key = 0x04
 86	KeyCtrlE          Key = 0x05
 87	KeyCtrlF          Key = 0x06
 88	KeyCtrlG          Key = 0x07
 89	KeyBackspace      Key = 0x08
 90	KeyCtrlH          Key = 0x08
 91	KeyTab            Key = 0x09
 92	KeyCtrlI          Key = 0x09
 93	KeyCtrlJ          Key = 0x0A
 94	KeyCtrlK          Key = 0x0B
 95	KeyCtrlL          Key = 0x0C
 96	KeyEnter          Key = 0x0D
 97	KeyCtrlM          Key = 0x0D
 98	KeyCtrlN          Key = 0x0E
 99	KeyCtrlO          Key = 0x0F
100	KeyCtrlP          Key = 0x10
101	KeyCtrlQ          Key = 0x11
102	KeyCtrlR          Key = 0x12
103	KeyCtrlS          Key = 0x13
104	KeyCtrlT          Key = 0x14
105	KeyCtrlU          Key = 0x15
106	KeyCtrlV          Key = 0x16
107	KeyCtrlW          Key = 0x17
108	KeyCtrlX          Key = 0x18
109	KeyCtrlY          Key = 0x19
110	KeyCtrlZ          Key = 0x1A
111	KeyEsc            Key = 0x1B
112	KeyCtrlLsqBracket Key = 0x1B
113	KeyCtrl3          Key = 0x1B
114	KeyCtrl4          Key = 0x1C
115	KeyCtrlBackslash  Key = 0x1C
116	KeyCtrl5          Key = 0x1D
117	KeyCtrlRsqBracket Key = 0x1D
118	KeyCtrl6          Key = 0x1E
119	KeyCtrl7          Key = 0x1F
120	KeyCtrlSlash      Key = 0x1F
121	KeyCtrlUnderscore Key = 0x1F
122	KeySpace          Key = 0x20
123	KeyBackspace2     Key = 0x7F
124	KeyCtrl8          Key = 0x7F
125)
126
127// Alt modifier constant, see Event.Mod field and SetInputMode function.
128const (
129	ModAlt Modifier = 1 << iota
130	ModMotion
131)
132
133// Cell colors, you can combine a color with multiple attributes using bitwise
134// OR ('|').
135const (
136	ColorDefault Attribute = iota
137	ColorBlack
138	ColorRed
139	ColorGreen
140	ColorYellow
141	ColorBlue
142	ColorMagenta
143	ColorCyan
144	ColorWhite
145	ColorDarkGray
146	ColorLightRed
147	ColorLightGreen
148	ColorLightYellow
149	ColorLightBlue
150	ColorLightMagenta
151	ColorLightCyan
152	ColorLightGray
153)
154
155// Cell attributes, it is possible to use multiple attributes by combining them
156// using bitwise OR ('|'). Although, colors cannot be combined. But you can
157// combine attributes and a single color.
158//
159// It's worth mentioning that some platforms don't support certain attributes.
160// For example windows console doesn't support AttrUnderline. And on some
161// terminals applying AttrBold to background may result in blinking text. Use
162// them with caution and test your code on various terminals.
163const (
164	AttrBold Attribute = 1 << (iota + 9)
165	AttrBlink
166	AttrHidden
167	AttrDim
168	AttrUnderline
169	AttrCursive
170	AttrReverse
171	max_attr
172)
173
174// Input mode. See SetInputMode function.
175const (
176	InputEsc InputMode = 1 << iota
177	InputAlt
178	InputMouse
179	InputCurrent InputMode = 0
180)
181
182// Output mode. See SetOutputMode function.
183const (
184	OutputCurrent OutputMode = iota
185	OutputNormal
186	Output256
187	Output216
188	OutputGrayscale
189	OutputRGB
190)
191
192// Event type. See Event.Type field.
193const (
194	EventKey EventType = iota
195	EventResize
196	EventMouse
197	EventError
198	EventInterrupt
199	EventRaw
200	EventNone
201)
202
203// AttributeToRGB converts an Attribute to the underlying rgb triplet.
204// This is only useful if termbox is in Full RGB mode and the specified
205// attribute is also an attribute with r, g, b specified
206func AttributeToRGB(attr Attribute) (uint8, uint8, uint8) {
207	var color uint64 = uint64(attr) / uint64(max_attr)
208	// Have to right-shift with the highest attribute bit.
209	// For this, we divide by max_attr
210	var b uint8 = uint8(color % 256)
211	var g uint8 = uint8(color >> 8 % 256)
212	var r uint8 = uint8(color >> 16 % 256)
213	return r, g, b
214}
215
216// RGBToAttribute is used to convert an rgb triplet into a termbox attribute.
217// This attribute can only be applied when termbox is in Full RGB mode,
218// otherwise it'll be ignored and no color will be drawn.
219// R, G, B have to be in the range of 0 and 255.
220func RGBToAttribute(r uint8, g uint8, b uint8) Attribute {
221	var color uint64 = uint64(b)
222	color += uint64(g) << 8
223	color += uint64(r) << 16
224	color += 1 << 25
225	color = color * uint64(max_attr)
226	// Left-shift back to the place where rgb is stored.
227	return Attribute(color)
228}