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,6 +6,9 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
// If we are not done
if m.focused != DONE {
// Setup keybindings
switch msg.String() {
case "ctrl+c", "q":
@ -32,6 +35,25 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
}
} else {
// If we are done then handle keybindings a bit differently
// Setup keybindings for authDialog
switch msg.String() {
case "ctrl+c":
// Exit when user presses 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
}
}
}
m.authDialog, cmd = m.authDialog.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
if m.width == 0 {
// Initialize the static lists and make sure the content
@ -52,12 +74,14 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Update the styles with the correct width
dialogStyle = dialogStyle.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)
}
}
}
}
// Run another update loop
m.lists[m.focused], cmd = m.lists[m.focused].Update(msg)
return m, cmd
}

View file

@ -12,6 +12,7 @@ import (
func (m model) View() string {
if m.width != 0 {
title := ""
view := "Empty View :("
switch m.focused {
case INTRO:
title = dialogStyle.Render(
@ -31,11 +32,16 @@ func (m model) View() string {
"becomes unbootable, as you will be asked to verify the files generated",
),
)
view = listStyle.Render(m.lists[m.focused].View())
case GPUS:
title = titleStyle.MarginLeft(2).Render(
"Select a GPU to check the IOMMU groups of",
)
view = listStyle.Render(m.lists[m.focused].View())
case GPU_GROUP:
title = titleStyle.Render(
fmt.Sprint(
@ -44,11 +50,15 @@ func (m model) View() string {
),
)
view = listStyle.Render(m.lists[m.focused].View())
case USB:
title = titleStyle.Render(
"[OPTIONAL]: Select a USB Controller to check the IOMMU groups of",
)
view = listStyle.Render(m.lists[m.focused].View())
case USB_GROUP:
title = titleStyle.Render(
fmt.Sprint(
@ -57,6 +67,8 @@ func (m model) View() string {
),
)
view = listStyle.Render(m.lists[m.focused].View())
case VBIOS:
// Get the program directory
exe, _ := os.Executable()
@ -89,6 +101,8 @@ func (m model) View() string {
title = fmt.Sprintf(text, m.vbios_path, scriptdir)
view = listStyle.Render(m.lists[m.focused].View())
case VIDEO:
title = dialogStyle.Render(
fmt.Sprint(
@ -99,13 +113,15 @@ func (m model) View() string {
),
)
view = listStyle.Render(m.lists[m.focused].View())
case DONE:
title = dialogStyle.Render(
fmt.Sprint(
"The configuration files have been generated and are\n",
"located inside the \"config\" folder\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 files inside the \"etc\" folder must be copied to your system.\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.",
),
)
view = m.authDialog.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 {
return "Loading..."
}

View file

@ -1,7 +1,12 @@
package internal
import (
"fmt"
"os/user"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
@ -29,6 +34,7 @@ type model struct {
offsety []int
width int
height int
authDialog textinput.Model
}
// Consts used to navigate the main model
@ -44,8 +50,21 @@ const (
)
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
return &model{}
return &model{
authDialog: authInput,
}
}
func (m model) Init() tea.Cmd {
@ -89,7 +108,7 @@ func (m *model) initLists(width, height int) {
// Update the styles with the correct width
dialogStyle = dialogStyle.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)
// Make m.fetched and set all values to FALSE