package app import ( "fmt" "strconv" "git.sr.ht/~rockorager/vaxis" "git.sr.ht/~rockorager/vaxis/widgets/textinput" "git.tilde.town/nbsp/neofeels/config" "git.tilde.town/nbsp/neofeels/ui" ) type Config struct { title string list ui.List help string config *config.Config descriptions []string } var configList = []string{ "publish to html", "publish to gopher", "default to nopub", } func NewConfig() *Config { cfg, err := config.Read() if err != nil { cfg = config.Default } return &Config{ title, ui.NewList(configList), "↑↓/kj move ↵ enter q return", cfg, []string{ `do you want to publish your feels online? if yes, your feels will be published to a directory of your choice in your public_html. if not, your feels will only be readable from within the tilde.town network. if you already have a publishing directory, i'll remove it for you (don't worry, your written entries will still be saved!)`, `gopher is a pre-web technology that is text-oriented and primarily used to share folders of your files with the world. would you like to publish your feels to gopher? entries you write will automatically get linked to ~/public_gopher/feels/, including gophermap generation. files you manually delete will no longer be visible from your gopherhole, and will be purged from your gophermap on your next entry update.`, `should your posts automatically show up on your world-visible pages, such as html and gopher? you can change this behaviour on a per-post basis after the fact. changes to this setting will not be made retroactively.`, `in which directory under public_html should your blog reside? for example, "blog" will make it visible under https://tilde.town/~you/blog.`, }, } } func (config *Config) Event(state *ui.State, event vaxis.Event) (processed bool) { if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress { switch key.String() { case "Ctrl+c", "Ctrl+d": close(ui.Quit) case "Down", "j": config.list.Down() case "Up", "k": config.list.Up() case "End", "Shift+g": config.list.End() case "Home", "g": config.list.Home() case "0", "1": i, _ := strconv.Atoi(key.String()) config.list.SetIndex(i) case "2", "3": if config.config.Publishing { i, _ := strconv.Atoi(key.String()) config.list.SetIndex(i) } case "q", "h", "Left": ui.ViewChange <- NewMainMenu() case "Enter", "l", "Right", "Space": switch config.list.Index() { case 0: config.config.Publishing = !config.config.Publishing case 1: config.config.Gopher = !config.config.Gopher case 2: config.config.Nopub = !config.config.Nopub case 3: config.config.PublishDir = config.changePublishDir(state) } config.config.Write() } processed = true } config.Draw(state) return } func (config *Config) Draw(state *ui.State) { if config.config.Publishing { config.list.SetItems(append(configList, "html publish directory")) } else { config.list.SetItems(configList) } win := state.Window() win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: config.title}) config.list.Draw(vaxis.Window{ Vx: win.Vx, Parent: nil, Column: win.Width/2 - 21, Row: win.Height/2 - 2, Width: 28, Height: 4, }) win.New(win.Width/2-40, win.Height/2+3, 80, 10).Print(vaxis.Segment{Text: config.descriptions[config.list.Index()]}) win.New(win.Width/2-15, win.Height/2+13, 30, 1).Print(vaxis.Segment{Text: config.help}) // drawing the current selected options win.New(win.Width/2+7, win.Height/2-2, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Publishing)}) win.New(win.Width/2+7, win.Height/2-1, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Gopher)}) win.New(win.Width/2+7, win.Height/2, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Nopub)}) if config.config.Publishing { win.New(win.Width/2+7, win.Height/2+1, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.PublishDir)}) } } func (config *Config) changePublishDir(state *ui.State) string { ti := textinput.New() ti.SetContent(config.config.PublishDir) config.Draw(state) win := state.Window() ti.Draw(vaxis.Window{ Vx: win.Vx, Parent: &win, Column: win.Width/2 + 9, Row: win.Height/2 + 1, Width: 12, Height: 1, }) for ev := range win.Vx.Events() { switch ev := ev.(type) { case vaxis.Key: switch ev.String() { case "Ctrl+c", "Esc", "Enter": state.HideCursor() return ti.String() } } ti.Update(ev) config.Draw(state) ti.Draw(vaxis.Window{ Vx: win.Vx, Parent: nil, Column: win.Width/2 + 9, Row: win.Height/2 + 1, Width: 12, Height: 1, }) state.Render() } return "" }