Skip to content

Commit 7b03227

Browse files
committed
Got both lists working and scaling up of terminal
1 parent 240dfd6 commit 7b03227

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

main.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
func (m Model) Init() tea.Cmd {
16-
return createModel
16+
return nil
1717
}
1818

1919
var (
20+
docStyle = lipgloss.NewStyle().Padding(1, 2)
2021
titleStyle = lipgloss.NewStyle().MarginLeft(2)
2122
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
2223
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
@@ -38,7 +39,7 @@ func (d displayItemDelegate) Render(w io.Writer, m list.Model, index int, listIt
3839
return
3940
}
4041

41-
str := fmt.Sprintf("%d. %s", index+1, i.name)
42+
str := fmt.Sprintf("%-6d%s", index+1, i.name)
4243

4344
fn := itemStyle.Render
4445
if index == m.Index() {
@@ -65,7 +66,7 @@ func (r resolutionItemDelegate) Render(w io.Writer, m list.Model, index int, lis
6566
return
6667
}
6768

68-
str := fmt.Sprintf("%d. %s", index+1, i)
69+
str := fmt.Sprintf("%-6d%s", index+1, i)
6970

7071
fn := itemStyle.Render
7172
if index == m.Index() {
@@ -77,7 +78,7 @@ func (r resolutionItemDelegate) Render(w io.Writer, m list.Model, index int, lis
7778
fmt.Fprint(w, fn(str))
7879
}
7980

80-
func createModel() tea.Msg {
81+
func createModel() Model {
8182
cmd := exec.Command("xrandr", "-q")
8283
stdout, _ := cmd.CombinedOutput()
8384
output := string(stdout)
@@ -100,8 +101,11 @@ func createModel() tea.Msg {
100101
for _, res := range lines[begin:end] {
101102
resolutions = append(resolutions, resolution(get_res(res)))
102103
}
103-
displayResolutions := list.New(resolutions, resolutionItemDelegate{}, 32, 14)
104-
displayResolutions.Title = fmt.Sprintf("Select resoultion for %s", nl.name)
104+
displayResolutions := list.New(resolutions, resolutionItemDelegate{}, 32, 20)
105+
displayResolutions.Title = fmt.Sprintf("Resolution for %s", nl.name)
106+
displayResolutions.SetShowStatusBar(false)
107+
displayResolutions.SetShowHelp(false)
108+
displayResolutions.SetShowPagination(false)
105109
display := Display{nl.name, nl.connected, nl.current, displayResolutions}
106110
if display.connected {
107111
displays = append(displays, display)
@@ -118,9 +122,11 @@ func createModel() tea.Msg {
118122

119123
}
120124

121-
l := list.New(displays, displayItemDelegate{}, 20, 14)
125+
l := list.New(displays, displayItemDelegate{}, 32, 20)
122126
l.SetShowStatusBar(true)
123-
l.Title = current
127+
l.SetShowHelp(false)
128+
l.SetShowPagination(false)
129+
l.Title = fmt.Sprintf("Current: %s", current)
124130
l.SetFilteringEnabled(false)
125131
l.SetShowPagination(false)
126132

@@ -145,29 +151,45 @@ func extract_metadata(line string) NameLine {
145151
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
146152
switch msg := msg.(type) {
147153
case tea.WindowSizeMsg:
148-
return m, nil
149-
case Model:
150-
m = msg
154+
h, v := docStyle.GetFrameSize()
155+
m.displays.SetSize(msg.Width-h, msg.Height-v)
156+
for _, disp := range m.displays.Items() {
157+
switch d := disp.(type) {
158+
case Display:
159+
d.resolutions.SetSize(msg.Width-h, msg.Height-v)
160+
default:
161+
m.displays.Title = "Not that type."
162+
}
163+
}
164+
165+
var cmd tea.Cmd
166+
if m.selected {
167+
m.display.resolutions, cmd = m.display.resolutions.Update(msg)
168+
} else {
169+
m.displays, cmd = m.displays.Update(msg)
170+
}
171+
return m, cmd
151172
case Status:
152173
return m, tea.Quit
153174
case tea.KeyMsg:
154175
switch msg.String() {
155-
156176
case "ctrl+c", "q":
157177
return m, tea.Quit
158178
case "b":
159179
m.selected = false
160180
return m, nil
161181
case "enter", " ":
162182
if m.selected {
163-
183+
return m, change_resolution(m)
164184
} else {
165185
var si = m.displays.Index()
166186
var sn = m.displays.SelectedItem().(Display)
167187
m.display = sn
168188
m.selected = true
189+
var updateCmd tea.Cmd
190+
m.display.resolutions, updateCmd = m.display.resolutions.Update(msg)
169191
statusCmd := m.displays.NewStatusMessage(fmt.Sprintf("%d", si))
170-
return m, tea.Cmd(statusCmd)
192+
return m, tea.Batch(updateCmd, statusCmd)
171193
}
172194
}
173195
}
@@ -182,10 +204,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
182204
}
183205

184206
func change_resolution(m Model) tea.Cmd {
207+
var sd = m.displays.SelectedItem().(Display)
208+
resolution := string(sd.resolutions.SelectedItem().(resolution))
209+
new := sd.name
185210
return func() tea.Msg {
186-
exec.Command("xrandr", "--output", m.screen, "--mode", m.resolution, "--fb", m.resolution, "--primary").Run()
211+
exec.Command("xrandr", "--output", new, "--mode", resolution, "--fb", resolution, "--primary").Run()
187212

188-
if m.current != m.screen {
213+
if m.current != new {
189214
exec.Command("xrandr", "--output", m.current, "--off").Run()
190215
exec.Command("bspc", "desktop", m.current, "--to-monitor", m.screen).Run()
191216
return Status("Changed resolution and moved desktop")
@@ -201,14 +226,14 @@ func get_res(line string) string {
201226

202227
func (m Model) View() string {
203228
if !m.selected {
204-
return "\n" + m.displays.View()
229+
return docStyle.Render(m.displays.View())
205230
} else {
206-
return "\n" + m.display.resolutions.View()
231+
return docStyle.Render(m.display.resolutions.View())
207232
}
208233
}
209234

210235
func main() {
211-
p := tea.NewProgram(Model{}, tea.WithAltScreen())
236+
p := tea.NewProgram(createModel(), tea.WithAltScreen())
212237
if _, err := p.Run(); err != nil {
213238
fmt.Printf("There's been an error: %v", err)
214239
os.Exit(1)

structs.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ type NameLine struct {
1818
}
1919

2020
type Model struct {
21-
displays list.Model
22-
selected bool
23-
display Display
24-
resolutions []string
25-
screen string
26-
current string
27-
resolution string
21+
displays list.Model
22+
selected bool
23+
display Display
24+
screen string
25+
current string
26+
resolution string
2827
}
2928

3029
type Status string

0 commit comments

Comments
 (0)