Skip to content

Commit

Permalink
Options Popup doesn't manage the list overflow!
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
Elius94 committed Apr 12, 2022
1 parent d190e6a commit 2e193fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/tcp_simulator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let period = 100

let mode = 'random'

const periodList = [10, 100, 250, 500, 1000, 2000, 5000, 10000]
const periodList = [10, 100, 250, 500, 1000, 2000, 5000, 10000, 20000, 30000, 60000, 120000, 300000, 600000, 900000, 1800000, 3600000]
const modeList = ["random", "linear"]

const clientManager = new EventEmitter()
Expand Down
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ class OptionPopup extends EventEmitter {
this.options = options
this.selected = selected
this.visible = visible
this.marginTop = 4
this.startIndex = 0
if (this.CM.widgetsCollection[this.id]) {
this.CM.unRegisterWidget(this)
const message = `OptionPopup ${this.id} already exists.`
Expand All @@ -270,13 +272,27 @@ class OptionPopup extends EventEmitter {
this.CM.registerWiget(this)
}

adaptOptions() {
return this.options.slice(this.startIndex, this.startIndex + this.CM.Terminal.rows - this.marginTop - 4)
}

keyListner(str, key) {
switch (key.name) {
case 'down':
this.setSelected(this.options[(this.options.indexOf(this.selected) + 1) % this.options.length])
if (this.CM.Terminal.rows - this.marginTop - 4 < this.options.length) {
if (this.selected === this.options[this.adaptOptions().length + this.startIndex]) {
this.startIndex++
}
} else {
this.startIndex = 0
}
break
case 'up':
this.setSelected(this.options[(this.options.indexOf(this.selected) - 1 + this.options.length) % this.options.length])
if (this.startIndex > 0 && this.selected === this.adaptOptions()[0]) {
this.startIndex--
}
break
case 'return':
{
Expand Down Expand Up @@ -373,13 +389,13 @@ class OptionPopup extends EventEmitter {
footer += "┘\n"

let content = ""
this.options.forEach((option, index) => {
this.adaptOptions().forEach((option, index) => {
content += `│${option === this.selected ? "<" : " "} ${option}${option === this.selected ? " >" : " "}${" ".repeat(windowWidth - option.toString().length - 4)}│\n`
})

const windowDesign = `${header}${content}${footer}`
windowDesign.split('\n').forEach((line, index) => {
this.CM.Terminal.cursorTo(Math.round((this.CM.Terminal.columns / 2) - (windowWidth / 2)), 4 + index)
this.CM.Terminal.cursorTo(Math.round((this.CM.Terminal.columns / 2) - (windowWidth / 2)), this.marginTop + index)
this.CM.Terminal.write(line)
})
return this
Expand Down

0 comments on commit 2e193fd

Please sign in to comment.