<commit_before>// Copyright 2017 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"errors"
	"fmt"
	"io"
	"os"
	"os/exec"
	"time"
)

type colorInfo struct {
	SidebarsBg string
	SidebarsFg string
	Urgent     string
	Key        string
	Value      string
	Bg         string
}

type dzenInfo struct {
	name   string
	cmd    *exec.Cmd
	args   []string
	stdin  io.WriteCloser
	hidden bool
}

type popupConfig struct {
	Info    string
	Clock   string
	Weather string
	User    string
}

type barConfig struct {
	Height       int
	LeftBarWidth int
	Contiguous   string
	Position     string
}

type info struct {
	icon      string
	key       string
	value     string
	format    *string
	formatted string
	length    int
}

var (
	formatDefault string
	formatUrgent  string

	mainbarWidth  = 500
	leftBarWidth  = 0
	barHeight     = 15
	contiguousBar = true
	isTopBar      = true

	dzenMainbar []dzenInfo
	dzenLeftbar []dzenInfo

	err      error
	username string
)

const (
	// Yeah, I am wondering about this as well.
	barWidthMagic = 7.5
)

func initDzenBars() {
	dzenLeftbar = make([]dzenInfo, len(monitors))
	dzenMainbar = make([]dzenInfo, len(monitors))

}

func loadDzenColorFormats() {
	formatDefault = fmt.Sprintf("^fg(%s)%%s ^fg(%s)%%s", config.Colors.Key, config.Colors.Value)
	formatUrgent = fmt.Sprintf("^fg(%s)%%s %%s", config.Colors.Urgent)
}

func barWidthFromKey(key string) int {
	w := 0
	started := false
	for i := range keys {
		if keys[i] == key {
			started = true
		}

		if started {
			w += data[keys[i]].length
		}

	}
	w += len(username) + 5

	ret := int(float32(w) * barWidthMagic)
	return ret
}

