restructor code

This commit is contained in:
Jeff Carr 2025-08-25 10:02:14 -05:00
parent 9d5bd8d5b9
commit 6dd0052dcf
10 changed files with 350 additions and 132 deletions

View File

@ -3,13 +3,15 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
default: goimports verbose
build:
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
./xstartplacement
./startxplacement
verbose:
GO111MODULE=off go build -v -x \
GO111MODULE=off go install -v -x \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
install:

View File

@ -1 +1,24 @@
# devils pie code to try to fix window placement
# This is an attempt to redo 'startx' not as a bash script this time
GOALS:
* use a protobuf TEXT config file
* store global file in ~/etc/startx.text
* store user config file in ~/.config/startx.text
* parse additional settings from ~/.config/startx.d/
* restore the geometry of the apps
* make a GUI save/edit/restore tool
CURRENTLY WORKING:
* works with x windows
* restores mate-terminal on debian
TODO:
* wayland support
* other programs like firefox & keepass
* actually execute from lightdm
* run underneath mate-session
* run underneath all the other WM
* rename binary 'startx'

36
argv.go Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
/*
this parses the command line arguements using alex flint's go-arg
*/
var argv args
type args struct {
Restore string `arg:"--restore" help:"restore terminal windows from a config file"`
Save *EmptyCmd `arg:"subcommand:save" help:"save current window geometries to the your config file"`
Dump *EmptyCmd `arg:"subcommand:dump" help:"show your current window geometries"`
Force bool `arg:"--force" help:"try to strong arm things"`
Verbose bool `arg:"--verbose" help:"show more output"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"`
}
type EmptyCmd struct {
}
func (args) Version() string {
return ARGNAME + " " + VERSION + " Built on " + BUILDTIME
}
func (a args) Description() string {
return `
startxplacment -- run this after 'startx' to restore all your apps
will attempt to launch your terminal windows on the right Workspaces
and with the right geometries. TODO: restore the bash working paths
`
}

87
argvAutoshell.go Normal file
View File

@ -0,0 +1,87 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"os"
)
/*
handles shell autocomplete
*/
// used for shell auto completion
// var ARGNAME string = "forge" // todo: get this from $0 ?
func deleteMatch() {
// f := forgedb.InitSimple()
fmt.Println("go.wit.com/lib/gui/repostatus todo: need to do this")
}
func (args) doBashAuto() {
argv.doBashHelp()
switch argv.BashAuto[0] {
case "dump":
fmt.Println("--terminals")
default:
if argv.BashAuto[0] == ARGNAME {
// list the subcommands here
fmt.Println("--restore save dump")
}
}
os.Exit(0)
}
// prints help to STDERR // TODO: move everything below this to go-args
func (args) doBashHelp() {
if argv.BashAuto[1] != "''" {
// if this is not blank, then the user has typed something
return
}
if argv.BashAuto[0] != ARGNAME {
// if this is not the name of the command, the user already started doing something
return
}
if argv.BashAuto[0] == ARGNAME {
me.pp.WriteHelp(os.Stderr)
return
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "something went wrong with the GO args package")
fmt.Fprintln(os.Stderr, "")
}
// complete -F forge --bash forge
func (args) doBash() {
fmt.Println("# add this in your bashrc:")
fmt.Println("")
fmt.Println("# todo: add this to go-arg as a 'hidden' go-arg option --bash")
fmt.Println("#")
fmt.Println("# Put the below in the file: ~/.local/share/bash-completion/completions/" + ARGNAME)
fmt.Println("#")
fmt.Println("# todo: make this output work/parse with:")
fmt.Println("# complete -C " + ARGNAME + " --bash go")
fmt.Println("")
fmt.Println("_" + ARGNAME + "_complete()")
fmt.Println("{")
fmt.Println(" # sets local to this func vars")
fmt.Println(" local cur prev all")
fmt.Println(" cur=${COMP_WORDS[COMP_CWORD]}")
fmt.Println(" prev=${COMP_WORDS[COMP_CWORD-1]}")
fmt.Println(" all=${COMP_WORDS[@]}")
fmt.Println("")
fmt.Println(" # this is where we generate the go-arg output")
fmt.Println(" GOARGS=$(" + ARGNAME + " --auto-complete $prev \\'$cur\\' $all)")
fmt.Println("")
fmt.Println(" # this compares the command line input from the user")
fmt.Println(" # to whatever strings we output")
fmt.Println(" COMPREPLY=( $(compgen -W \"$GOARGS\" -- $cur) ) # THIS WORKS")
fmt.Println(" return 0")
fmt.Println("}")
fmt.Println("complete -F _" + ARGNAME + "_complete " + ARGNAME)
fmt.Println("")
fmt.Println("# copy and paste the above into your bash shell should work")
os.Exit(0)
}

