diff --git a/constants/constants.go b/constants/constants.go index 1fc6f74..6f83926 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -15,6 +15,9 @@ const AporiaStartxPath = "APORIA_STARTX_PATH" const AporiaExec = "APORIA_EXEC" const X11StartupCommand = "exec /bin/bash --login /etc/aporia/.scripts/startx.sh /etc/aporia/.scripts/xsetup.sh" +var ShutdownCommand = []string{"/bin/bash", "-c", "shutdown now"} +var RebootCommand = []string{"/bin/bash", "-c", "reboot"} + // Ascii art used when there is no config const DefaultAsciiArt = `` diff --git a/tui/draw.go b/tui/draw.go index 2cd0d27..7ac47ba 100644 --- a/tui/draw.go +++ b/tui/draw.go @@ -14,8 +14,7 @@ const trCorner = "┐" const blCorner = "└" const brCorner = "┘" -const boxHeight = 6 -const boxWidth = 30 +const boxWidth = 35 // Draw the background, which should only be drawn once. func (self *Tui) setupDraw() { @@ -23,56 +22,58 @@ func (self *Tui) setupDraw() { ansi.MoveCursor(0, 0) draw(self.asciiArt, self.TermSize) ansi.MoveCursor(0, 0) - self.drawBox() + self.drawTopLeftBox() + self.drawBox(0, self.TermSize.Cols - 14, 14, []string{"F11 Shutdown", "F12 Reboot"}) } func (self *Tui) draw() error { // Draw the message if self.lastDrawnMessage != self.message { self.lastDrawnMessage = self.message - ansi.MoveCursor(2, 0) - self.drawLine(self.message) + self.drawLine(self.message, 2, 0, boxWidth) } // Draw the currently selected field thisLine, cursorPos := self.fields[self.position].draw(boxWidth - 2) - ansi.MoveCursor(self.position+3, 0) - self.drawLine(thisLine) + self.drawLine(thisLine, self.position+3, 0, boxWidth) - ansi.MoveCursor(self.position+3, cursorPos + 1) + ansi.MoveCursor(self.position+3, cursorPos+1) return nil } -// Draw the vertical margin. -func drawMargin(height int) { - for i := 0; i < (height-boxHeight)/2; i++ { - fmt.Print("\n\r") - } -} - func eraseLine(num int) { fmt.Print("\033[", num, "K") } // Draw the box. Return the vertical lines taken up. -func (self Tui) drawBox() { - fmt.Print(tlCorner, strings.Repeat(horizontal, boxWidth-2), trCorner, "\n\r") - - self.drawLine(self.message) - +func (self Tui) drawTopLeftBox() { + lines := []string{""} for _, field := range self.fields { line, _ := field.draw(boxWidth - 2) - self.drawLine(line) + lines = append(lines, line) } - fmt.Print(blCorner, strings.Repeat(horizontal, boxWidth-2), brCorner, "\n\r") + self.drawBox(0, 0, boxWidth, lines) + } -func (self Tui) drawLine(text string) { +func (self Tui) drawBox(line int, col int, width int, text []string) { + ansi.MoveCursor(line, col) + fmt.Print(tlCorner, strings.Repeat(horizontal, width-2), trCorner) + + for i, line := range text { + self.drawLine(line, i+2, col, width) + } + + ansi.MoveCursor(len(text)+2+line, col) + fmt.Print(blCorner, strings.Repeat(horizontal, width-2), brCorner) +} +func (self Tui) drawLine(text string, line, col, width int) { + ansi.MoveCursor(line, col) fmt.Print(vertical) fmt.Print(text) - fmt.Print(strings.Repeat(" ", maxInt(boxWidth-2-len(text), 0))) - fmt.Print(vertical, "\n\r") + fmt.Print(strings.Repeat(" ", maxInt(width-2-len(text), 0))) + fmt.Print(vertical) } diff --git a/tui/read.go b/tui/read.go index 1ba3d88..cfe185f 100644 --- a/tui/read.go +++ b/tui/read.go @@ -44,6 +44,18 @@ func ReadTermChars() func() ([]int, error) { continue } + if reflect.DeepEqual(buff, []int{27, 91, 50}) { + continue + } + + if reflect.DeepEqual(buff, []int{27, 91, 50, 51}) { + continue + } + + if reflect.DeepEqual(buff, []int{27, 91, 50, 52}) { + continue + } + copy := buff buff = []int{} return copy, nil diff --git a/tui/tui.go b/tui/tui.go index 04d7e9a..5dda997 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -4,10 +4,12 @@ import ( "fmt" "math/rand" "os" + "os/exec" "reflect" "aporia/ansi" "aporia/config" + "aporia/constants" "aporia/login" "golang.org/x/term" @@ -156,6 +158,18 @@ func (self *Tui) handleInput(symbol []int) { return } + // F11 + if reflect.DeepEqual(symbol, []int{27, 91, 50, 51, 126}) { + exec.Command(constants.ShutdownCommand[0], constants.ShutdownCommand[1:]...).Run() + return + } + + // F12 + if reflect.DeepEqual(symbol, []int{27, 91, 50, 52, 126}) { + exec.Command(constants.RebootCommand[0], constants.RebootCommand[1:]...).Run() + return + } + // Control + C if reflect.DeepEqual(symbol, []int{3}) { os.Exit(1)