func leftBarContent(screen int) string {
	if len(config.Popups.Info) > 0 {
		return fmt.Sprintf("^ca(1,%s %d %d %d)^fg(%s)^bg(%s)  info^fg(%s)^bg(%s)  ^ca()\n", config.Popups.Info, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
	}
	return fmt.Sprintf("^fg(%s)^bg(%s)  info^fg(%s)^bg(%s)  \n", config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
}

func statusBarLen() int {
	bar := ""
	var key string

	// Regular bar without any color formatting.
	for i := range keys {
		key = keys[i]
		if collected, ok := data[key]; ok {
			bar = fmt.Sprintf("%s %s %s", bar, collected.icon, collected.value)
		}
	}

	return len(bar)
}

func statusBar(screen int) string {
	bar := ""
	var key string

	for i := range keys {
		key = keys[i]
		if collected, ok := data[key]; ok {
			switch {
			case key == "clock" && len(config.Popups.Clock) > 0:
				barWidth := barWidthFromKey(key)
				bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Clock, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
			case key == "weather" && len(config.Popups.Weather) > 0:
				barWidth := barWidthFromKey(key)
				bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Weather, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
			default:
				bar = fmt.Sprintf("%s %s", bar, collected.formatted)
			}
		}
	}
	if len(config.Popups.User) > 0 {
		bar = fmt.Sprintf("%s ^ca(1,dzen-popup-user %d %d %d)^fg(%s)^fg(%s)^bg(%s) %s ^ca()", bar, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
	} else {
		bar = fmt.Sprintf("%s ^fg(%s)^fg(%s)^bg(%s) %s ", bar, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
	}

	resizeDzenMainBar()
	return bar
}

func updateStatusBar() {
	collectStats()
	for i := 0; i < len(dzenMainbar); i++ {
		io.WriteString(dzenMainbar[i].stdin, fmt.Sprintf("%s\n", statusBar(i)))
	}
}

func resizeDzenMainBar() {
	if contiguousBar {
		return
	}

	if newStatusbarLen := int(float32(statusBarLen()) * barWidthMagic); newStatusbarLen != mainbarWidth {
		mainbarWidth = newStatusbarLen
		drawDzenMainBar()<commit_msg>Parametrize user popup command<commit_after>// Copyright 2017 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"errors"
	"fmt"
	"io"
	"os"
	"os/exec"
	"time"
)

type colorInfo struct {
	SidebarsBg string
	SidebarsFg string
	Urgent     string
	Key        string
	Value      string
	Bg         string
}

type dzenInfo struct {
	name   string
	cmd    *exec.Cmd
	args   []string
	stdin  io.WriteCloser
	hidden bool
}

type popupConfig struct {
	Info    string
	Clock   string
	Weather string
	User    string
}

type barConfig struct {
	Height       int
	LeftBarWidth int
	Contiguous   string
	Position     string
}

type info struct {
	icon      string
	key       string
	value     string
	format    *string
	formatted string
	length    int
}

var (
	formatDefault string
	formatUrgent  string

	mainbarWidth  = 500
	leftBarWidth  = 0
	barHeight     = 15
	contiguousBar = true
	isTopBar      = true

	dzenMainbar []dzenInfo
	dzenLeftbar []dzenInfo

	err      error
	username string
)

const (
	// Yeah, I am wondering about this as well.
	barWidthMagic = 7.5
)

func initDzenBars() {
	dzenLeftbar = make([]dzenInfo, len(monitors))
	dzenMainbar = make([]dzenInfo, len(monitors))

}

func loadDzenColorFormats() {
	formatDefault = fmt.Sprintf("^fg(%s)%%s ^fg(%s)%%s", config.Colors.Key, config.Colors.Value)
	formatUrgent = fmt.Sprintf("^fg(%s)%%s %%s", config.Colors.Urgent)
}

func barWidthFromKey(key string) int {
	w := 0
	started := false
	for i := range keys {
		if keys[i] == key {
			started = true
		}

		if started {
			w += data[keys[i]].length
		}

	}
	w += len(username) + 5

	ret := int(float32(w) * barWidthMagic)
	return ret
}

func leftBarContent(screen int) string {
	if len(config.Popups.Info) > 0 {
		return fmt.Sprintf("^ca(1,%s %d %d %d)^fg(%s)^bg(%s)  info^fg(%s)^bg(%s)  ^ca()\n", config.Popups.Info, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
	}
	return fmt.Sprintf("^fg(%s)^bg(%s)  info^fg(%s)^bg(%s)  \n", config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
}

func statusBarLen() int {
	bar := ""
	var key string

	// Regular bar without any color formatting.
	for i := range keys {
		key = keys[i]
		if collected, ok := data[key]; ok {
			bar = fmt.Sprintf("%s %s %s", bar, collected.icon, collected.value)
		}
	}

	return len(bar)
}

func statusBar(screen int) string {
	bar := ""
	var key string

	for i := range keys {
		key = keys[i]
		if collected, ok := data[key]; ok {
			switch {
			case key == "clock" && len(config.Popups.Clock) > 0:
				barWidth := barWidthFromKey(key)
				bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Clock, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
			case key == "weather" && len(config.Popups.Weather) > 0:
				barWidth := barWidthFromKey(key)
				bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Weather, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
			default:
				bar = fmt.Sprintf("%s %s", bar, collected.formatted)
			}
		}
	}
	if len(config.Popups.User) > 0 {
		bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d)^fg(%s)^fg(%s)^bg(%s) %s ^ca()", bar, config.Popups.User, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
	} else {
		bar = fmt.Sprintf("%s ^fg(%s)^fg(%s)^bg(%s) %s ", bar, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
	}

	resizeDzenMainBar()
	return bar
}

func updateStatusBar() {
	collectStats()
	for i := 0; i < len(dzenMainbar); i++ {
		io.WriteString(dzenMainbar[i].stdin, fmt.Sprintf("%s\n", statusBar(i)))
	}
}

func resizeDzenMainBar() {
	if contiguousBar {
		return
	}

	if newStatusbarLen := int(float32(statusBarLen()) * barWidthMagic); newStatusbarLen != mainbarWidth {
		mainbarWidth = newStatusbarLen
		drawDzenMainBar()
{'fraction_non_alphanumeric': 0.1448486168590721, 'fraction_numerical': 0.007623611413635374, 'mean_word_length': 3.7213367609254497, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1096995', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3322, 'n_words': 869}