cli: extract complete_newline() to text_util module

I'll add more text formatting functions there.
This commit is contained in:
Yuya Nishihara 2023-03-04 16:55:01 +09:00
parent e8fd12aff6
commit 82604eda02
5 changed files with 29 additions and 15 deletions

View file

@ -59,7 +59,6 @@ use thiserror::Error;
use toml_edit;
use tracing_subscriber::prelude::*;
use crate::commit_templater;
use crate::config::{
config_path, AnnotatedValue, CommandNameAndArgs, ConfigSource, LayeredConfigs,
};
@ -68,6 +67,7 @@ use crate::merge_tools::{ConflictResolveError, DiffEditError};
use crate::template_parser::{TemplateAliasesMap, TemplateParseError};
use crate::templater::Template;
use crate::ui::{ColorChoice, Ui};
use crate::{commit_templater, text_util};
#[derive(Clone, Debug)]
pub enum CommandError {
@ -1882,7 +1882,7 @@ impl DescriptionArg {
impl From<String> for DescriptionArg {
fn from(s: String) -> Self {
DescriptionArg(complete_newline(s))
DescriptionArg(text_util::complete_newline(s))
}
}
@ -1898,14 +1898,6 @@ impl AsRef<str> for DescriptionArg {
}
}
pub fn complete_newline(s: impl Into<String>) -> String {
let mut s = s.into();
if !s.is_empty() && !s.ends_with('\n') {
s.push('\n');
}
s
}
#[derive(Clone, Debug)]
pub struct RevisionArg(String);

View file

@ -49,7 +49,7 @@ use jujutsu_lib::{conflicts, file_util, revset};
use maplit::{hashmap, hashset};
use crate::cli_util::{
self, check_stale_working_copy, get_config_file_path, print_checkout_stats,
check_stale_working_copy, get_config_file_path, print_checkout_stats,
resolve_multiple_nonempty_revsets, resolve_mutliple_nonempty_revsets_flag_guarded,
run_ui_editor, serialize_config_value, short_commit_hash, user_error, user_error_with_hint,
write_config_value_to_file, Args, CommandError, CommandHelper, DescriptionArg, RevisionArg,
@ -59,8 +59,8 @@ use crate::config::{AnnotatedValue, ConfigSource};
use crate::diff_util::{self, DiffFormat, DiffFormatArgs};
use crate::formatter::{Formatter, PlainTextFormatter};
use crate::graphlog::{get_graphlog, Edge};
use crate::template_parser;
use crate::ui::Ui;
use crate::{template_parser, text_util};
#[derive(clap::Parser, Clone, Debug)]
enum Commands {
@ -1770,7 +1770,7 @@ fn edit_description(
.filter(|line| !line.starts_with("JJ: "))
.join("\n");
description.truncate(description.trim_end_matches('\n').len());
Ok(cli_util::complete_newline(description))
Ok(text_util::complete_newline(description))
}
fn cmd_describe(

View file

@ -23,7 +23,6 @@ use jujutsu_lib::op_store::WorkspaceId;
use jujutsu_lib::repo::Repo;
use jujutsu_lib::rewrite;
use crate::cli_util;
use crate::formatter::Formatter;
use crate::template_parser::{
self, CoreTemplatePropertyKind, FunctionCallNode, IntoTemplateProperty, TemplateAliasesMap,
@ -33,6 +32,7 @@ use crate::templater::{
self, IntoTemplate, PlainTextFormattedProperty, Template, TemplateFunction, TemplateProperty,
TemplatePropertyFn,
};
use crate::text_util;
struct CommitTemplateLanguage<'repo, 'b> {
repo: &'repo dyn Repo,
@ -158,7 +158,7 @@ fn build_commit_keyword<'repo>(
let repo = language.repo;
let property = match name {
"description" => language.wrap_string(wrap_fn(|commit| {
cli_util::complete_newline(commit.description())
text_util::complete_newline(commit.description())
})),
"change_id" => language.wrap_commit_or_change_id(wrap_fn(move |commit| {
CommitOrChangeId::new(repo, IdKind::Change(commit.change_id().to_owned()))

View file

@ -27,5 +27,6 @@ pub mod operation_templater;
mod progress;
pub mod template_parser;
pub mod templater;
pub mod text_util;
pub mod time_util;
pub mod ui;

21
src/text_util.rs Normal file
View file

@ -0,0 +1,21 @@
// Copyright 2022-2023 The Jujutsu Authors
//
// 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
//
// https://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.
pub fn complete_newline(s: impl Into<String>) -> String {
let mut s = s.into();
if !s.is_empty() && !s.ends_with('\n') {
s.push('\n');
}
s
}