<commit_before>// Copyright © 2017 National Library of Norway.
// 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 util

import (
	"fmt"
	tm "github.com/buger/goterm"
	"github.com/golang/protobuf/proto"
	"io"
	"reflect"
	"strings"
)

var debugEnabled = false

func MarshalTable(w io.Writer, msg proto.Message) error {
	table := tm.NewTable(2, 10, 2, ' ', 0)

	var values reflect.Value
	values = reflect.ValueOf(msg).Elem().FieldByName("Value")
	if values.IsValid() {
		if values.Len() == 0 {
			fmt.Println("Empty result")
			return nil
		}

		m := values.Index(0).Interface().(proto.Message)
		tableDef := GetTableDef(m)
		formatHeader(table, tableDef, m)

		for i := 0; i < values.Len(); i++ {
			m := values.Index(i).Interface().(proto.Message)
			err := formatData(table, tableDef, m)
			if err != nil {
				return err
			}
			debug(m)
		}
	} else {
		m := reflect.ValueOf(msg).Interface().(proto.Message)

		debug(m)

		tableDef := GetTableDef(m)
		formatHeader(table, tableDef, m)

		err := formatData(table, tableDef, m)
		if err != nil {
			return err
		}
	}

	fmt.Fprintln(w, table)

	return nil
}

func debug(m proto.Message) {
	if debugEnabled {
		v := reflect.ValueOf(m).Elem()
		t := v.Type()
		for i := 0; i < v.NumField(); i++ {
			f := v.Field(i)
			fmt.Printf("%d: %s %s = %v\n", i,
				t.Field(i).Name, f.Type(), f.Interface())
		}
	}
}<commit_msg>Revert "Make output in table format work with output option -o and behave as expected when paging overflows screen"<commit_after>// Copyright © 2017 National Library of Norway.
// 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 util

import (
	"fmt"
	tm "github.com/buger/goterm"
	"github.com/golang/protobuf/proto"
	"io"
	"reflect"
	"strings"
)

var debugEnabled = false

func MarshalTable(w io.Writer, msg proto.Message) error {
	table := tm.NewTable(2, 10, 2, ' ', 0)

	var values reflect.Value
	values = reflect.ValueOf(msg).Elem().FieldByName("Value")
	if values.IsValid() {
		if values.Len() == 0 {
			fmt.Println("Empty result")
			return nil
		}

		m := values.Index(0).Interface().(proto.Message)
		tableDef := GetTableDef(m)
		formatHeader(table, tableDef, m)

		for i := 0; i < values.Len(); i++ {
			m := values.Index(i).Interface().(proto.Message)
			err := formatData(table, tableDef, m)
			if err != nil {
				return err
			}
			debug(m)
		}
	} else {
		m := reflect.ValueOf(msg).Interface().(proto.Message)

		debug(m)

		tableDef := GetTableDef(m)
		formatHeader(table, tableDef, m)

		err := formatData(table, tableDef, m)
		if err != nil {
			return err
		}
	}

	tm.Println(table)
	tm.Flush()

	return nil
}

func debug(m proto.Message) {
	if debugEnabled {
		v := reflect.ValueOf(m).Elem()
		t := v.Type()
		for i := 0; i < v.NumField(); i++ {
			f := v.Field(i)
			fmt.Printf("%d: %s %s = %v\n", i,
				t.Field(i).Name, f.Type(), f.Interface())
		}
	}
}
{'fraction_non_alphanumeric': 0.1461836998706339, 'fraction_numerical': 0.008796895213454075, 'mean_word_length': 3.305122494432071, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '5558570', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1293, 'n_words': 410}