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{ "pager", "publish to html", "publish to gopher", "default to nopub", "default to html", } func NewConfig() *Config { cfg, err := config.Read() if err != nil { cfg = config.Default } return &Config{ title, ui.NewList(configList, 0), "↑↓/kj move ↵ enter q return", cfg, []string{ `which pager do you want to use to display feels? here you can add additional pre-processing to your pager. leaving this option unset defaults to $PAGER, and if that isn't set, neofeels falls back to less.`, `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.`, `should your posts automatically be parsed as html in the neofeels reader? this is not related to the html publishing option, and only applies to the terminal reader. 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", "Ctrl+n": config.list.Down() case "Up", "k", "Ctrl+p": 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 "q", "h", "Left": ui.ViewChange <- NewMainMenu(6) case "Enter", "l", "Right", "Space": switch config.list.Index() { case 0: config.config.Pager = config.changePager(state) case 1: config.config.Publishing = !config.config.Publishing case 2: config.config.Gopher = !config.config.Gopher case 3: config.config.Nopub = !config.config.Nopub case 4: config.config.HTML = !config.config.HTML case 5: 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: 6, }) win.New(win.Width/2-40, win.Height/2+5, 80, 10).Print(vaxis.Segment{Text: config.descriptions[config.list.Index()]}) win.New(win.Width/2-15, win.Height/2+15, 30, 1).Print(vaxis.Segment{Text: config.help}) // drawing the current selected options win.New(win.Width/2+7, win.Height/2-2, 40, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Pager)}) win.New(win.Width/2+7, win.Height/2-1, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Publishing)}) win.New(win.Width/2+7, win.Height/2, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Gopher)}) win.New(win.Width/2+7, win.Height/2+1, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.Nopub)}) win.New(win.Width/2+7, win.Height/2+2, 14, 1).Print(vaxis.Segment{Text: fmt.Sprintf(" %-12v", config.config.HTML)}) if config.config.Publishing { win.New(win.Width/2+7, win.Height/2+3, 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 + 2, 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 + 2, Width: 12, Height: 1, }) state.Render() } return "" } func (config *Config) changePager(state *ui.State) string { ti := textinput.New() ti.SetContent(config.config.Pager) config.Draw(state) win := state.Window() ti.Draw(vaxis.Window{ Vx: win.Vx, Parent: &win, Column: win.Width/2 + 9, Row: win.Height/2 - 2, Width: 40, 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 - 2, Width: 40, Height: 1, }) state.Render() } return "" }