102
common.go Normal file
View File

@ -0,0 +1,102 @@
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Helper functions
func getWindowList() (map[string]string, error) {
cmd := exec.Command("wmctrl", "-l")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, err
}
windows := make(map[string]string)
scanner := bufio.NewScanner(&out)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) > 0 {
windows[fields[0]] = strings.Join(fields[3:], " ")
}
}
return windows, nil
}
// findNewWindow compares two maps of windows and returns the ID of the new window.
func findNewWindow(before, after map[string]string) string {
for id := range after {
if _, ok := before[id]; !ok {
return id
}
}
return ""
}
// parseConfig remains the same as before.
func parseConfig(filePath string) ([]WindowConfig, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var configs []WindowConfig
scanner := bufio.NewScanner(file)
var currentConfig WindowConfig
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not get user home directory: %w", err)
}
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, " Title: ") {
title := strings.TrimSpace(strings.TrimPrefix(line, " Title: "))
currentConfig.Title = title
parts := strings.SplitN(title, ": ", 2)
if len(parts) == 2 {
path := parts[1]
if strings.HasPrefix(path, "~") {
path = filepath.Join(homeDir, path[1:])
}
currentConfig.Path = path
}
} else if strings.HasPrefix(line, " Geometry: ") {
geomStr := strings.TrimSpace(strings.TrimPrefix(line, " Geometry: "))
var x, y, w, h string
_, err := fmt.Sscanf(geomStr, "X=%s Y=%s Width=%s Height=%s", &x, &y, &w, &h)
if err == nil {
x = strings.TrimSuffix(x, ",")
y = strings.TrimSuffix(y, ",")
w = strings.TrimSuffix(w, ",")
currentConfig.Geometry = fmt.Sprintf("%sx%s+%s+%s", w, h, x, y)
}
} else if strings.HasPrefix(line, " Workspace: ") {
currentConfig.Workspace = strings.TrimSpace(strings.TrimPrefix(line, " Workspace: "))
} else if line == "---" {
if currentConfig.Path != "" {
configs = append(configs, currentConfig)
}
currentConfig = WindowConfig{} // Reset for the next entry
}
}
if currentConfig.Path != "" {
configs = append(configs, currentConfig)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return configs, nil
}

22
exit.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"os"
"go.wit.com/log"
)
func okExit(thing string) {
if thing != "" {
log.Info("regex exit:", thing, "ok")
}
os.Exit(0)
}
func badExit(err error) {
log.Info("regex failed: ", err)
os.Exit(-1)
}

View File

