Starting work on the authDialog needed for Elevate

This commit is contained in:
HikariKnight 2023-04-10 16:28:12 +02:00
parent 798427280b
commit 7eb7e2fa1c
3 changed files with 85 additions and 24 deletions

View file

@ -6,31 +6,53 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
// Setup keybindings
switch msg.String() {
case "ctrl+c", "q":
// Exit when user presses Q or CTRL+C
return m, tea.Quit
case "enter": // If we are not done
if m.width != 0 { if m.focused != DONE {
// Process the selected item, if the return value is true then exit the application // Setup keybindings
if m.processSelection() { switch msg.String() {
case "ctrl+c", "q":
// Exit when user presses Q or CTRL+C
return m, tea.Quit
case "enter":
if m.width != 0 {
// Process the selected item, if the return value is true then exit the application
if m.processSelection() {
return m, tea.Quit
}
}
case "ctrl+z", "backspace":
// Go backwards in the model
if m.focused > 0 && m.focused != DONE {
m.focused--
return m, nil
} else if m.focused == DONE {
// Since we have no QuickEmu support, skip the usb controller configuration
m.focused = VIDEO
} else {
// If we are at the beginning, just exit
return m, tea.Quit return m, tea.Quit
} }
} }
case "ctrl+z", "backspace": } else {
// Go backwards in the model // If we are done then handle keybindings a bit differently
if m.focused > 0 && m.focused != DONE { // Setup keybindings for authDialog
m.focused-- switch msg.String() {
return m, nil case "ctrl+c":
} else if m.focused == DONE { // Exit when user presses CTRL+C
// Since we have no QuickEmu support, skip the usb controller configuration
m.focused = VIDEO
} else {
// If we are at the beginning, just exit
return m, tea.Quit return m, tea.Quit
case "enter":
if m.width != 0 {
// Process the selected item, if the return value is true then exit the application
if m.processSelection() {
return m, tea.Quit
}
}
} }
m.authDialog, cmd = m.authDialog.Update(msg)
return m, cmd
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
if m.width == 0 { if m.width == 0 {
@ -52,12 +74,14 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Update the styles with the correct width // Update the styles with the correct width
dialogStyle = dialogStyle.Width(m.width) dialogStyle = dialogStyle.Width(m.width)
listStyle = listStyle.Width(m.width) listStyle = listStyle.Width(m.width)
titleStyle = titleStyle.Width(m.width - 2) titleStyle = titleStyle.Width(m.width - 4)
choiceStyle = choiceStyle.Width(m.width) choiceStyle = choiceStyle.Width(m.width)
} }
} }
} }
} }
// Run another update loop
m.lists[m.focused], cmd = m.lists[m.focused].Update(msg) m.lists[m.focused], cmd = m.lists[m.focused].Update(msg)
return m, cmd return m, cmd
} }

View file

