Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: term.GetTerminalWidth failed on windows #819

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions third_party/dyff/output_human.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ import (

"github.com/gonvenience/bunt"
"github.com/gonvenience/neat"
"github.com/gonvenience/term"
"github.com/gonvenience/text"
"github.com/gonvenience/ytbx"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/texttheater/golang-levenshtein/levenshtein"
yamlv3 "gopkg.in/yaml.v3"

"kusionstack.io/kusion/third_party/term"
)

// ascii font is slant
Expand All @@ -55,7 +56,7 @@ type stringWriter interface {
WriteString(s string) (int, error)
}

// HumanReport is a reporter with human readable output in mind
// HumanReport is a reporter with human-readable output in mind
type HumanReport struct {
NoTableStyle bool
DoNotInspectCerts bool
Expand All @@ -66,7 +67,7 @@ type HumanReport struct {
Report
}

// WriteReport writes a human readable report to the provided writer
// WriteReport writes a human-readable report to the provided writer
func (report *HumanReport) WriteReport(out io.Writer) error {
writer := bufio.NewWriter(out)
defer writer.Flush()
Expand Down Expand Up @@ -109,7 +110,7 @@ func (report *HumanReport) WriteReport(out io.Writer) error {
return nil
}

// generateHumanDiffOutput creates a human readable report of the provided diff and writes this into the given bytes buffer. There is an optional flag to indicate whether the document index (which documents of the input file) should be included in the report of the path of the difference.
// generateHumanDiffOutput creates a human-readable report of the provided diff and writes this into the given bytes buffer. There is an optional flag to indicate whether the document index (which documents of the input file) should be included in the report of the path of the difference.
func (report *HumanReport) generateHumanDiffOutput(output stringWriter, diff Diff, useGoPatchPaths bool, showDocumentIdx bool) error {
output.WriteString("\n")
output.WriteString(pathToString(diff.Path, useGoPatchPaths, showDocumentIdx))
Expand All @@ -125,7 +126,7 @@ func (report *HumanReport) generateHumanDiffOutput(output stringWriter, diff Dif
blocks[i] = generatedOutput
}

// For the use case in which only a path-less diff is suppose to be printed,
// For the use case in which only a path-less diff is supposed to be printed,
// omit the indent in this case since there is only one element to show
indent := 2
if len(diff.Path.PathElements) == 0 {
Expand Down Expand Up @@ -469,16 +470,17 @@ func (report *HumanReport) LoadX509Certs(from, to string) (string, string, error

// Create a YAML (hash with key/value) from a certificate to only display a few
// important fields (https://www.sslshopper.com/certificate-decoder.html):
// Common Name: www.example.com
// Organization: Company Name
// Organization Unit: Org
// Locality: Portland
// State: Oregon
// Country: US
// Valid From: April 2, 2018
// Valid To: April 2, 2019
// Issuer: www.example.com, Company Name
// Serial Number: 14581103526614300972 (0xca5a7c67490a792c)
//
// Common Name: www.example.com
// Organization: Company Name
// Organization Unit: Org
// Locality: Portland
// State: Oregon
// Country: US
// Valid From: April 2, 2018
// Valid To: April 2, 2019
// Issuer: www.example.com, Company Name
// Serial Number: 14581103526614300972 (0xca5a7c67490a792c)
func certificateSummaryAsYAML(cert *x509.Certificate) string {
const template = `Subject:
Common Name: %s
Expand Down
101 changes: 101 additions & 0 deletions third_party/term/term.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package term

import (
"os"

"github.com/mitchellh/go-ps"
"golang.org/x/crypto/ssh/terminal"
)

// DefaultTerminalWidth is the default fallback terminal width.
const DefaultTerminalWidth = 80

// DefaultTerminalHeight is the default fallback terminal height.
const DefaultTerminalHeight = 25

// FixedTerminalWidth allows a manual fixed override of the terminal width.
var FixedTerminalWidth = -1

// FixedTerminalHeight allows a manual fixed override of the terminal height.
var FixedTerminalHeight = -1

// GetTerminalWidth return the terminal width (available characters per line)
func GetTerminalWidth() int {
width, _ := GetTerminalSize()
return width
}

// GetTerminalSize return the terminal size as a width and height tuple. In
// case the terminal size cannot be determined, a reasonable default is
// used: 80x25. A manual override is possible using FixedTerminalWidth
// and FixedTerminalHeight.
func GetTerminalSize() (int, int) {
// In case this is a garden container, disable the terminal size detection
// and fall back to a reasonable assumption that is a bit bigger in size
// than the default terminal fallback dimensions.
if FixedTerminalWidth < 0 && FixedTerminalHeight < 0 && IsUnixGardenContainer() {
FixedTerminalWidth = 120
FixedTerminalHeight = 25
}

// Return user preference (explicit overwrite) of both width and height
if FixedTerminalWidth > 0 && FixedTerminalHeight > 0 {
return FixedTerminalWidth, FixedTerminalHeight
}

width, height, err := terminal.GetSize(int(os.Stdout.Fd()))

switch {
// Return default fallback value
case err != nil:
return DefaultTerminalWidth, DefaultTerminalHeight

// Return user preference of width, actual value for height
case FixedTerminalWidth > 0:
return FixedTerminalWidth, height

// Return user preference of height, actual value for width
case FixedTerminalHeight > 0:
return width, FixedTerminalHeight

// Return actual determined values
default:
return width, height
}
}

// IsUnixGardenContainer is used for Unix-like system, returns whether the current
// process is started in the process tree of garden container
// (https://github.com/cloudfoundry/garden).
// IsUnixGardenContainer fixes the function github.com/gonvenience/term@v1.0.0#IsGardenContainer,
// which causes nil-pointer panic on windows, cause there is no process of pid 1.
func IsUnixGardenContainer() bool {
if process, err := ps.FindProcess(1); err == nil && process != nil {
switch process.Executable() {
case "garden-init":
return true
}
}

return false
}
Loading