@ -1,12 +1,9 @@
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
@ -19,7 +16,7 @@ type WindowConfig struct {
Workspace string
}
func main() {
func doLaunch() {
// 1. Get current working directory.
pwd, err := os.Getwd()
if err != nil {
@ -28,7 +25,6 @@ func main() {
}
// 2. Read and parse the configuration file.
configFile := "/home/jcarr/go/src/gemini/xstartplacement.out"
configs, err := parseConfig(configFile)
if err != nil {
fmt.Printf("Failed to parse config file '%s': %v\n", configFile, err)
@ -108,95 +104,3 @@ func main() {
fmt.Println("Window setup complete.")
}
}
// getWindowList returns a map of window IDs to their titles.
func getWindowList() (map[string]string, error) {
cmd := exec.Command("wmctrl", "-l")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, err
}
windows := make(map[string]string)
scanner := bufio.NewScanner(&out)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) > 0 {
windows[fields[0]] = strings.Join(fields[3:], " ")
}
}
return windows, nil
}
// findNewWindow compares two maps of windows and returns the ID of the new window.
func findNewWindow(before, after map[string]string) string {
for id := range after {
if _, ok := before[id]; !ok {
return id
}
}
return ""
}
// parseConfig remains the same as before.
func parseConfig(filePath string) ([]WindowConfig, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var configs []WindowConfig
scanner := bufio.NewScanner(file)
var currentConfig WindowConfig
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not get user home directory: %w", err)
}
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, " Title: ") {
title := strings.TrimSpace(strings.TrimPrefix(line, " Title: "))
currentConfig.Title = title
parts := strings.SplitN(title, ": ", 2)
if len(parts) == 2 {
path := parts[1]
if strings.HasPrefix(path, "~") {
path = filepath.Join(homeDir, path[1:])
}
currentConfig.Path = path
}
} else if strings.HasPrefix(line, " Geometry: ") {
geomStr := strings.TrimSpace(strings.TrimPrefix(line, " Geometry: "))
var x, y, w, h string
_, err := fmt.Sscanf(geomStr, "X=%s Y=%s Width=%s Height=%s", &x, &y, &w, &h)
if err == nil {
x = strings.TrimSuffix(x, ",")
y = strings.TrimSuffix(y, ",")
w = strings.TrimSuffix(w, ",")
currentConfig.Geometry = fmt.Sprintf("%sx%s+%s+%s", w, h, x, y)
}
} else if strings.HasPrefix(line, " Workspace: ") {
currentConfig.Workspace = strings.TrimSpace(strings.TrimPrefix(line, " Workspace: "))
} else if line == "---" {
if currentConfig.Path != "" {
configs = append(configs, currentConfig)
}
currentConfig = WindowConfig{} // Reset for the next entry
}
}
if currentConfig.Path != "" {
configs = append(configs, currentConfig)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return configs, nil
}

55
main.go Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// An app to submit patches for the 30 GO GUI repos
import (
"os"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/log"
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
// used for shell auto completion
var ARGNAME string = "startxplacment"
// using this for now. triggers config save
var configSave bool
var configFile string = "/home/jcarr/.config/startxplacement.out"
func main() {
me = new(mainType)
gui.InitArg()
me.pp = arg.MustParse(&argv)
if argv.Bash {
argv.doBash()
os.Exit(0)
}
if len(argv.BashAuto) != 0 {
argv.doBashAuto()
os.Exit(0)
}
if argv.Dump != nil {
// doDump()
log.Info("dump here")
okExit("")
}
if argv.Restore != "" {
log.Info("restore here")
okExit("")
}
// doGui()
okExit("")
}

17
structs.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
)
var me *mainType
// this app's variables
type mainType struct {
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
myGui *gui.Node // the gui toolkit handle
}

View File

@ -26,9 +26,8 @@ type CurrentState struct {
Path string
}
func main() {
func doSync() {
// 1. Read the desired state from the config file.
configFile := "/home/jcarr/go/src/gemini/xstartplacement.out"
desiredStates, err := parseDesiredState(configFile)
if err != nil {
fmt.Printf("Error parsing config file: %v\n", err)
@ -105,7 +104,7 @@ func launchTerminal(state DesiredState) {
fmt.Printf("Successfully launched terminal for %s\n", state.Path)
}
// parseDesiredState reads the xstartplacement.out file.
// parseDesiredState reads the startxplacement.out file.
func parseDesiredState(filePath string) ([]DesiredState, error) {
file, err := os.Open(filePath)
if err != nil {
@ -197,32 +196,3 @@ func getCurrentState() ([]CurrentState, error) {
}
return states, nil
}
// Helper functions
func getWindowList() (map[string]string, error) {
cmd := exec.Command("wmctrl", "-l")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, err
}
windows := make(map[string]string)
scanner := bufio.NewScanner(&out)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) > 0 {
windows[fields[0]] = strings.Join(fields[3:], " ")
}
}
return windows, nil
}
func findNewWindow(before, after map[string]string) string {
for id := range after {
if _, ok := before[id]; !ok {
return id
}
}
return ""
}