Skip to content

Commit

Permalink
Add shutdown and reboot button
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-arrow committed Dec 26, 2023
1 parent 0eced02 commit 810d5a7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
3 changes: 3 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ``

Expand Down
51 changes: 26 additions & 25 deletions tui/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,66 @@ 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() {
ansi.Clear()
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)
}
12 changes: 12 additions & 0 deletions tui/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"math/rand"
"os"
"os/exec"
"reflect"

"aporia/ansi"
"aporia/config"
"aporia/constants"
"aporia/login"

"golang.org/x/term"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 810d5a7

Please sign in to comment.