@ -12,6 +12,7 @@ import (
func (m model) View() string { func (m model) View() string {
if m.width != 0 { if m.width != 0 {
title := "" title := ""
view := "Empty View :("
switch m.focused { switch m.focused {
case INTRO: case INTRO:
title = dialogStyle.Render( title = dialogStyle.Render(
@ -31,11 +32,16 @@ func (m model) View() string {
"becomes unbootable, as you will be asked to verify the files generated", "becomes unbootable, as you will be asked to verify the files generated",
), ),
) )
view = listStyle.Render(m.lists[m.focused].View())
case GPUS: case GPUS:
title = titleStyle.MarginLeft(2).Render( title = titleStyle.MarginLeft(2).Render(
"Select a GPU to check the IOMMU groups of", "Select a GPU to check the IOMMU groups of",
) )
view = listStyle.Render(m.lists[m.focused].View())
case GPU_GROUP: case GPU_GROUP:
title = titleStyle.Render( title = titleStyle.Render(
fmt.Sprint( fmt.Sprint(
@ -44,11 +50,15 @@ func (m model) View() string {
), ),
) )
view = listStyle.Render(m.lists[m.focused].View())
case USB: case USB:
title = titleStyle.Render( title = titleStyle.Render(
"[OPTIONAL]: Select a USB Controller to check the IOMMU groups of", "[OPTIONAL]: Select a USB Controller to check the IOMMU groups of",
) )
view = listStyle.Render(m.lists[m.focused].View())
case USB_GROUP: case USB_GROUP:
title = titleStyle.Render( title = titleStyle.Render(
fmt.Sprint( fmt.Sprint(
@ -57,6 +67,8 @@ func (m model) View() string {
), ),
) )
view = listStyle.Render(m.lists[m.focused].View())
case VBIOS: case VBIOS:
// Get the program directory // Get the program directory
exe, _ := os.Executable() exe, _ := os.Executable()
@ -89,6 +101,8 @@ func (m model) View() string {
title = fmt.Sprintf(text, m.vbios_path, scriptdir) title = fmt.Sprintf(text, m.vbios_path, scriptdir)
view = listStyle.Render(m.lists[m.focused].View())
case VIDEO: case VIDEO:
title = dialogStyle.Render( title = dialogStyle.Render(
fmt.Sprint( fmt.Sprint(
@ -99,13 +113,15 @@ func (m model) View() string {
), ),
) )
view = listStyle.Render(m.lists[m.focused].View())
case DONE: case DONE:
title = dialogStyle.Render( title = dialogStyle.Render(
fmt.Sprint( fmt.Sprint(
"The configuration files have been generated and are\n", "The configuration files have been generated and are\n",
"located inside the \"config\" folder\n", "located inside the \"config\" folder\n",
"\n", "\n",
"* The \"cmdline\" file contains kernel arguments that your bootloader needs\n", "* The \"kernel_args\" file contains kernel arguments that your bootloader needs\n",
"* The \"quickemu\" folder contains files that might be\n useable for quickemu in the future\n", "* The \"quickemu\" folder contains files that might be\n useable for quickemu in the future\n",
"* The files inside the \"etc\" folder must be copied to your system.\n", "* The files inside the \"etc\" folder must be copied to your system.\n",
" NOTE: Verify that these files are correctly formated/edited!\n", " NOTE: Verify that these files are correctly formated/edited!\n",
@ -114,9 +130,11 @@ func (m model) View() string {
"run it to copy the files to your system and make a backup of your old files.", "run it to copy the files to your system and make a backup of your old files.",
), ),
) )
view = m.authDialog.View()
} }
//return listStyle.SetString(fmt.Sprintf("%s\n\n", title)).Render(m.lists[m.focused].View()) //return listStyle.SetString(fmt.Sprintf("%s\n\n", title)).Render(m.lists[m.focused].View())
return lipgloss.JoinVertical(lipgloss.Left, fmt.Sprintf("%s\n%s\n", title, listStyle.Render(m.lists[m.focused].View()))) return lipgloss.JoinVertical(lipgloss.Left, fmt.Sprintf("%s\n%s\n", title, view))
} else { } else {
return "Loading..." return "Loading..."
} }

View file

@ -1,7 +1,12 @@
package internal package internal
import ( import (
"fmt"
"os/user"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
@ -29,6 +34,7 @@ type model struct {
offsety []int offsety []int
width int width int
height int height int
authDialog textinput.Model
} }
// Consts used to navigate the main model // Consts used to navigate the main model
@ -44,8 +50,21 @@ const (
) )
func NewModel() *model { func NewModel() *model {
// Get the username
user, err := user.Current()
errorcheck.ErrorCheck(err, "Error getting username")
username := user.Username
// Create the auth input and focus it
authInput := textinput.New()
authInput.EchoMode = textinput.EchoPassword
authInput.Prompt = fmt.Sprintf("\n[sudo] password for %s: ", username)
authInput.Focus()
// Create a blank model and return it // Create a blank model and return it
return &model{} return &model{
authDialog: authInput,
}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -89,7 +108,7 @@ func (m *model) initLists(width, height int) {
// Update the styles with the correct width // Update the styles with the correct width
dialogStyle = dialogStyle.Width(m.width) dialogStyle = dialogStyle.Width(m.width)
listStyle = listStyle.Width(m.width) listStyle = listStyle.Width(m.width)
titleStyle = titleStyle.Width(m.width - 2) titleStyle = titleStyle.Width(m.width - 4)
choiceStyle = choiceStyle.Width(m.width) choiceStyle = choiceStyle.Width(m.width)
// Make m.fetched and set all values to FALSE // Make m.fetched and set all values to FALSE