Elius94 / console-gui-tools

A simple library to draw option menu or other popup inputs and layout on Node.js console.
MIT License
123 stars 18 forks source link

[Bug]: escape keypressed doesn’t work properly #93

Closed 0xJWLabs closed 1 month ago

0xJWLabs commented 1 month ago

What happened?

So right now I’m making something that works like glow. So i have this two keybinds, left arrow and escape. When i pressed left arrow it makes my box visible again and still make my box keypress event works again. But if i press escape, it makes my table visible again, but the keypress stops working, even though both keys use the same function. Does anyone know why?

What should have happened?

escape key work like left arrow.

Code

onKeyBind() {
    this.console.on("keypressed", (key) => {
      switch (key.name) {
        case "q":
          this.close()
          break
        case "left":
        case "h":
        case "escape":
          if (this.isReading) {
            this.isReading = false
            this.table.visible = true
            this.table.focus()
            this.renderList()
          }
          break
        default:
          break
        }
      })

    this.table.on('keypress', (key: KeyListenerArgs) => {
      if (!this.table.isVisible()) return
      if (!this.table.focused) return
      switch (key.name) {
        case "up":
        case "k":
          if (this.data.index > 0) {
            this.data.index -= 1
            this.drawTable()
          }
          break
        case "down":
        case "j":
          if (this.data.index < this.data.files.length - 1) {
            this.data.index += 1
            this.drawTable()
          }
          break
        case "space":
        case "return":
        case "l":
          if (this.data.files[this.data.index]) {
            this.table.visible = false
            this.table.content.clear()
            this.table.update()
            this.isReading = true
            this.readMarkdown(this.data.files[this.data.index])
          }
          break
        default:
          break
      }
    })

    this.table.on("relativeMouse", (e: RelativeMouseEvent) => {
      if (e.name === "MOUSE_WHEEL_UP") {
        if (this.data.index > 0) {
          this.data.index -= 1
        }
      } else if (e.name === "MOUSE_WHEEL_DOWN") {
        if (this.data.index < this.data.files.length - 1) {
          this.data.index += 1
          this.drawTable()
        }
      } else if (e.name === "MOUSE_LEFT_BUTTON_PRESSED") {
        if (this.data.files.length <= this.table.absoluteValues.height) {
          this.data.index = e.data.y - 1
          this.drawTable()
          return
        }
        const realStartIndex = mapToRange(this.table.content.scrollIndex, this.data.files.length - this.table.absoluteValues.height, 0, 0, this.data.files.length - this.table.absoluteValues.height)
        this.data.index = e.data.y + realStartIndex - 1
        this.drawTable()
      }
    })
  }

Library Version

latest version

Node Version

20.16

What operating system are you using?

Windows

Terminal

Wezterm with WSL

Interest to fix the bug

0xJWLabs commented 1 month ago

Found the solution, just need to add setTimeout, does anyone know why?

Elius94 commented 1 month ago

Thanks for the issue 🙏 I'll check when I return home after summer holiday. Have a nice day

Elius94 commented 1 week ago

Found the solution, just need to add setTimeout, does anyone know why?

Where did you added setTimeout?