mirror of
https://github.com/EnumeratedDev/Typer.git
synced 2025-07-01 23:58:22 +00:00
Compare commits
4 Commits
e25916228c
...
8d24988057
Author | SHA1 | Date | |
---|---|---|---|
8d24988057 | |||
6cbe0e8ab2 | |||
f31650e67a | |||
a3138a9e86 |
@ -37,4 +37,7 @@ keybindings:
|
||||
command: "menu-edit"
|
||||
- keybinding: "F3"
|
||||
cursor_modes: ["buffer","dropdown"]
|
||||
command: "menu-buffers"
|
||||
command: "menu-buffers"
|
||||
- keybinding: "Ctrl-E"
|
||||
cursor_modes: ["buffer"]
|
||||
command: "execute"
|
105
src/command.go
105
src/command.go
@ -202,6 +202,66 @@ func initCommands() {
|
||||
},
|
||||
}
|
||||
|
||||
toggleTopBar := Command{
|
||||
cmd: "toggle-top-bar",
|
||||
run: func(window *Window, args ...string) {
|
||||
window.ShowTopMenu = !window.ShowTopMenu
|
||||
},
|
||||
}
|
||||
|
||||
toggleLineIndex := Command{
|
||||
cmd: "toggle-line-index",
|
||||
run: func(window *Window, args ...string) {
|
||||
window.ShowLineIndex = !window.ShowLineIndex
|
||||
},
|
||||
}
|
||||
|
||||
setStyleCmd := Command{
|
||||
cmd: "set-style",
|
||||
run: func(window *Window, args ...string) {
|
||||
if len(args) >= 1 {
|
||||
input := args[0]
|
||||
|
||||
if input == "" {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := AvailableStyles[input]; !ok {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
return
|
||||
}
|
||||
|
||||
if ok := SetCurrentStyle(window.screen, input); ok {
|
||||
PrintMessage(window, fmt.Sprintf("Setting style to '%s'", input))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
inputChannel := RequestInput(window, "Style to switch to:", "")
|
||||
go func() {
|
||||
input := <-inputChannel
|
||||
|
||||
if input == "" {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := AvailableStyles[input]; !ok {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
return
|
||||
}
|
||||
|
||||
if ok := SetCurrentStyle(window.screen, input); ok {
|
||||
PrintMessage(window, fmt.Sprintf("Setting style to '%s'", input))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("Could not set style to '%s'", input))
|
||||
}
|
||||
}()
|
||||
},
|
||||
}
|
||||
|
||||
menuFileCmd := Command{
|
||||
cmd: "menu-file",
|
||||
run: func(window *Window, args ...string) {
|
||||
@ -246,6 +306,47 @@ func initCommands() {
|
||||
},
|
||||
}
|
||||
|
||||
executeCmd := Command{
|
||||
cmd: "execute",
|
||||
run: func(window *Window, args ...string) {
|
||||
inputChannel := RequestInput(window, "Run:", "")
|
||||
|
||||
go func() {
|
||||
input := strings.TrimSpace(<-inputChannel)
|
||||
|
||||
if input == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var arguments []string
|
||||
|
||||
builder := &strings.Builder{}
|
||||
quoted := false
|
||||
for _, r := range input {
|
||||
if r == '"' {
|
||||
quoted = !quoted
|
||||
} else if !quoted && r == ' ' {
|
||||
arguments = append(arguments, builder.String())
|
||||
builder.Reset()
|
||||
} else {
|
||||
builder.WriteRune(r)
|
||||
}
|
||||
}
|
||||
if builder.Len() > 0 {
|
||||
arguments = append(arguments, builder.String())
|
||||
}
|
||||
|
||||
window.CursorMode = CursorModeBuffer
|
||||
|
||||
if len(arguments) == 1 {
|
||||
RunCommand(window, arguments[0])
|
||||
} else {
|
||||
RunCommand(window, arguments[0], arguments[1:]...)
|
||||
}
|
||||
}()
|
||||
},
|
||||
}
|
||||
|
||||
// Register commands
|
||||
commands["copy"] = ©Cmd
|
||||
commands["paste"] = &pasteCmd
|
||||
@ -256,10 +357,14 @@ func initCommands() {
|
||||
commands["next-buffer"] = &nextBufferCmd
|
||||
commands["new-buffer"] = &newBufferCmd
|
||||
commands["close-buffer"] = &closeBufferCmd
|
||||
commands["toggle-top-bar"] = &toggleTopBar
|
||||
commands["toggle-line-index"] = &toggleLineIndex
|
||||
commands["set-style"] = &setStyleCmd
|
||||
commands["menu-file"] = &menuFileCmd
|
||||
commands["menu-edit"] = &menuEditCmd
|
||||
commands["menu-buffers"] = &menuBuffersCmd
|
||||
commands["quit"] = &quitCmd
|
||||
commands["execute"] = &executeCmd
|
||||
}
|
||||
|
||||
func RunCommand(window *Window, cmd string, args ...string) bool {
|
||||
|
@ -38,9 +38,11 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
for window.screen != nil {
|
||||
for !window.closed {
|
||||
window.Draw()
|
||||
window.ProcessEvents()
|
||||
}
|
||||
|
||||
window.screen.Fini()
|
||||
window.screen = nil
|
||||
}
|
||||
|
54
src/style.go
54
src/style.go
@ -46,8 +46,29 @@ type typerStyleYaml struct {
|
||||
Colors map[string]string `yaml:"colors"`
|
||||
}
|
||||
|
||||
var FallbackStyle = TyperStyle{
|
||||
Name: "fallback",
|
||||
Description: "Fallback style",
|
||||
StyleType: "8-color",
|
||||
|
||||
BufferAreaBg: tcell.ColorBlack,
|
||||
BufferAreaFg: tcell.ColorWhite,
|
||||
BufferAreaSel: tcell.ColorNavy,
|
||||
TopMenuBg: tcell.ColorWhite,
|
||||
TopMenuFg: tcell.ColorBlack,
|
||||
DropdownBg: tcell.ColorWhite,
|
||||
DropdownFg: tcell.ColorBlack,
|
||||
DropdownSel: tcell.ColorNavy,
|
||||
LineIndexBg: tcell.ColorWhite,
|
||||
LineIndexFg: tcell.ColorBlack,
|
||||
MessageBarBg: tcell.ColorWhite,
|
||||
MessageBarFg: tcell.ColorBlack,
|
||||
InputBarBg: tcell.ColorWhite,
|
||||
InputBarFg: tcell.ColorBlack,
|
||||
}
|
||||
|
||||
var AvailableStyles = make(map[string]TyperStyle)
|
||||
var CurrentStyle TyperStyle
|
||||
var CurrentStyle = FallbackStyle
|
||||
|
||||
func readStyles() {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
@ -145,7 +166,7 @@ func readStyleYamlFile(filepath string) (TyperStyle, error) {
|
||||
return style, nil
|
||||
}
|
||||
|
||||
func SetCurrentStyle(screen tcell.Screen) {
|
||||
func SetCurrentStyle(screen tcell.Screen, styleName string) bool {
|
||||
availableTypes := make([]string, 1)
|
||||
availableTypes[0] = "8-color"
|
||||
if screen.Colors() >= 16 {
|
||||
@ -158,30 +179,13 @@ func SetCurrentStyle(screen tcell.Screen) {
|
||||
availableTypes = append(availableTypes, "true-color")
|
||||
}
|
||||
|
||||
if style, ok := AvailableStyles[Config.SelectedStyle]; ok && slices.Index(availableTypes, style.StyleType) != -1 {
|
||||
if style, ok := AvailableStyles[styleName]; ok && slices.Index(availableTypes, style.StyleType) != -1 {
|
||||
CurrentStyle = style
|
||||
} else if style, ok := AvailableStyles[Config.FallbackStyle]; ok {
|
||||
CurrentStyle = style
|
||||
} else {
|
||||
CurrentStyle = TyperStyle{
|
||||
Name: "fallback",
|
||||
Description: "Fallback style",
|
||||
StyleType: "8-color",
|
||||
|
||||
BufferAreaBg: tcell.ColorBlack,
|
||||
BufferAreaFg: tcell.ColorWhite,
|
||||
BufferAreaSel: tcell.ColorNavy,
|
||||
TopMenuBg: tcell.ColorWhite,
|
||||
TopMenuFg: tcell.ColorBlack,
|
||||
DropdownBg: tcell.ColorWhite,
|
||||
DropdownFg: tcell.ColorBlack,
|
||||
DropdownSel: tcell.ColorNavy,
|
||||
LineIndexBg: tcell.ColorWhite,
|
||||
LineIndexFg: tcell.ColorBlack,
|
||||
MessageBarBg: tcell.ColorWhite,
|
||||
MessageBarFg: tcell.ColorBlack,
|
||||
InputBarBg: tcell.ColorWhite,
|
||||
InputBarFg: tcell.ColorBlack,
|
||||
}
|
||||
screen.SetStyle(tcell.StyleDefault.Foreground(CurrentStyle.BufferAreaFg).Background(CurrentStyle.BufferAreaBg))
|
||||
screen.Sync()
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -123,7 +123,9 @@ func drawTopMenu(window *Window) {
|
||||
|
||||
// Draw buffer info
|
||||
bufferInfoMsg := getBufferInfoMsg(window)
|
||||
drawText(screen, sizeX-len(bufferInfoMsg)-1, 0, sizeX-1, 0, topMenuStyle, bufferInfoMsg)
|
||||
if sizeX-len(bufferInfoMsg)-1 > currentX+2 {
|
||||
drawText(screen, sizeX-len(bufferInfoMsg)-1, 0, sizeX-1, 0, topMenuStyle, bufferInfoMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func getBufferInfoMsg(window *Window) string {
|
||||
|
@ -36,6 +36,8 @@ type Window struct {
|
||||
CurrentBuffer *Buffer
|
||||
|
||||
screen tcell.Screen
|
||||
|
||||
closed bool
|
||||
}
|
||||
|
||||
var mouseHeld = false
|
||||
@ -70,16 +72,22 @@ func CreateWindow() (*Window, error) {
|
||||
log.Fatalf("Failed to initialize screen: %s", err)
|
||||
}
|
||||
|
||||
// Set screen style
|
||||
SetCurrentStyle(screen)
|
||||
screen.SetStyle(tcell.StyleDefault.Foreground(CurrentStyle.BufferAreaFg).Background(CurrentStyle.BufferAreaBg))
|
||||
|
||||
// Enable mouse
|
||||
screen.EnableMouse()
|
||||
|
||||
// Set window screen field
|
||||
window.screen = screen
|
||||
|
||||
// Try to set screen style to selected one
|
||||
if ok := SetCurrentStyle(screen, Config.SelectedStyle); !ok {
|
||||
// Try to set screen style to selected fallback one
|
||||
if ok := SetCurrentStyle(screen, Config.FallbackStyle); !ok {
|
||||
// Use hard-coded fallback style
|
||||
screen.SetStyle(tcell.StyleDefault.Foreground(CurrentStyle.BufferAreaFg).Background(CurrentStyle.BufferAreaBg))
|
||||
PrintMessage(&window, "Could not set style either to selected one nor to fallback one!")
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize top menu
|
||||
initTopMenu()
|
||||
|
||||
@ -637,8 +645,11 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
|
||||
}
|
||||
|
||||
func (window *Window) Close() {
|
||||
window.screen.Fini()
|
||||
window.screen = nil
|
||||
window.closed = true
|
||||
err := window.screen.PostEvent(tcell.NewEventInterrupt(nil))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) GetTextAreaDimensions() (int, int, int, int) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user